ansible代表团到其他主机

时间:2016-05-23 14:14:59

标签: ansible

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

---
- 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

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

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

有什么不对吗?我可以用其他任何方式委托命令吗?

1 个答案:

答案 0 :(得分:0)

我已使用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和短

上进行的
jessie$ cat /tmp/stretch.fritz.box 
jessie.fritz.box

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

也许这个功能是在Ansible 2.1和2.4之间引入的。