由于工作需要,经常需要把目标节点获得的信息写入执行节点文件日志。
所以经常用到delegate_to,connection,local_action写法难看,基本不用。
delegate_to和connection最后达到的目标是一致的,就是把目标机器上的{{ }}大括号标记的变量在被代理连接的节点上调用。
示例
---
- name: connection
hosts: controller
vars:
tmplog: /tmp/connection.log
tasks:
- name: create tmplog
shell: test ! -f {{ tmplog }} && touch {{ tmplog }}
failed_when: false
- name: conneciton
shell: echo "connection . {{ inventory_hostname }} $(hostname) ." >> {{ tmplog }}
connection: local
- name: delegate_to
shell: echo "delegate_to . {{ inventory_hostname }} $(hostname) ." >> {{ tmplog }}
delegate_to: localhost
#####
[root@node-1 test_plays]# cat /tmp/connection.log
connection . 192.168.10.3 node-1.domain.tld .
connection . 192.168.10.4 node-1.domain.tld .
delegate_to . 192.168.10.3 node-1.domain.tld .
delegate_to . 192.168.10.4 node-1.domain.tld .
其他节点的tmplog都是空的