2017-01-19 30 views
0

當測試:ansible 2.2.0.0,2.2.1.0 & 2.1.4.0ansible返回不正確的使用瓦爾動態庫存

我有一個庫存腳本返回運行時,此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 componentB_pool port 9999" 
} 

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

--limit作品該組件可以在組件A-service_ci中列出的主機上進行部署,對於host1.example.com組件則可以部署。我從componentB-service_ci vars中獲取nginxpool和httpport的值。我讀了documentation,但不明白這是怎麼發生的?這是一個錯誤還是我只是不理解這裏有多麼可行的作品?

+0

可能重複[在特定主機組上運行](http://stackoverflow.com/questions/41500463/run-ansible-on-specific-hosts-group) – techraf

回答

0

我認爲重點是,ansible先建立完整的庫存,然後搜索符合您的限制的劇本。

由於主機(host1.example.com)屬於指定相同變量的兩個組,因此在設置清單時會混淆信息。 host1.example.com變量的httpport的內容可以來自componentA組或從componentB組。

庫存建成後,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_httpportcomponentA_nginxpoolcomponentB_httpportcomponentB_nginxpool

+0

Thx爲洞察。我後來發現這個問題在github https://github.com/ansible/ansible/issues/9065 我真的認爲這是一個錯誤,使用不同的變量名稱是不會工作的,因爲它是一個通用的手冊,我結束了用一個包裝腳本在運行ansible-playbook之前進行查找並將變量注入extra-vars。 –