ansible的delegate_to、connection、和local_action


由于工作需要,经常需要把目标节点获得的信息写入执行节点文件日志。

所以经常用到delegate_to和connection,而local_action写法难看,基本不用。

delegate_to和connection最后达到的目标是一致的,

就是把目标服务器上的{{ }}大括号标记的变量在代理连接的节点(delegate_to: host)上调用。

示例

inventory_file : /etc/ansible/hosts

[controller]
192.168.10.3 node-1
192.168.10.4 node-2
...

playbook:
--- - 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
...
inventory_hostname 当前task的host在inventory文件中的hostname, $(hostname)代理host上的hostname ##### [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 . 在node-1上执行,node-2的tmplog都是空的

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM