我有角色和主机的集合,这些角色是针对这些角色运行的。但我也有“自定义”主机,只需要其中一些角色,并且它会因主机需要的角色而异。我怎样才能创建一个针对特定主机组运行的剧本,并在每个角色之前询问是否应该运行该角色?
到目前为止,我创建了playbook custom.yml(这不起作用......):
- name: Select roles
hosts: custom
become: yes
roles:
- role: custom
- { role: common, when: run_common == "Y" or run_common == "y" }
- { role: system, when: run_system == "Y" or run_system == "y" }
- { role: date, when: run_date == "Y" or run_date == "y" }
- { role: swap, when: run_swap == "Y" or run_swap == "y" }
- { role: users, when: run_users == "Y" or run_users == "y" }
我创建了一个角色来提示是否运行特定角色 - custom / tasks / main.yml:
- include_tasks: select_roles.yml
with_items:
- common
- system
- date
- swap
- users
定制/任务/ select_roles.yml:
- include_vars:
vars_prompt:
- name: "run_{{ item }}"
prompt: "Run {{ item }}?"
default: "N"
tasks:
- set_fact:
run_{{ item }}: "run_{{ item }}"
when: "run_{{ item }}" == "Y"
目前我让它在一个单一的剧本中运行,但它有很多复制粘贴和编辑,看起来很难看:
- name: Run select roles on all custom nodes
hosts: custom
become: yes
vars_prompt:
- name: "run_common"
prompt: "Run common?"
default: "N"
private: no
- name: "run_system"
prompt: "Run system?"
default: "N"
private: no
- name: "run_date"
prompt: "Run date?"
default: "N"
private: no
- name: "run_swap"
prompt: "Run swap?"
default: "N"
private: no
- name: "run_users"
prompt: "Run users?"
default: "N"
private: no
roles:
- { role: common, when: run_common == "Y" or run_common == "y" }
- { role: system, when: run_system == "Y" or run_system == "y" }
- { role: date, when: run_date == "Y" or run_date == "y" }
- { role: swap, when: run_swap == "Y" or run_swap == "y" }
- { role: users, when: run_users == "Y" or run_users == "y" }
看这个解决方案。我觉得你会喜欢它。您可以将您的变量放在另一个文件中并先加载它们。
---
- name: Run Specific Role
hosts: localhost
connection: local
gather_facts: false
vars:
mymenu:
- common
- system
- date
- swap
- users
allowed_items:
- "1"
- "2"
- "3"
- "4"
- "5"
vars_prompt:
- name: runmenu
prompt: "Run\n1 - {{ mymenu[0] }}\n2 - {{ mymenu[1] }}\n3 - {{ mymenu[2] }}\n4 - {{ mymenu[3] }}\n5 - {{ mymenu[4] }}\n[Enter Values with Comma as Separator]"
private: false
pre_tasks:
- name: Set Fact
set_fact:
menu{{ item }}: "menu{{ item }}"
with_items: "{{ runmenu.split(',') }}"
when: item in allowed_items
roles:
- role: common
when: menu1 is defined