我必须使用 template.j2 列出文件中的所有服务器。目的是使用 ansible 库存文件生成一个最新的配置文件。所有文件都在 ansible 服务器上。我有一个 generate-projectconf.yml、一个 template.j2 和库存文件。问题是,使用我的方法,本地主机也在生成的文件中。我只想要库存文件中的 IP。
我的 yml 文件看起来像这样
- hosts: localhost
tasks:
- name: modif du project.conf
template: src="template.j2" dest="/tmp/project.conf"
模板.j2 文件
...
ServersList
{% for host in groups[servers_to_monitor] %}
{{ hostvars[host]['ansible_hostname'] }} : {{ hostvars[host]['ansible_eth0']['ipv4']['address'] }}
{% endfor %}
...
库存文件看起来像这样
[DB_Servers]
cas05 ansible_ssh_host=192.168.20.105 ansible_user=ansible
cas06 ansible_ssh_host=192.168.20.106 ansible_user=ansible
[MS_Account_Servers]
acc21 ansible_host=192.168.20.99 ansible_user=ansible
acc22 ansible_host=192.168.20.100 ansible_user=ansible
[MS_Admin_Servers]
adm21 ansible_host=192.168.20.79 ansible_user=ansible
adm22 ansible_host=192.168.20.80 ansible_user=ansible
[MS_Admingui_Servers]
ihm21 ansible_host=192.168.20.81 ansible_user=ansible
要启动它,我执行命令
ansible-playbook generate-projectconf.yml -i /.../inventory --extra-vars "servers_to_monitor=all"
结果如下所示:
...
dep01 : 192.168.20.3
ihm21 : 192.168.20.81
adm21 : ...
...