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

所以经常用到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都是空的

ansible的delegate_to、connection、和local_action的更多相关文章

  1. 4.ansible的delegate_to

    完成发布流程如下 first 修改nginx 配置文件下线 web1-2 使用 delegate_to 将默认hosts指定为 nginx主机 使用remote_user 将用户 锁定为 root s ...

  2. Ansible 日常使用技巧 - 运维总结

    Ansible默认只会创建5个进程并发执行任务,所以一次任务只能同时控制5台机器执行.如果有大量的机器需要控制,例如20台,Ansible执行一个任务时会先在其中5台上执行,执行成功后再执行下一批5台 ...

  3. 使用ansible kubectl插件连接kubernetes pod以及实现原理

    ansible kubectl connection plugin ansible是目前业界非常火热的自动化运维工具.ansible可以通过ssh连接到目标机器上,从而完成指定的命令或者操作. 在ku ...

  4. ansible 错误记录(1)

    基本环境:docker基于centos7 在docker里面安装ansible 不管是在root还是普通用户下执行 ansible all -m ping  都报如下错误: 172.20.1.1 | ...

  5. 使用Kubespray在ubuntu上自动部署K8s1.9.0集群

    Kubespray 是 Kubernetes incubator 中的项目,目标是提供 Production Ready Kubernetes 部署方案,该项目基础是通过 Ansible Playbo ...

  6. 记录k8s:k8s1.8.4无坑离线安装

    安装部署: 1. 使用vagrant 准备3太虚拟机,自己使用Vbox 准备3太也可以. 2. 按照 https://github.com/gjmzj/kubeasz 安装. 3. 使用letsenc ...

  7. 【Ansible】SSH Error: ssh_exchange_identification: Connection closed by remote host

    ansible ssh到目标机器 时好时坏,报错:  SSH Error: ssh_exchange_identification: Connection closed by remote host ...

  8. ansible 任务委派 delegate_to

    ansible 任务委派功能delegate_to run_noce: true  在一个主机上面只执行一次一个任务. ,如果没有这个参数的话,每个playbook中的组的主机都会执行一次. 我们有的 ...

  9. Ansible安装部署以及常用模块详解

    一.  Ansible 介绍Ansible是一个配置管理系统configuration management system, python 语言是运维人员必须会的语言, ansible 是一个基于py ...

随机推荐

  1. 一些有意思的Linux命令

    1.输出你最常用的十条命令 history|awk '{print $2}'|awk 'BEGIN {FS="|"} {print $1}'|sort|uniq -c|sort - ...

  2. Python——Tk控件说明

    控件 描述 具体说明 Button 按钮  点此链接 Canvas 提供绘图形状的功能,包含图像和位图  点此链接 Checkbutton 多选框  点此链接 Entry 单行文本框  点此链接 Fr ...

  3. Amazon SNS (Simple Notification Service) Using C# and Visual Studio

    SNS (Amazon Simple Notification Services) Amazon SNS (Amazon Simple Notification Services) is a noti ...

  4. Azure - Create your first function using Visual Studio

    Azure Functions lets you execute your code in a serverless environment without having to first creat ...

  5. python控制台输出带颜色的文字方法

    #格式: 设置颜色开始 :\033[显示方式;前景色;背景色m   注意:开头部分的三个参数:显示方式,前景色,背景色是可选参数,可以只写其中的某一个:另外由于表示三个参数不同含义的数值都是唯一的没有 ...

  6. [测试篇]MarkDown之代码块行号+折叠图片

    对比测试代码编号 $(function(){ $('pre code').each(function(){ texts = $(this).text().replace(/&(?!#?[a-z ...

  7. hdu 5536 Chip Factory (01 Trie)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题面; Chip Factory Time Limit: 18000/9000 MS (Java/O ...

  8. GIT-常规操作

    本地安装git, 安装文件: Git客户端: 可百度搜索:GIT64位或GIT32位等关键字找到相应的版本进行下载. 本地地址:D:\20-git\Git-2.20.1-64-bit.exe 也可百度 ...

  9. SpringBoot入门-2(两种热部署方式)

    在编写代码的时候,你会发现我们只是简单把打印信息改变了,就需要重新部署,如果是这样的编码方式,那么我们估计一天下来就真的是打几个Hello World就下班了.那么如何解决热部署的问题呢?那就是spr ...

  10. SpringBoot系列: 设计Restful风格的API

    RESTful 架构REST 并非一种技术或规范, 而是一种架构风格, 如果一个架构符合Rest的约束条件和原则, 就可以称作是 RESTful 架构. REST全称是Representational ...