1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

consul_io inventory script: fixes awx and python 3 kv_group (#620) (#1956)

* feat(env): extending configuration options by env variables

* feat(env): extending config option docs

* feat(consul_io): fix byte chain decoding for python3

* fix(pep8): E128

* changelog added

* Update changelogs/fragments/620-consul_io-env-variables-conf-based.yml

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update changelogs/fragments/620-consul_io-env-variables-conf-based.yml

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update changelogs/fragments/620-consul_io-env-variables-conf-based.yml

Co-authored-by: Felix Fontein <felix@fontein.de>

* fix(typo): adding dots

Co-authored-by: Davy Bondeau <davy.bondeau@gmail.com>
Co-authored-by: Felix Fontein <felix@fontein.de>
(cherry picked from commit 3f882ee6a2)

Co-authored-by: atbore-phx <atbore-phx@users.noreply.github.com>
This commit is contained in:
patchback[bot] 2021-03-04 08:27:19 +01:00 committed by GitHub
parent 693efb35b3
commit 53a5cdaed7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 5 deletions

View file

@ -0,0 +1,5 @@
---
bugfixes:
- consul_io inventory script - kv_groups - fix byte chain decoding for Python 3 (https://github.com/ansible-collections/community.general/pull/620).
minor_changes:
- consul_io inventory script - conf options - allow custom configuration options via env variables (https://github.com/ansible-collections/community.general/pull/620).

View file

@ -28,7 +28,7 @@ This script can be run with the switches
--host to restrict the inventory to a single named node. (requires datacenter config) --host to restrict the inventory to a single named node. (requires datacenter config)
The configuration for this plugin is read from a consul_io.ini file located in the The configuration for this plugin is read from a consul_io.ini file located in the
same directory as this inventory script. All config options in the config file same directory as this inventory script or via environment variables. All config options in the config file
are optional except the host and port, which must point to a valid agent or are optional except the host and port, which must point to a valid agent or
server running the http api. For more information on enabling the endpoint see. server running the http api. For more information on enabling the endpoint see.
@ -36,17 +36,23 @@ http://www.consul.io/docs/agent/options.html
Other options include: Other options include:
'bulk_load'
boolean flag. Load all possible data before building inventory JSON
If true, script processes in-memory data. JSON generation reduces drastically
This can also be set with the environmental variable CONSUL_BULK_LOAD.
'datacenter': 'datacenter':
which restricts the included nodes to those from the given datacenter which restricts the included nodes to those from the given datacenter
This can also be set with the environmental variable CONSUL_DATACENTER This can also be set with the environmental variable CONSUL_DATACENTER.
'url': 'url':
the URL of the Consul cluster. host, port and scheme are derived from the the URL of the Consul cluster. host, port and scheme are derived from the
URL. If not specified, connection configuration defaults to http requests URL. If not specified, connection configuration defaults to http requests
to localhost on port 8500. to localhost on port 8500.
This can also be set with the environmental variable CONSUL_URL This can also be set with the environmental variable CONSUL_URL.
'domain': 'domain':
@ -57,6 +63,15 @@ have consul hooked into your DNS server for these to resolve. See the consul
DNS docs for more info. DNS docs for more info.
which restricts the included nodes to those from the given datacenter which restricts the included nodes to those from the given datacenter
This can also be set with the environmental variable CONSUL_DOMAIN.
'suffixes':
boolean flag. By default, final JSON is built based on all available info in consul.
Suffixes means that services groups will be added in addition to basic information. See servers_suffix for additional info
There are cases when speed is preferable than having services groups
False value will reduce script execution time drastically.
This can also be set with the environmental variable CONSUL_SUFFIXES.
'servers_suffix': 'servers_suffix':
@ -64,6 +79,7 @@ defining the a suffix to add to the service name when creating the service
group. e.g Service name of 'redis' and a suffix of '_servers' will add group. e.g Service name of 'redis' and a suffix of '_servers' will add
each nodes address to the group name 'redis_servers'. No suffix is added each nodes address to the group name 'redis_servers'. No suffix is added
if this is not set if this is not set
This can also be set with the environmental variable CONSUL_SERVERS_SUFFIX.
'tags': 'tags':
@ -71,11 +87,13 @@ boolean flag defining if service tags should be used to create Inventory
groups e.g. an nginx service with the tags ['master', 'v1'] will create groups e.g. an nginx service with the tags ['master', 'v1'] will create
groups nginx_master and nginx_v1 to which the node running the service groups nginx_master and nginx_v1 to which the node running the service
will be added. No tag groups are created if this is missing. will be added. No tag groups are created if this is missing.
This can also be set with the environmental variable CONSUL_TAGS.
'token': 'token':
ACL token to use to authorize access to the key value store. May be required ACL token to use to authorize access to the key value store. May be required
to retrieve the kv_groups and kv_metadata based on your consul configuration. to retrieve the kv_groups and kv_metadata based on your consul configuration.
This can also be set with the environmental variable CONSUL_TOKEN.
'kv_groups': 'kv_groups':
@ -86,6 +104,7 @@ names to which the node should be added e.g. if the inventory contains node
'nyc-web-1' in datacenter 'nyc-dc1' and kv_groups = 'ansible/groups' then the key 'nyc-web-1' in datacenter 'nyc-dc1' and kv_groups = 'ansible/groups' then the key
'ansible/groups/nyc-dc1/nyc-web-1' will be queried for a group list. If this query 'ansible/groups/nyc-dc1/nyc-web-1' will be queried for a group list. If this query
returned 'test,honeypot' then the node address to both groups. returned 'test,honeypot' then the node address to both groups.
This can also be set with the environmental variable CONSUL_KV_GROUPS.
'kv_metadata': 'kv_metadata':
@ -95,6 +114,7 @@ find a json dictionary of metadata entries. If found, each key/value pair in the
dictionary is added to the metadata for the node. eg node 'nyc-web-1' in datacenter dictionary is added to the metadata for the node. eg node 'nyc-web-1' in datacenter
'nyc-dc1' and kv_metadata = 'ansible/metadata', then the key 'nyc-dc1' and kv_metadata = 'ansible/metadata', then the key
'ansible/metadata/nyc-dc1/nyc-web-1' should contain '{"databse": "postgres"}' 'ansible/metadata/nyc-dc1/nyc-web-1' should contain '{"databse": "postgres"}'
This can also be set with the environmental variable CONSUL_KV_METADATA.
'availability': 'availability':
@ -102,6 +122,7 @@ if true then availability groups will be created for each service. The node will
be added to one of the groups based on the health status of the service. The be added to one of the groups based on the health status of the service. The
group name is derived from the service name and the configurable availability group name is derived from the service name and the configurable availability
suffixes suffixes
This can also be set with the environmental variable CONSUL_AVAILABILITY.
'available_suffix': 'available_suffix':
@ -109,10 +130,12 @@ suffix that should be appended to the service availability groups for available
services e.g. if the suffix is '_up' and the service is nginx, then nodes with services e.g. if the suffix is '_up' and the service is nginx, then nodes with
healthy nginx services will be added to the nginix_up group. Defaults to healthy nginx services will be added to the nginix_up group. Defaults to
'_available' '_available'
This can also be set with the environmental variable CONSUL_AVAILABLE_SUFFIX.
'unavailable_suffix': 'unavailable_suffix':
as above but for unhealthy services, defaults to '_unavailable' as above but for unhealthy services, defaults to '_unavailable'
This can also be set with the environmental variable CONSUL_UNAVAILABLE_SUFFIX.
Note that if the inventory discovers an 'ssh' service running on a node it will Note that if the inventory discovers an 'ssh' service running on a node it will
register the port as ansible_ssh_port in the node's metadata and this port will register the port as ansible_ssh_port in the node's metadata and this port will
@ -337,7 +360,7 @@ class ConsulInventory(object):
else: else:
index, groups = self.consul_api.kv.get(key) index, groups = self.consul_api.kv.get(key)
if groups and groups['Value']: if groups and groups['Value']:
for group in groups['Value'].split(','): for group in groups['Value'].decode().split(','):
self.add_node_to_map(self.nodes_by_kv, group.strip(), node) self.add_node_to_map(self.nodes_by_kv, group.strip(), node)
def load_data_from_service(self, service_name, service, node_data): def load_data_from_service(self, service_name, service, node_data):
@ -488,7 +511,10 @@ class ConsulConfig(dict):
setattr(self, arg, getattr(args, arg)) setattr(self, arg, getattr(args, arg))
def read_env_vars(self): def read_env_vars(self):
env_var_options = ['datacenter', 'url'] env_var_options = ['host', 'token', 'datacenter', 'servers_suffix',
'tags', 'kv_metadata', 'kv_groups', 'availability',
'unavailable_suffix', 'available_suffix', 'url',
'domain', 'suffixes', 'bulk_load']
for option in env_var_options: for option in env_var_options:
value = None value = None
env_var = 'CONSUL_' + option.upper() env_var = 'CONSUL_' + option.upper()