我正在尝试创建一个任务,该任务将获取清单中的主机目标IP地址和主机名,然后将其保存到本地目录中的文件中。
很乐意像这样保存数据:
hostname:ip-address
从哪里开始?
答案 0 :(得分:0)
我建议使用“ delegate_to
”,我在计算机上尝试了一下,然后正常工作。我将第一步删除该文件(如果存在),以便可以多次执行。这是一个非常快速的示例,我建议对路径使用变量,等等。但是我认为您可以理解
---
- hosts: your_inventory
tasks:
- name: delete the file if exists
file:
path: /home/yourpath/host_ip.txt
state: absent
delegate_to: localhost
- name: get data to a file
lineinfile:
dest: /home/yourpath/host_ip.txt
create: yes
line: "{{hostvars[inventory_hostname].ansible_hostname}}:{{hostvars[inventory_hostname].ansible_default_ipv4.address}}"
delegate_to: localhost