这里用到ansible的setup模块,来获取系统信息

[root@node1 tomcat]#cat /etc/ansible/roles/hosts/tasks/main.yml 
- name: copy /etc/hosts
  template: src=hosts.j2 dest=/etc/hosts
[root@node1 tomcat]#cat templates/hosts.j2 
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
{{ ansible_eth0.ipv4.address }} {{ ansible_nodename }}

备注:eth0是网卡名,可根据实际替换