Thank you for reading this post, don't forget to subscribe!
1 2 3 4 5 6 |
- name: Generate /etc/hosts file template: src: templates/hosts.j2 dest: /etc/hosts backup: yes tags: fist |
Шаблон:
1 2 3 4 5 6 7 8 9 10 11 |
# templates/hosts.j2 # {{ ansible_managed }} 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 # Network nodes as generated through Ansible. {% for host in play_hosts %} {% if 'ansible_eth0' in hostvars[host] %} {{ hostvars[host]['ansible_eth0']['ipv4']['address'] }} {{ host }} {% endif %} {% endfor %} |
Инвентори-файл:
1 2 3 4 5 6 7 |
[srv-dev] domain_host1 ansible_host=10.10.1.2 domain_host2 ansible_host=10.10.2.2 domain_host3 ansible_host=10.10.2.3 [srv-dev:vars] ansible_python_interpreter=/usr/bin/python2 |
ЗЫ: не забываем включить gather_facts: yes