Ansible Facts Variables Usage
Ansible Version: 2.1.0.0
Inventory File: inventory
This is the content of the file: “inventory”
[elasti]
192.168.61.100
192.168.61.101
192.168.61.102
[elasti:vars]
ansible_user=stark
ansible_ssh_pass=12345
===================================================================
Template File: sample_template.j2
This is the content of the file: “sample_template.j2”
{% for host in groups[‘elasti’] %}
{{ host }}
{% endfor %}
{{ hostvars[groups[‘elasti’][0]][‘ansible_eth0’][‘ipv4’][‘address’] }}
{{ hostvars[groups[‘elasti’][1]][‘ansible_eth0’][‘ipv4’][‘address’] }}
{{ hostvars[groups[‘elasti’][2]][‘ansible_eth0’][‘ipv4’][‘address’] }}
{{ hostvars[groups[‘elasti’][0]][‘ansible_eth1’][‘ipv4’][‘address’] }}
{{ hostvars[groups[‘elasti’][1]][‘ansible_eth1’][‘ipv4’][‘address’] }}
{{ hostvars[groups[‘elasti’][2]][‘ansible_eth1’][‘ipv4’][‘address’] }}
discovery.zen.ping.unicast.hosts: [{% for host in groups[‘elasti’] %}”{{ host }}”,{% endfor %}]
============================================================================
Playbook File: play.yml
This is the content of the file: “play.yml”
—
– hosts: elasti
gather_facts: yes
become: yes
tasks:
– name: copy template into config file
template: src=sample_template.j2 dest=/tmp/output.txt
===========================================================================
Command: ansible-playbook play.yml -K
Output File: /tmp/output.txt
This is the content of output generated of file: /tmp/output.txt
192.168.61.100
192.168.61.101
192.168.61.102
192.168.61.100
192.168.61.101
192.168.61.102
10.0.3.15
10.0.3.15
10.0.3.15
discovery.zen.ping.unicast.hosts: [“192.168.61.100″,”192.168.61.101″,”192.168.61.102”,]
============================================================================
Reference Link: http://docs.ansible.com/ansible/faq.html