我收集了角色和运行这些角色的主机。但是我也有只需要其中一些角色的“自定义”主机,并且它改变了主机所需的角色。我该如何创建可针对特定主机组运行的剧本,并在每个角色之前询问是否应运行该角色?
到目前为止,我已经创建了剧本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
custom / tasks / 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" }
答案 0 :(得分:0)
查找此解决方案。我觉得你会喜欢它的。您可以将var保存在另一个文件中,然后先加载它们。
---
- 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