------------------------------------------------------------------------------- Ansible Inventory Handling ------------------------------------------------------------------------------- Inventory.... Inventory format (YAML, JSON, and INI formats) https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html Developing a dynamic inventory - covers both plugin and script https://docs.ansible.com/ansible/latest/dev_guide/developing_inventory.html Test with ansible-inventory --list Output is JSON, or with --yaml flag YAML. You can set the $ANSIBLE_INVENTORY variable which points to the path for your inventory plugin and/or script, e.g. export ANSIBLE_INVENTORY=your_inventory_script.py or export ANSIBLE_INVENTORY=/root/your_inventory_plugin.py ------------------------------------------------------------------------------- Ansible inventory must NOT be empty. If the specified inventory, host pattern or "--limit" option leaves us with no hosts to target, ansible will error, (not produce a warning). However if you include a 'dummy' host, that is later rejected by the host patterns within the playbook, then you will only get a warning.... skipping: no hosts matched A warning rather than a error is preferable in a gitlab CI pipeline. ------------------------------------------------------------------------------- List all matching hosts... ansible -i inventory.yml {playbook} --list-hosts ------------------------------------------------------------------------------- Get Facts in the inventory for a specific host (if there are any) ansible-inventory -i inventory.yml --host seet-hobbit.\* NOTE: the host pattern must only match ONE host! And only output the variables attached to the host, and not the groups the host is in :-( Basically it is VERY limited! ------------------------------------------------------------------------------- Get the groups of a host.. get_groups.yml =======8<--------CUT HERE---------- - hosts: *host pattern* tasks: # report the full inventory groupings (by group) - debug: var=groups run_once: true # report the groups each host belongs to (by host) - debug: var=group_names # List just the groups in inventory # This includes 'all', and 'ungrouped', whether it has hosts or not. - debug: msg='{{ groups.keys }}' run_once: true =======8<--------CUT HERE---------- ansible -i inventory.yml get_groups.yml NOTE: the above needs 'no gather facts' to make faster =======8<--------CUT HERE---------- - hosts: localhost vars: my_groups: "{{ groups|difference(['all', 'ungrouped']) }}" my_hosts: "{{ my_groups|map('extract', groups)|list }}" my_inventory: "{{ dict(my_groups|zip(my_hosts)) }}" tasks: - debug: var: my_inventory =======8<--------CUT HERE---------- =============================================================================== Local inventory plugins... You can put your inventory plugin under ~/.ansible/plugins/inventory/ for example ~/.ansible/plugins/inventory/get_my_inv.py then enable it... export ANSIBLE_INVENTORY_ENABLED=get_my_inv Test with ansible-inventory --list These python inventories require a python class with specific subroutines that can be called. The subroutines places hosts and there groups using class functions... =======8<--------CUT HERE---------- from ansible.plugins.inventory import BaseInventoryPlugin, Cacheable # Uncomment to see errors... # logging.getLogger().setLevel(logging.DEBUG) class InventoryModule(BaseInventoryPlugin, Cacheable): NAME = "get_inv" def verify_file(self, path): return true def parse(self, inventory, loader, path, cache=True): super(InventoryModule, self).parse(inventory, loader, path, cache) cache_key = self.get_cache_key(path) ... server = the FQDN of server group = group to add server to... self.inventory.add_group(group) self.inventory.add_host(server, group=group) =======8<--------CUT HERE---------- ------------------------------------------------------------------------------- Using nmap... Get a list of community pluging available. ansible-doc -t inventory -l Get the list of hosts dynamically using the "nmap" command... nmap -v -n -sn --osscan-limit --max-os-tries 1 132.234.28.0/24 Save this yaml file as "nmap_inventory.yml" then run command ansible-inventory -i nmap_inventory.yml --list =======8<--------CUT HERE---------- --- plugin: community.general.nmap address: 132.234.28.0/24 strict: False ipv4: True ipv6: False ports: False groups: production: "'-prd-' in hostname" development: "'-prd-' not in hostname" ... =======8<--------CUT HERE---------- -------------------------------------------------------------------------------