Ansible 包含基于条件的任务文件
Ansible include task files based on conditional
刚刚进入 ansible
并想知道您是否可以根据 --extra-vars
中传递的变量将文件包含在任务 main.yml
中?
示例;
main.yml
-- 来自 role/tasks
# Upload latest json to s3
- include: upload-code.yml
playbook.yml
-- 来自剧本
- name: Launch cloudformation stack
hosts: 127.0.0.1
connection: local
roles:
- launch.cloudformation
如您所见,playbook.yml
包含 role
,main.yml
包含 upload-code.yml
如何根据如下条件
添加 upload-code.yml
ansible-playbook playbook.yml --extra-vars "include=upload"
您可以通过对包含任务应用 when
条件来执行此操作。
- include: upload-code.yml
when:
- include is defined
- include == "upload"
或者,如果您有多个包含文件,您也可以使用 include
作为名称的一部分。
- include: "{{ include }}-code.yml"
when: include is defined
刚刚进入 ansible
并想知道您是否可以根据 --extra-vars
中传递的变量将文件包含在任务 main.yml
中?
示例;
main.yml
-- 来自 role/tasks
# Upload latest json to s3
- include: upload-code.yml
playbook.yml
-- 来自剧本
- name: Launch cloudformation stack
hosts: 127.0.0.1
connection: local
roles:
- launch.cloudformation
如您所见,playbook.yml
包含 role
,main.yml
包含 upload-code.yml
如何根据如下条件
添加upload-code.yml
ansible-playbook playbook.yml --extra-vars "include=upload"
您可以通过对包含任务应用 when
条件来执行此操作。
- include: upload-code.yml
when:
- include is defined
- include == "upload"
或者,如果您有多个包含文件,您也可以使用 include
作为名称的一部分。
- include: "{{ include }}-code.yml"
when: include is defined