【发布时间】:2016-05-23 14:14:59
【问题描述】:
我使用 ansible 2.1,我想使用 delegate_to 对一组主机运行命令。我使用 localhost 作为主机参数,我想将“触摸”命令委托给两个 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
有什么问题吗?我可以通过其他方式委派命令吗?
【问题讨论】:
-
改用
with_items: groups.cls -
尝试使用_items:groups.cls 我得到:[弃用警告]:不推荐使用裸变量。更新您的 playbook,以便环境值使用完整的变量语法 ('{{groups.cls}}')。此功能将在未来的版本中删除。可以通过在 ansible.cfg 中设置 deprecation_warnings=False 来禁用弃用警告。
标签: ansible