如何使用标签在ansible中的两个任务之间进行选择
how to chose between two tasks in ansible using tags
我想将标签用于 select 在安装或升级过程中的特定任务集:
所以场景假设:我有一个任务 A 和一个任务 B 我想使用标签 select A 或 B
这是我到目前为止所拥有的:在我的 task/main.yml 中的角色:
- name: "Configuration"
include: 03-configuration.yml
when: upgrade
tags:
- configuration
所以我像这样启动我的剧本,它引导我转到文件 03-configuration.yml 并播放该文件中的所有内容
ansible-playbook -v ThePlayBook.yml --tags "configuration"
03-configuration.yml 文件:
- name: "A"
- name: "B"
但是正如您在此文件 03-configuration.yml 中看到的那样,我有两个任务,我正在谈论 A 和 B,所以此时它会尝试执行这两个任务,而我想使用“安装”或“升级”之类的标签来启动其中一个。
您可以使用类似
的方法
---
- hosts: localhost
become: false
gather_facts: false
tasks:
- name: "Config"
include_tasks: install_update.yml
tags:
- config
- name: "A"
debug:
tags: install
- name: "B"
debug:
tags: update
通过
调用
ansible-playbook ThePlayBook.yml --tags="config"
ansible-playbook ThePlayBook.yml --tags="config,install"
ansible-playbook ThePlayBook.yml --tags="config,update"
导致输出
PLAY RECAP **********************
localhost : ok=1
...
TASK [A] ************************
ok: [localhost] =>
msg: Hello world!
PLAY RECAP **********************
localhost : ok=2
...
TASK [B] ************************
ok: [localhost] =>
msg: Hello world!
PLAY RECAP **********************
localhost : ok=2
进一步阅读
例如,给定文件
shell> cat configuration.yml
- debug:
msg: installation
tags: installation
- debug:
msg: upgrade
tags: upgrade
将其包含在剧本中
shell> cat playbook.yml
- hosts: localhost
tasks:
- include_tasks: configuration.yml
when: upgrade|default(false)|bool
tags: configuration
当你运行这个剧本没有任何标签
shell> ansible-playbook playbook.yml -e upgrade=true
所有任务执行完毕
TASK [debug] **************************************************************
ok: [localhost] =>
msg: installation
TASK [debug] **************************************************************
ok: [localhost] =>
msg: upgrade
当您 运行 仅使用 配置 标签时
shell> ansible-playbook playbook.yml -e upgrade=true -t configuration
包含文件但未执行任何任务
TASK [include_tasks] ********************************************************
included: /export/scratch/tmp8/configuration.yml for localhost
tags: configuration
未被包含文件中的任务继承。参见 Tag inheritance for includes ...。如果要执行这样的任务,如果在命令行上指定了标签,则需要这两个标签。例如
shell> ansible-playbook playbook.yml -e upgrade=true -t configuration,installation
给出删节
TASK [debug] **************************************************************
ok: [localhost] =>
msg: installation
在 Tags 中查看更多详细信息。
我想将标签用于 select 在安装或升级过程中的特定任务集: 所以场景假设:我有一个任务 A 和一个任务 B 我想使用标签 select A 或 B 这是我到目前为止所拥有的:在我的 task/main.yml 中的角色:
- name: "Configuration"
include: 03-configuration.yml
when: upgrade
tags:
- configuration
所以我像这样启动我的剧本,它引导我转到文件 03-configuration.yml 并播放该文件中的所有内容
ansible-playbook -v ThePlayBook.yml --tags "configuration"
03-configuration.yml 文件:
- name: "A"
- name: "B"
但是正如您在此文件 03-configuration.yml 中看到的那样,我有两个任务,我正在谈论 A 和 B,所以此时它会尝试执行这两个任务,而我想使用“安装”或“升级”之类的标签来启动其中一个。
您可以使用类似
的方法---
- hosts: localhost
become: false
gather_facts: false
tasks:
- name: "Config"
include_tasks: install_update.yml
tags:
- config
- name: "A"
debug:
tags: install
- name: "B"
debug:
tags: update
通过
调用ansible-playbook ThePlayBook.yml --tags="config"
ansible-playbook ThePlayBook.yml --tags="config,install"
ansible-playbook ThePlayBook.yml --tags="config,update"
导致输出
PLAY RECAP **********************
localhost : ok=1
...
TASK [A] ************************
ok: [localhost] =>
msg: Hello world!
PLAY RECAP **********************
localhost : ok=2
...
TASK [B] ************************
ok: [localhost] =>
msg: Hello world!
PLAY RECAP **********************
localhost : ok=2
进一步阅读
例如,给定文件
shell> cat configuration.yml
- debug:
msg: installation
tags: installation
- debug:
msg: upgrade
tags: upgrade
将其包含在剧本中
shell> cat playbook.yml
- hosts: localhost
tasks:
- include_tasks: configuration.yml
when: upgrade|default(false)|bool
tags: configuration
当你运行这个剧本没有任何标签
shell> ansible-playbook playbook.yml -e upgrade=true
所有任务执行完毕
TASK [debug] **************************************************************
ok: [localhost] =>
msg: installation
TASK [debug] **************************************************************
ok: [localhost] =>
msg: upgrade
当您 运行 仅使用 配置 标签时
shell> ansible-playbook playbook.yml -e upgrade=true -t configuration
包含文件但未执行任何任务
TASK [include_tasks] ********************************************************
included: /export/scratch/tmp8/configuration.yml for localhost
tags: configuration
未被包含文件中的任务继承。参见 Tag inheritance for includes ...。如果要执行这样的任务,如果在命令行上指定了标签,则需要这两个标签。例如
shell> ansible-playbook playbook.yml -e upgrade=true -t configuration,installation
给出删节
TASK [debug] **************************************************************
ok: [localhost] =>
msg: installation
在 Tags 中查看更多详细信息。