0

I want to build ansible inventory and assign hosts to respective groups based on the values gathered from Clients.

Eg: Information of all the clients and their respective group will be pushed to ansible server every night in the below format.

File Format

Hostgroup1,Hostname1
Hostgroup1,Hostname2
Hostgroup1,Hostname3
Hostgroup2,Hostname1
Hostgroup2,Hostname2

From the content of above file i need to create ansible inventory

  • what have you tried ? what is the expected output ? Ansible work with Yaml put can accept other format. – Kiwy May 16 '18 at 12:47
1

You have several options you can use here.

1) Write a script which takes those files and generates the "inventory file" format that ansible uses. Then run that script on a cron job or something after all the uploads take place.

2) Write a script which generates output format complying with the Ansible inventory JSON format, and use it as such: https://docs.ansible.com/ansible/2.5/plugins/inventory/script.html

3) Use the add_host task type as the very first task in your playbook (with a localhost host) with a loop to add the hosts.

add_host:
  name: "{{ item.hostname }}"
  groups: "{{ item.group }}"
with_items: "{{ somehow inject your list here }}"
| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.