【问题标题】:Default value for ansible hosts in playbook?剧本中ansible主机的默认值?
【发布时间】:2017-09-13 03:34:01
【问题描述】:

我的示例剧本(适用于 ansible 2.1)是:

---
# This is sample playbook.

- name: add sample_role
  hosts: '{{ target }}'
  become: true
  become_user: root
  roles:
    - sample role

当我运行它时,一切都很好:

ansible-playbook -i staging test_playbook.yml --extra-vars "target=192.168.15.29"

如何省略目标?

我尝试过这样的事情:

  hosts: '{{ target | default(all) }}'

  hosts: '{{ target | default(hostvars) }}'

然后运行:

ansible-playbook -i staging test_playbook.yml

,但不幸的是:

错误! 'all' 未定义

或:

错误! 'hostvars' 未定义

【问题讨论】:

    标签: ansible


    【解决方案1】:

    如果你只是简单地添加 all 而不带引号,那么它将它作为变量,所以你可以像这样修复它:

    hosts: '{{ target | default("all") }}'

    hostvars 而言,这是 ansible 中的保留字,我认为不能用于此目的。它的价值是这样的:

    ok: [localhost] => {
        "msg": {
            "localhost": {
                "ansible_check_mode": false,
                "ansible_version": {
                    "full": "2.1.0.0",
                    "major": 2,
                    "minor": 1,
                    "revision": 0,
                    "string": "2.1.0.0"
                },
                "group_names": [],
                "groups": {
                    "all": [
                        "localhost"
                    ],
                    "ungrouped": []
                },
                "inventory_dir": null,
                "inventory_file": null,
                "inventory_hostname": "localhost",
                "inventory_hostname_short": "localhost",
                "omit": "__omit_place_holder__41c600ef78930ed8b38e6eed4e5b5ab51199729e",
                "playbook_dir": "/Users/xyz/test-ansible"
            }
        }
    }
    

    希望对你有所帮助。

    【讨论】:

    • all 周围的双引号对我有用:hosts: '{{ target | default("all") }}'
    • 为什么'{{ target | default("all") }}' 有效?特别是,为什么| 符号有效?这是在哪里记录的?我有一个用例,我想将多个默认组设置为目标。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-20
    • 2017-03-05
    • 1970-01-01
    • 1970-01-01
    • 2015-02-03
    • 2018-09-21
    相关资源
    最近更新 更多