Ansible Facts Variables usage tutorial

Ansible Facts Variables usage tutorial

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

Advertisements

2 thoughts on “Ansible Facts Variables usage tutorial

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s