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都是空的

 

转载于:https://www.cnblogs.com/wangl-blog/p/9039656.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值