我想将一个特定组下的主机名和ssh_host从库存文件写入我的localhost中的文件。我试过这个:
我的角色
set_fact: 主持人:“{{hosts_list | default({})| combine(…
一个选择是使用 lineinfile 和 delegate_to 本地主机。
- hosts: test_01 tasks: - set_fact: my_inventory_hostname: "{{ ansible_host }}" - tempfile: delegate_to: localhost register: result - lineinfile: path: "{{ result.path }}" line: "inventory_hostname: {{ my_inventory_hostname }}" delegate_to: localhost - debug: msg: "inventory_hostname: {{ ansible_host }} stored in {{ result.path }}"
注意:没有必要引用条件。条件默认扩展。
when: my_group in group_names
非常感谢你的回答@ Vladimir Botka
我想出了将属于某个组的主机写入文件的另一个要求,下面的代码可以帮助解决这个问题(添加到@ Vladimir的解决方案)。
- hosts: all become: no gather_facts: no tasks: - set_fact: my_inventory_hostname: "{{ ansible_host }}" - lineinfile: path: temp/hosts line: "inventory_hostname: {{ my_inventory_hostname }}" when: inventory_hostname in groups['my_group'] delegate_to: localhost - debug: msg: "inventory_hostname: {{ ansible_host }}"