3

template.j2を使用してすべてのサーバーをファイルにリストアップする必要があります。その目的は、可能なインベントリファイルで最新の設定ファイルを生成することです。すべてのファイルは、不可能なサーバー上にあります。 私はgenerate-projectconf.yml、template.j2とインベントリファイルを持っています。 問題は、私の方法では、ローカルホストも生成されたファイルにあることです。私はインベントリファイルにあるIPだけを必要とします。ホストアクションがlocalhostの場合にローカルホストをグループから除外する方法

マイYMLファイルは、インベントリファイルは、これを起動するには、その

[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 

のように見えること

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

template.j2ファイル

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

のように、私はコマンドを実行するに見えます

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

結果は次のようになります。

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

答えて

2

は、テンプレート内のサーバーのリストから(あなたのケースlocalhostで)現在のホストを除外:

{% for host in groups[servers_to_monitor] | difference([inventory_hostname]) %} 
完璧に動作
+0

、どうもありがとうございました! –

関連する問題