是否可以根据映射值为 Ansible 角色指定主机?

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)

Vla*_*tka 3

还有更多选择:

\n
    \n
  1. 将逻辑代入hosts的表达式中:
  2. \n
\n
shell> 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

给你想要的

\n
shell> 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)\n
shell> 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)\n
\n
    \n
  1. 创建子项(组别名)
  2. \n
\n
shell> 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

然后,剧本给出了相同的结果

\n
shell> 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

如果您无法更改库存文件,请将别名放入单独的文件中。例如,

\n
shell> 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)\n
shell> cat inventory/01-hosts \n[STG]\nnode1\n\n[LIVE]\napp_node\ndb_node\ngateway_node\n
Run Code Online (Sandbox Code Playgroud)\n
shell> cat inventory/02-aliases \n[single:children]\nSTG\n\n[distributed:children]\nLIVE\n
Run Code Online (Sandbox Code Playgroud)\n

然后,剧本给出了相同的结果

\n
shell> 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
\n
    \n
  1. 使用库存插件构建。看
  2. \n
\n
shell> ansible-doc -t inventory constructed\n
Run Code Online (Sandbox Code Playgroud)\n

例如,库存

\n
shell> 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)\n
shell> 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)\n
shell> 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

给出相同的结果

\n
shell> 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
\n \n

如果您坚持使用角色测试用例,请创建一个角色

\n
shell> 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

并在剧本中将其与您喜欢的任何标签一起使用

\n
shell> cat pb.yml\n- hosts: map_group\n  roles:\n    - role: setup\n
Run Code Online (Sandbox Code Playgroud)\n
\n