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