由于工作需要,经常需要把目标节点获得的信息写入执行节点文件日志。
所以经常用到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都是空的


本文介绍了一种使用Ansible进行日志记录的方法,通过delegate_to和connection模块将目标节点的信息写入执行节点的日志文件中。这种方法适用于需要在集中位置查看多个节点状态的工作场景。
18

被折叠的 条评论
为什么被折叠?



