IP FQDN HOSTNAME
(FQDN, Fully qualified domain name, 完全資格域名),修改 host2.j2 產生所有 inventroy hosts。
[deyu1@kvm19 ansible]$ vim hosts.j2 [deyu1@kvm19 ansible]$ cat hosts.j2 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain {% for host in groups['all'] %} {{ hostvars[host]['ansible_default_ipv4']['address'] }} {{ hostvars[host] ['ansible_fqdn'] }} {{ hostvars[host]['ansible_hostname'] }} {% endfor %}
[deyu1@kvm19 ansible]$ vim hosts.yml [deyu1@kvm19 ansible]$ cat hosts.yml --- - name: Create /etc/myhosts hosts: all tasks: - name: Template a file to /etc/myhosts template: src: /home/deyu1/ansible/hosts.j2 dest: /etc/myhosts when: '"dev" in group_names'