0

测试:ansible 2.2.0.0、2.2.1.0 & 2.1.4.0

我有一个清单脚本,它在运行时返回这个 json(例如为了最小化):

{
  "componentA-service_ci": {
    "hosts": [
      "host1.example.com",
      "host2.example.com"
    ],
    "vars": {
      "httpport": "8100",
      "nginxpool": "componentA_pool"
    }
  },
  "componentB-service_ci": {
    "hosts": [
      "host1.example.com",
      "host3.example.com"
    ],
    "vars": {
      "httpport": "9999",
      "nginxpool": "componentB_pool"
    }
  }
}

我正在编写的剧本是用于部署应用程序的。清单中的变量对于组是唯一的,即每个服务都有自己的 lb 池和 http 端口。一台主机上也可以有多个应用程序。这就是剧本喜欢的样子:

---
- hosts: all
  remote_user: deployment
  become: true
  become_method: sudo
  become_user: root
  gather_facts: yes
  serial: 1
  roles:
    - app-deploy

角色中的任务只是打印出变量 nginxpool 和 httpport。

我像这样运行剧本:

ansible-playbook deploy.yml -i inventory.py --limit componentA-service_ci

预期结果:

TASK [app-deploy : debug] ******************************************************
ok: [host1.example.com] => {
    "msg": "pool componentA_pool port 8100"
}

TASK [app-deploy : debug] ******************************************************
ok: [host2.example.com] => {
    "msg": "pool componentA_pool port 8100"
}

实际结果:

TASK [app-deploy : debug] ******************************************************
ok: [host1.example.com] => {
    "msg": "pool componentB_pool port 9999"
}

TASK [app-deploy : debug] ******************************************************
ok: [host2.example.com] => {
    "msg": "pool componentA_pool port 8100"
}

--limitansible的工作部署在为componentA-service_ci列出的主机上,但对于host1.example.com,我从componentB-service_ci vars获取nginxpool和httpport的值。我阅读了文档,但不明白这是怎么发生的?这是一个错误还是我只是不理解 ansible 在这里的工作原理?

4

1 回答 1

0

我认为重点是,ansible 首先构建完整的库存,然后搜索符合您限制的剧本。

由于主机 ( host1.example.com) 属于指定相同变量的两个组,因此在设置清单时信息会混淆。httpport变量的内容host1.example.com可以来自componentA-group 或来自componentB-group。

建立清单后,ansible 尝试将播放限制为包含指定限制组中的主机的播放

重命名变量,使变量名是唯一的,然后在 play 中使用特定组中的特定变量名:

{
  "componentA-service_ci": {
    "hosts": [
      "host1.example.com",
      "host2.example.com"
    ],
    "vars": {
      "componentA_httpport": "8100",
      "componentA_nginxpool": "componentA_pool"
    }
  },
  "componentB-service_ci": {
    "hosts": [
      "host1.example.com",
      "host3.example.com"
    ],
    "vars": {
      "componentB_httpport": "9999",
      "componentB_nginxpool": "componentB_pool"
    }
  }
}

现在主机host1.example.com有四个变量componentA_httpport,componentA_nginxpoolcomponentB_httpport, componentB_nginxpool

于 2017-01-19T13:05:25.697 回答