Ansible通常以主机为中心:如果您的主机中的某个主机变量在清单中的多个组中包含多个主机变量,则该主机的所有变量将被合并,而具有相同名称的变量将相互覆盖。示例:
# Inventory
[app_stages:children]
stage_development
stage_acceptance
[stage_development]
localhost db_unique_name=scdev oracle_version_db=12.1.0.2 oracle_edition_db=SE2
localhost db_unique_name=ecdev oracle_version_db=12.1.0.2 oracle_edition_db=EE
[stage_acceptance]
localhost db_unique_name=scacp oracle_version_db=18 oracle_edition_db=SE2
localhost db_unique_name=ecacp oracle_version_db=18 oracle_edition_db=EE
# Playbook
- name: test inventory
hosts: "{{ hostgroup }}"
tasks:
- name: show variables
debug:
msg: host {{ ansible_hostname }}, db_name = {{ db_unique_name }}, db_version = {{ oracle_version_db }}, db_edition = {{ oracle_edition_db }}
结果:
ansible-playbook playbook_1.yml -i inventory_1 -e hostgroup=stage_development
TASK [show variables] ******************************************************************************************************************
ok: [localhost] => {
"msg": "host server_1, db_name = ecacp, db_version = 18, db_edition = EE"
}
即使我要求ansible仅在stage_development上工作,它也会显示并显示另一组中设置的变量。
我需要一种不同的方法:首先,我想指定带有一些基本参数的服务(例如,数据库名称,版本等,请参见上文)。是否将其中一些或全部部署到同一主机是第二要务。这项功能可行,但不太优雅,因为有关服务的信息在剧本中,而不在清单中,后者稍后应由某些外部工具(CMS)生成:
# Inventory
[app_stages:children]
stage_development
stage_acceptance
[stage_development:children]
srv_scdev
srv_ecdev
[stage_acceptance:children]
srv_scacp
srv_ecacp
[srv_scdev]
localhost
[srv_ecdev]
localhost
[srv_scacp]
localhost
[srv_ecacp]
localhost
# Playbook
---
- name: stage development, setup database scdev
hosts: srv_scdev
vars:
db_unique_name: scdev
oracle_version_db: 12.1.0.2
oracle_edition_db: SE2
tasks:
- name: show variables
debug:
msg: host {{ ansible_hostname }}, db_name = {{ db_unique_name }}, db_version = {{ oracle_version_db }}, db_edition = {{ oracle_edition_db }}
- name: stage development, setup database ecdev
hosts: srv_ecdev
vars:
db_unique_name: ecdev
oracle_version_db: 12.1.0.2
oracle_edition_db: EE
tasks:
- name: show variables
debug:
msg: host {{ ansible_hostname }}, db_name = {{ db_unique_name }}, db_version = {{ oracle_version_db }}, db_edition = {{ oracle_edition_db }}
- name: stage acceptance, setup database scacp
hosts: srv_scacp
vars:
db_unique_name: scacp
oracle_version_db: 18
oracle_edition_db: SE2
tasks:
- name: show variables
debug:
msg: host {{ ansible_hostname }}, db_name = {{ db_unique_name }}, db_version = {{ oracle_version_db }}, db_edition = {{ oracle_edition_db }}
- name: stage acceptance, setup database ecacp
hosts: srv_ecacp
vars:
db_unique_name: ecacp
oracle_version_db: 18
oracle_edition_db: EE
tasks:
- name: show variables
debug:
msg: host {{ ansible_hostname }}, db_name = {{ db_unique_name }}, db_version = {{ oracle_version_db }}, db_edition = {{ oracle_edition_db }}
...
问题:如何正确/可重复使用/优雅?
答案 0 :(得分:1)
更改最少的解决方案是使用ansible_host
设置扩展您的库存,并为库存本身引入一个假名,例如
# Inventory
[app_stages:children]
stage_development
stage_acceptance
[stage_development]
dev1 ansible_host=localhost db_unique_name=scdev oracle_version_db=12.1.0.2 oracle_edition_db=SE2
dev2 ansible_host=localhost db_unique_name=ecdev oracle_version_db=12.1.0.2 oracle_edition_db=EE
[stage_acceptance]
stage1 ansible_host=localhost db_unique_name=scacp oracle_version_db=18 oracle_edition_db=SE2
stage2 ansible_host=localhost db_unique_name=ecacp oracle_version_db=18 oracle_edition_db=EE