3
votes

I'm running an ansible playbook on a list of hosts with a host file:

[consul]
${HOST1} ansible_ssh_host=${HOST1} ansible_ssh_user=devops ansible_ssh_pass=blabla
${HOST2} ansible_ssh_host=${HOST2} ansible_ssh_user=devops ansible_ssh_pass=blabla
.......so on...

The thing is that I need to pass a different variable for each host. I know of the flag -e that allows me to send a variable with the ansible-playbook command but it's not for each of the hosts.

I'm running the playbook with this:

ansible-playbook -vvvv site.yml

How can I pass a different var for each host?

Thanks!

Note: I'm using ansible 1.7.1

1
What kind of variable (value) is that? You could build a mapping in a var somewhere, use host_vars (group_vars), query the inventory to discover facts about other hosts, etc.weirdan
Since you specifically say you want to "pass a different variable for each host", you should check out host_vars which are available in your 1.7 version: docs.ansible.com/ansible/intro_inventory.htmldan_linder

1 Answers

2
votes

Two ways you should be able to do this:

1) Include the variable in your host file:

[consul]
${HOST1} ansible_ssh_host=${HOST1} .... myvar=x
${HOST2} ansible_ssh_host=${HOST2} .... myvar=y

2) Or use the include_vars task to load a file based on the host name

include_vars: "{{ ansible_ssh_host }}.yml"

The second method is good if you have a lot of variables to load for a host.

For more complex cases the lookups module might help: http://docs.ansible.com/ansible/playbooks_lookups.html