2
votes

I have multiple host details under one group in my Ansible hosts file. Like below,

[web-server]
10.0.0.1 name=apache ansible_ssh_user=username
10.0.0.2 name=nginx ansible_ssh_user=username 

My ansible playbook,

---
  - hosts: web-server[0]
    roles:
      - role: apache

These details gets added dynamically in the hosts file and I have no control over the order of lines getting added under a group. Hence I can't use the logic web-server[0] or web-server[1]

I want to mention the host by filtering based on the "name" parameter in the playbook since name will be unique. Is there a way to do this, Kindly help.

1
Can you not create new groups for your apache and nginx servers?Xiong Chiamiov

1 Answers

2
votes

If you can modify inventory file generation process, make it like this:

[web-server]
apache ansible_host=10.0.0.1 ansible_ssh_user=username
nginx ansible_host=10.0.0.2 ansible_ssh_user=username

And then use:

- hosts: apache