ansible在使用动态库存时返回不正确的VAR
测试日期:ansible 2.2.0.0、2.2.1.0和2.1.4.0 我有一个清单脚本,在运行时返回这个json(例如,为了最小化): 我正在编写的剧本是用于部署应用程序的。清单中的VAR对于组是唯一的,即每个服务都有自己的lb池和http端口。此外,一台主机上可以有多个应用程序。下面是剧本的内容:ansible在使用动态库存时返回不正确的VAR,ansible,ansible-playbook,ansible-2.x,ansible-inventory,Ansible,Ansible Playbook,Ansible 2.x,Ansible Inventory,测试日期:ansible 2.2.0.0、2.2.1.0和2.1.4.0 我有一个清单脚本,在运行时返回这个json(例如,为了最小化): 我正在编写的剧本是用于部署应用程序的。清单中的VAR对于组是唯一的,即每个服务都有自己的lb池和http端口。此外,一台主机上可以有多个应用程序。下面是剧本的内容: --- - hosts: all remote_user: deployment become: true become_method: sudo become_user: ro
---
- 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"
}
实际结果:
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"
}
--limit
的工作原理是,ansible部署在为componentA-service\u ci列出的主机上,但对于host1.example.com,我从componentB-service\u ci vars获取nginxpool和httpport的值。我读了这本书,但不明白这是怎么发生的?这是一个bug还是我不明白ansible在这里是如何工作的 我认为关键是,ansible首先建立完整的库存,然后搜索符合您限制的剧本
由于主机(host1.example.com
)属于指定相同变量的两个组,因此在设置资源清册时,信息会混淆。host1.example.com
的httpport
变量的内容可以来自组件a
-组或组件b
-组
建立资源清册后,ansible尝试将播放限制为包含指定限制组中主机的播放
重命名变量,使变量名唯一,然后在播放中使用特定组中的特定变量名:
{
"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\u httport
,componentA\u nginxpool
和componentB\u httpport
,componentB\u nginxpool
insight的Thx可能重复。后来我在github上发现了这个问题,我真的认为这是一个bug,使用不同的变量名是行不通的,因为它是一个通用的playbook,在运行ansible playbook并将变量作为额外变量注入之前,我最终使用了一个包装器脚本进行查找。