首页 > 解决方案 > Ansible:用于暂存/生产主机的 YAML 清单

问题描述

我可以t setup inventory which will be easy and useful in 为 Ansible Playbook 使用 yml` 格式 :(

例如:

all:
  children:
    db:
      children:
        production:
          hosts:
            1.1.1.1:

        staging:
          hosts:
            11.11.11.11:

    web:
      children:
        production:
          hosts:
            2.2.2.2:

        staging:
          hosts:
            22.22.22.22:

所以,我有两本剧本:

剧本-db.yml

...
hosts:
  - db

playbook-web.yml

...
hosts:
  - db

我想像这样使用这个库存:

andible-playbook -D playbook-db.yml --limit=staging

我希望我的剧本将仅用于dbstaging主机,但剧本适用于所有staging主机:11.11.11.11并且22.22.22.22(但我期待仅 11.11.11.11):(

我怎样才能正确认识它?

标签: ansible

解决方案


看来您对库存文件有误解,因为 ansible 正在按照您的描述进行操作

ansible-inventory -i your-posted-file.yml --list

发出

{   ...
    "all": {
        "children": [
            "db",
            "ungrouped",
            "web"
        ]
    },
    "db": {
        "children": [
            "production",
            "staging"
        ]
    },
    "production": {
        "hosts": [
            "1.1.1.1",
            "2.2.2.2"
        ]
    },
    "staging": {
        "hosts": [
            "11.11.11.11",
            "22.22.22.22"
        ]
    }

显示该组具有and的db所有成员,但正如您所描述的那样具有主机productionstagingstaging"11.11.11.11", "22.22.22.22"

我想也许您将 yaml 缩进与成员资格混为一谈,但这不是 ansible 库存的工作方式。

更有可能的是,您想要一个db-staginganddb-production组,对于您描述的情况,您只需要db主机staging,让db组表示每个 db成员


推荐阅读