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