2016-12-27 4 views
2

各ノードのプライベートIPを取得する方法はありますか?このテンプレートでは不可能 - インベントリファイルからホスト名とプライベートIPを取得

{{ ansible_managed }} 

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 
::1   localhost localhost.localdomain localhost6 localhost6.localdomain6 

{% for item in groups['all'] %} 
{{ hostvars[item]['ansible_ssh_host'] }} {{ item }} 
{% endfor %} 

は、私は以下の結果が得られます。

Ansible managed 

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 
::1   localhost localhost.localdomain localhost6 localhost6.localdomain6 

127.0.0.1 arithproducer 
127.0.0.1 controller 
127.0.0.1 restapi 

は私のインベントリファイルビーイング:

[control] 
controller ansible_ssh_host=192.168.50.3 ansible_ssh_user=vagrant 

[servers] 
restapi ansible_ssh_host=192.168.50.5 ansible_ssh_user=vagrant 

[producers] 
arithproducer ansible_ssh_host=192.168.50.4 ansible_ssh_user=vagrant 

[services:children] 
servers 
producers 

アイデアは私ができるように、この情報を得ることです制御ノードのファイル/etc/hosts/を入力してください

+1

をテスト済みファイル。正常に動作します(つまり、インベントリファイルで指定されたIPアドレスが生成されます)。あなたは何か間違っているが、それは問題ではない。 – techraf

答えて

0

以下のテンプレートとタスクファイルを使用できます。

テンプレートファイル

{{ ansible_managed }} 

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 
::1   localhost localhost.localdomain localhost6 localhost6.localdomain6 


{% for group in groups %} 
{% if groups[group] and group != 'all' %} 
{% for host in groups[group] %} 
{{hostvars[host].ansible_default_ipv4.address}} {{ ansible_hostname }} 
{% endfor %} 

{% endif %} 
{% endfor %} 

タスク

--- 
- name: Create HostsFile 
    hosts: development 
    user: vagrant 
    become: yes 
    become_method: sudo 

    tasks: 
    - name: Create Hosts File 
     template: src=hostsFile.j2 dest=/tmp/hosts owner=root group=root 
関連する問題