把结果保存成一个文件(比如 /tmp/ping_baidu.log)
好,那我们把 Playbook 改一下,让每个主机的 ping
结果写到控制端(Ansible 管理机)上的一个文件,比如 /tmp/ping_results/10.0.19.207.log
。
保存为 ping_baidu.yml
:
- hosts: webservers
become: no
tasks:
- name: Ping www.baidu.com from each host
shell: ping -c 4 www.baidu.com
register: ping_result
- name: Ensure result directory exists on control node
delegate_to: localhost
file:
path: /tmp/ping_results
state: directory
mode: '0755'
- name: Save ping result to file on control node
delegate_to: localhost
copy:
content: "{{ inventory_hostname }}:\n{{ ping_result.stdout }}"
dest: "/tmp/ping_results/{{ inventory_hostname }}.log"
ansible-playbook -i /etc/ansible/hosts/inventory.ini ping_baidu.yml
执行完后,控制端会有目录 /tmp/ping_results/
,里面包含每台机器的结果:
/tmp/ping_results/10.0.19.207.log
/tmp/ping_results/10.0.19.208.log
文件内容类似:
10.0.19.207:
PING www.a.shifen.com (14.215.177.39) 56(84) bytes of data.
64 bytes from 14.215.177.39: icmp_seq=1 ttl=54 time=12.3 ms
64 bytes from 14.215.177.39: icmp_seq=2 ttl=54 time=13.1 ms
...