How to exclude localhost from groups when host action is localhost with impossible

I need to specify the entire server in a file using template.j2. The goal is to update the config file with the inventory file. All files are located on an irreplaceable server. I have a generate-projectconf.yml file, template.j2 and an inventory file. The problem is with my method, localhost is also in the generated file. I only need the IP address which is in the inventory file.

My yml file looks like this

- hosts: localhost
  tasks:
 - name: modif du project.conf
   template: src="template.j2" dest="/tmp/project.conf" 

      

file template.j2

...
ServersList
    {% for host in  groups[servers_to_monitor] %}
    {{ hostvars[host]['ansible_hostname'] }} : {{ hostvars[host]['ansible_eth0']['ipv4']['address'] }}
    {% endfor %}
...

      

The inventory file looks like this:

[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

      

To run this, I run the command

ansible-playbook  generate-projectconf.yml -i /.../inventory --extra-vars "servers_to_monitor=all"

      

The result looks like this:

...
dep01 : 192.168.20.3
ihm21 : 192.168.20.81
adm21 : ...
...

      

+3


source to share


1 answer


Exclude the current host (in your case localhost

) from the server list in your template:



{% for host in groups[servers_to_monitor] | difference([inventory_hostname]) %}

      

+2


source







All Articles