【问题标题】:ansible delegation to other hostsansible 委托给其他主机
【发布时间】: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


【解决方案1】:

我已经使用 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 之间引入的。

【讨论】:

    猜你喜欢
    • 2018-05-07
    • 1970-01-01
    • 2022-11-04
    • 1970-01-01
    • 2019-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多