Ansible 向其他东道主的可接受委派

Ansible 向其他东道主的可接受委派,ansible,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 }} >> /tm

我使用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

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

我已经使用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身上进行

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

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

可能这项功能是在Ansible 2.1和2.4之间引入的。

尝试使用\u items:groups.cls
而不是尝试使用\u items:groups.cls:[弃用警告]:使用裸变量是弃用的。更新您的剧本,以便环境值使用完整的变量语法(“{groups.cls}”)。此功能将在将来的版本中删除。可以通过在ansible.cfg中设置Deprecation\u warnings=False来禁用弃用警告。