ansible的delegate_to、connection、和local_action

进程间通信模块

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: </span>/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都是空的
补充: connection: local 可以针对playbook全局使用,而delegate_to: localhost 只能针对role task等局部代理

posted @   ishmaelwanglin  阅读(8453)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示