我正在尝试获取主机的主机名和 IP 地址并将它们保存到文件中。
我有这个解决方案;
- name: Create File with hosts and IP address.
when: inventory_hostname in groups['local']
lineinfile:
dest: "{{store_files_path}}/{{ansible_date_time.date}}/{{ansible_date_time.time}}/hosts.txt"
create: yes
line: "{{hostvars[inventory_hostname].ansible_hostname}}:{{hostvars[inventory_hostname].ansible_default_ipv4.address}}"
Run Code Online (Sandbox Code Playgroud)
但问题出在我的主机文件中,我有两个组,local并且Servers. 我想要得到Servers唯一的,而不是local唯一的组localhost。
我已经尝试过以下行,但它不起作用,它给了我一个错误。
line: "{{ hostvars[ groups['Servers'][0] ].ansible_hostname }} :{{ hostvars[ groups['Servers'][0] ].ansible_default_ipv4.address }}"
Run Code Online (Sandbox Code Playgroud)
我查了一下,发现是这样的,我该怎么办?