ansible 委托给其他主机 [英] ansible delegation to other hosts

查看:21
本文介绍了ansible 委托给其他主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 ansible 2.1 并且我想使用 delegate_to 向一组主机运行命令.我使用 localhost 作为主机参数,并且我想将touch"命令委托给两个 cls 主机我有以下

I use ansible 2.1 and I want to run a command to a group of hosts, using delegate_to. I use localhost as the host param and I want to delegate a "touch" command to both of cls hosts I have the following

---
- hosts: ansible
#  gather_facts: yes

  tasks:
  - debug: var=groups.cls

  - name: touch a file to running host
    shell: echo {{ item }} >> /tmp/{{ inventory_hostname }}
    delegate_to: "{{ item }}"
    with_items: "{{ groups.cls }}"

带输出:

[root@ansible control]# ansible-playbook -i inventory test.yml

PLAY ***************************************************************************

TASK [setup] *******************************************************************
ok: [ansible]

TASK [debug] *******************************************************************
ok: [ansible] => {
    "groups.cls": [
        "cls-host-1",
        "cls-host-2"
    ]
}

TASK [touch a file to running host] ********************************************
changed: [ansible -> cls-host-1] => (item=cls-host-1)
changed: [ansible -> cls-host-2] => (item=cls-host-2)

PLAY RECAP *********************************************************************
ansible                    : ok=3    changed=1    unreachable=0    failed=0

但触摸仅在第一台主机上完成:

but the touch is done only on the first host:

[root@cls-host-1 ~]# more /tmp/ansible
cls-host-1
cls-host-2

有什么问题吗?我可以以任何其他方式委派命令吗?

Is anything wrong? Can I delegate the command with any other way?

推荐答案

我已经使用 Ansible 2.4.0.0 测试了您的剧本的变体:

I've tested a variation of your playbook using Ansible 2.4.0.0:

#!/usr/bin/env ansible-playbook

- hosts: stretch.fritz.box
  tasks:
  - name: touch
    shell: echo {{item}} >>/tmp/{{inventory_hostname}}
    delegate_to: "{{item}}"
    with_items: 
      - jessie.fritz.box
      - short.fritz.box

这工作正常:触摸是在 jessie 和 short 上执行的

This is working fine: the touch is performed on jessie and short

jessie$ cat /tmp/stretch.fritz.box 
jessie.fritz.box

short$ cat /tmp/stretch.fritz.box 
short.fritz.box

也许这个特性是在 Ansible 2.1 到 2.4 之间引入的.

Perhaps this feature was introduced in Ansible between 2.1 and 2.4.

这篇关于ansible 委托给其他主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆