ansible delegation to other hosts

自作多情 提交于 2021-02-10 13:58:34

问题


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

with output:

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


回答1:


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

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

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



来源:https://stackoverflow.com/questions/37393428/ansible-delegation-to-other-hosts

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!