Ansible: `add_host` 模块只在播放适用的第一个主机上执行一次

创建于 2015-04-14  ·  3评论  ·  资料来源: ansible/ansible

问题类型:

错误报告

类似报告#9931

Ansible 版本:
  • Ansible 1.8.4(分离的 HEAD ebc8d48d34)最后更新时间为 2015/04/14 17:59:44(GMT +000)
  • Ansible 1.9.0 (分离的 HEAD 717ffe2bea) 最后更新 2015/04/14 17:58:24 (GMT +000)
  • Ansible 2.0.0 (devel 0345b675f8) 最后更新时间 2015/04/14 17:27:56 (GMT +000)
    环境:

Ubuntu 14.04

概括:

add_host任务仅在播放主机列表中的单个主机上执行。

重现步骤:

剧本

---
- name: Environment group_vars
  hosts: all
  gather_facts: no
  tasks:

    - debug: var=inventory_hostname

    - name: Add to group
      add_host: name={{inventory_hostname}} groups={{env}}

- name: Groups
  hosts: all
  gather_facts: no
  tasks:
    - debug: var=group_names
    - debug: var=groups[env]
    - fail: msg="done"

库存文件try.yml

10.0.0.70
10.0.1.171

执行

ansible-playbook -i ansible/try ansible/try.yml -e 'env=acceptance'

预期成绩:

我希望使用add_host模块Add to group的任务执行两次:对剧中的每个主机执行一次。 在演出结束时,每个主持人都将在acceptance

实际结果:
PLAY [Environment group_vars] *************************************************

TASK: [debug var=inventory_hostname] ******************************************
ok: [10.0.0.70] => {
    "var": {
        "inventory_hostname": "10.0.0.70"
    }
}
ok: [10.0.1.171] => {
    "var": {
        "inventory_hostname": "10.0.1.171"
    }
}

TASK: [Add to group] **********************************************************
ok: [10.0.0.70]

PLAY [Groups] *****************************************************************                                                                                                                                                                                        [30/1905]

TASK: [debug var=group_names] *************************************************
ok: [10.0.0.70] => {
    "var": {
        "group_names": [
            "acceptance",
            "ungrouped"
        ]
    }
}
ok: [10.0.1.171] => {
    "var": {
        "group_names": [
            "ungrouped"
        ]
    }
}

TASK: [debug var=groups[env]] *************************************************
ok: [10.0.0.70] => {
    "var": {
        "groups[env]": [
            "10.0.0.70"
        ]
    }
}

TASK: [debug var=groups[env]] *************************************************
ok: [10.0.0.70] => {
    "var": {
        "groups[env]": [
            "10.0.0.70"
        ]
    }
}
ok: [10.0.1.171] => {
    "var": {
        "groups[env]": [
            "10.0.0.70"
        ]
    }
}

TASK: [fail msg="done"] *******************************************************
failed: [10.0.0.70] => {"failed": true}
msg: done
failed: [10.0.1.171] => {"failed": true}
msg: done

FATAL: all hosts have already failed -- aborting

最有用的评论

@bcoca ,如果我想为另一个组的每个主机添加新主机到库存怎么办?

所有3条评论

可能的误解

你好!

非常感谢您提交给 Ansible。 它真诚地对我们意义重大。

我们认为您提交的罚单有点被误解了,因为有一件事与陈述的有点不同。

特别是,这是预期的行为,记录在本页(注释)中:

http://docs.ansible.com/add_host_module.html#id3

将来,这可能是更适合用户列表的主题,如果您需要有关上述内容的更多帮助,也可以在此处发布。

再次感谢您对此以及您对 Ansible 的兴趣!

在任何情况下,您都不会将新主机添加到清单中,您可能希望改用 group_by 指令。

@bcoca ,如果我想为另一个组的每个主机添加新主机到库存怎么办?

此页面是否有帮助?
0 / 5 - 0 等级