Sol*_*osa 5 defaults variable ansible
我有主机,我可以在其中在单节点架构或分布式架构上设置应用程序。
所以我有一个库存。
[STG]
node1
[LIVE]
app_node
db_node
gateway_node
Run Code Online (Sandbox Code Playgroud)
因此,默认值是的变量single
可以在 CLI 上更改为distributed
。
我有一个角色定义
- hosts:
gather_facts: no
roles:
- {role: setup, tags: ['setup', 'orchestra']}
Run Code Online (Sandbox Code Playgroud)
所以我希望主机线根据地图值是动态的
- hosts: 'if single then host == STG else LIVE'
Run Code Online (Sandbox Code Playgroud)
还有更多选择:
\nshell> cat pb.yml\n- hosts: "{{ (map_value == \'single\')|ternary(\'STG\', \'LIVE\') }}"\n tasks:\n - debug:\n var: ansible_play_hosts\n run_once: true\n
Run Code Online (Sandbox Code Playgroud)\n给你想要的
\nshell> ansible-playbook pb.yml -e map_value=single\n\nPLAY [single] ********************************************************************************\n\nTASK [debug] *********************************************************************************\nok: [node1] => \n ansible_play_hosts:\n - node1\n\nPLAY RECAP ***********************************************************************************\nnode1: ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 \n
Run Code Online (Sandbox Code Playgroud)\nshell> ansible-playbook pb.yml -e map_value=distributed\n\nPLAY [distributed] ***************************************************************************\n\nTASK [debug] *********************************************************************************\nok: [app_node] => \n ansible_play_hosts:\n - app_node\n - db_node\n - gateway_node\n\nPLAY RECAP ***********************************************************************************\napp_node: ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0\n\n
Run Code Online (Sandbox Code Playgroud)\nshell> cat hosts \n[STG]\nnode1\n\n[single:children]\nSTG\n\n[LIVE]\napp_node\ndb_node\ngateway_node\n\n[distributed:children]\nLIVE\n
Run Code Online (Sandbox Code Playgroud)\n然后,剧本给出了相同的结果
\nshell> cat pb.yml\n- hosts: "{{ map_value }}"\n tasks:\n - debug:\n var: ansible_play_hosts\n run_once: true\n
Run Code Online (Sandbox Code Playgroud)\n如果您无法更改库存文件,请将别名放入单独的文件中。例如,
\nshell> tree inventory/\ninventory/\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 01-hosts\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 02-aliases\n
Run Code Online (Sandbox Code Playgroud)\nshell> cat inventory/01-hosts \n[STG]\nnode1\n\n[LIVE]\napp_node\ndb_node\ngateway_node\n
Run Code Online (Sandbox Code Playgroud)\nshell> cat inventory/02-aliases \n[single:children]\nSTG\n\n[distributed:children]\nLIVE\n
Run Code Online (Sandbox Code Playgroud)\n然后,剧本给出了相同的结果
\nshell> ansible-playbook -i inventory pb.yml -e map_value=single\n...\nshell> ansible-playbook -i inventory pb.yml -e map_value=distributed\n...\n
Run Code Online (Sandbox Code Playgroud)\nshell> ansible-doc -t inventory constructed\n
Run Code Online (Sandbox Code Playgroud)\n例如,库存
\nshell> tree inventory\ninventory\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 01-hosts\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 02-constructed.yml\n
Run Code Online (Sandbox Code Playgroud)\nshell> cat inventory/01-hosts \n[STG]\nnode1\n\n[STG:vars]\nmap_group_value=single\n\n[LIVE]\napp_node\ndb_node\ngateway_node\n\n[LIVE:vars]\nmap_group_value=distributed\n
Run Code Online (Sandbox Code Playgroud)\nshell> cat inventory/02-constructed.yml \nplugin: constructed\nuse_extra_vars: true\ncompose:\n map_group: map_value\ngroups:\n map_group: map_group == map_group_value\n
Run Code Online (Sandbox Code Playgroud)\n然后,剧本
\n- hosts: map_group\n tasks:\n - debug:\n var: ansible_play_hosts\n run_once: true\n
Run Code Online (Sandbox Code Playgroud)\n给出相同的结果
\nshell> ansible-playbook -i inventory pb.yml -e map_value=single\n...\nshell> ansible-playbook -i inventory pb.yml -e map_value=distributed\n...\n
Run Code Online (Sandbox Code Playgroud)\n如果您坚持使用角色测试用例,请创建一个角色
\nshell> cat roles/setup/tasks/main.yml \n- debug:\n var: ansible_play_hosts\n run_once: true\n
Run Code Online (Sandbox Code Playgroud)\n并在剧本中将其与您喜欢的任何标签一起使用
\nshell> cat pb.yml\n- hosts: map_group\n roles:\n - role: setup\n
Run Code Online (Sandbox Code Playgroud)\n \n