ansible - 如何有条件地运行 Ansible playbook?
问题描述
我如何根据条件运行一些剧本。我将解释我目前的情况 - 我有一个主要的 yml -main.yml
它正在导入 4 个剧本,条件如下 -
---
- import_playbook: current-deployment-status.yml
- import_playbook: current-config-status.yml
- import_playbook: full-deployment.yml
when: target_release_version != current_release
- import_playbook: only-config-change.yml
#when: config_var.changed == true and target_release_version == current_release
第一个剧本current-deployment-status.yml
获取部署的当前版本并注册一个变量current_release
。同样,第二个剧本做一些配置检查并注册到一个变量config_var
中。
现在基于这两个变量,我必须执行我进一步的剧本。如果target_release_version == current_release
那时我不想进行部署,那么只有full-deployment.yml
在满足上述条件时才执行。
同样,如果只需要更改配置而无需部署 : config_var.changed == true and target_release_version == current_release
,则执行only-config-change.yml
有没有办法仅在条件满足时才执行剧本,否则跳过它并继续进行。
请让我知道是否有人可以指导我正确的方向
解决方案
是的,您可以使用 set_facts ansible 模块来实现它。
主要的.yml
---
- import_playbook: current-deployment-status.yml
- import_playbook: current-config-status.yml
- import_playbook: full-deployment.yml
when: target_release_version != current_release
- import_playbook: only-config-change.yml
when: config_var.changed == true and target_release_version == current_release
当前部署状态.yml
- name : Playbook current-deployment-status
hosts: all
user: ubuntu
gather_facts: True
tasks:
- name: echo playbook name
shell: echo "current-deployment-status.yml"
- set_fact:
current_release: "2"
当前配置状态.yml
- name : Playbook current-config-status.yml
hosts: all
user: ubuntu
gather_facts: True
tasks:
- name: echo playbook name
shell: echo "current-config-status.yml"
- name: task2
shell: echo "another task"
register: config_var
- debug: msg="{{config_var.changed}}"
完全部署.yml
- name : Playbook
hosts: all
user: ubuntu
gather_facts: True
tasks:
- name: echo playbook name
shell: echo "full_deployment.yml"
仅配置更改.yml
- name : Playbook only-config-change.yml
hosts: all
user: ubuntu
gather_facts: True
tasks:
- name: echo playbook name
shell: echo "only-config-change.yml"
ansible-playbook -i 172.31.6.248, main.yml -v --extra-vars "target_release_version=2"
ubuntu@ip-172-31-38-43:~/ansible_test$ ansible-playbook -i 172.31.6.248, main.yml -v --extra-vars "target_release_version=2"
PLAY [Playbook current-deployment-status] ***********************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************
ok: [172.31.6.248]
TASK [echo playbook name] ***************************************************************************************************************
changed: [172.31.6.248] => {"changed": true, "cmd": "echo \"current-deployment-status.yml\"", "delta": "0:00:00.002429", "end": "2020-05-31 16:46:29.900240", "rc": 0, "start": "2020-05-31 16:46:29.897811", "stderr": "", "stderr_lines": [], "stdout": "current-deployment-status.yml", "stdout_lines": ["current-deployment-status.yml"]}
TASK [set_fact] *************************************************************************************************************************
ok: [172.31.6.248] => {"ansible_facts": {"current_release": "2"}, "changed": false}
PLAY [Playbook current-config-status.yml] ***********************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************
ok: [172.31.6.248]
TASK [echo playbook name] ***************************************************************************************************************
changed: [172.31.6.248] => {"changed": true, "cmd": "echo \"current-config-status.yml\"", "delta": "0:00:00.002484", "end": "2020-05-31 16:46:30.776486", "rc": 0, "start": "2020-05-31 16:46:30.774002", "stderr": "", "stderr_lines": [], "stdout": "current-config-status.yml", "stdout_lines": ["current-config-status.yml"]}
TASK [task2] ****************************************************************************************************************************
changed: [172.31.6.248] => {"changed": true, "cmd": "echo \"another task\"", "delta": "0:00:00.002473", "end": "2020-05-31 16:46:31.048677", "rc": 0, "start": "2020-05-31 16:46:31.046204", "stderr": "", "stderr_lines": [], "stdout": "another task", "stdout_lines": ["another task"]}
TASK [debug] ****************************************************************************************************************************
ok: [172.31.6.248] => {
"msg": true
}
PLAY [Playbook full_deployment.yml] *****************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************
skipping: [172.31.6.248] => {"changed": false, "skip_reason": "Conditional result was False"}
TASK [echo playbook name] ***************************************************************************************************************
skipping: [172.31.6.248] => {"changed": false, "skip_reason": "Conditional result was False"}
PLAY [Playbook only-config-change.yml] **************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************
ok: [172.31.6.248]
TASK [echo playbook name] ***************************************************************************************************************
changed: [172.31.6.248] => {"changed": true, "cmd": "echo \"only-config-change.yml\"", "delta": "0:00:00.002585", "end": "2020-05-31 16:46:32.017156", "rc": 0, "start": "2020-05-31 16:46:32.014571", "stderr": "", "stderr_lines": [], "stdout": "only-config-change.yml", "stdout_lines": ["only-config-change.yml"]}
PLAY RECAP ******************************************************************************************************************************
172.31.6.248 : ok=9 changed=4 unreachable=0 failed=0
ansible-playbook -i 172.31.6.248, main.yml -v --extra-vars "target_release_version=3"
PLAY [Playbook current-deployment-status] ***********************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************
ok: [172.31.6.248]
TASK [echo playbook name] ***************************************************************************************************************
changed: [172.31.6.248] => {"changed": true, "cmd": "echo \"current-deployment-status.yml\"", "delta": "0:00:00.002490", "end": "2020-05-31 16:48:28.611482", "rc": 0, "start": "2020-05-31 16:48:28.608992", "stderr": "", "stderr_lines": [], "stdout": "current-deployment-status.yml", "stdout_lines": ["current-deployment-status.yml"]}
TASK [set_fact] *************************************************************************************************************************
ok: [172.31.6.248] => {"ansible_facts": {"current_release": "2"}, "changed": false}
PLAY [Playbook current-config-status.yml] ***********************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************
ok: [172.31.6.248]
TASK [echo playbook name] ***************************************************************************************************************
changed: [172.31.6.248] => {"changed": true, "cmd": "echo \"current-config-status.yml\"", "delta": "0:00:00.002565", "end": "2020-05-31 16:48:29.440319", "rc": 0, "start": "2020-05-31 16:48:29.437754", "stderr": "", "stderr_lines": [], "stdout": "current-config-status.yml", "stdout_lines": ["current-config-status.yml"]}
TASK [task2] ****************************************************************************************************************************
changed: [172.31.6.248] => {"changed": true, "cmd": "echo \"another task\"", "delta": "0:00:00.002459", "end": "2020-05-31 16:48:29.703006", "rc": 0, "start": "2020-05-31 16:48:29.700547", "stderr": "", "stderr_lines": [], "stdout": "another task", "stdout_lines": ["another task"]}
TASK [debug] ****************************************************************************************************************************
ok: [172.31.6.248] => {
"msg": true
}
PLAY [Playbook full_deployment.yml] *****************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************
ok: [172.31.6.248]
TASK [echo playbook name] ***************************************************************************************************************
changed: [172.31.6.248] => {"changed": true, "cmd": "echo \"full_deployment.yml\"", "delta": "0:00:00.002509", "end": "2020-05-31 16:48:30.610648", "rc": 0, "start": "2020-05-31 16:48:30.608139", "stderr": "", "stderr_lines": [], "stdout": "full_deployment.yml", "stdout_lines": ["full_deployment.yml"]}
PLAY [Playbook only-config-change.yml] **************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************
skipping: [172.31.6.248] => {"changed": false, "skip_reason": "Conditional result was False"}
TASK [echo playbook name] ***************************************************************************************************************
skipping: [172.31.6.248] => {"changed": false, "skip_reason": "Conditional result was False"}
PLAY RECAP ******************************************************************************************************************************
172.31.6.248 : ok=9 changed=4 unreachable=0 failed=0
推荐阅读
- ssl - 如何使用 node-opcua 客户端信任自签名服务器证书
- swift - Xcode: Your target contains an unsupported Swift Version
- matlab - 子图大小不同
- node.js - Passing data from opened window
- linux - 未能插入自定义内核模块
- c# - 如何以编程方式创建 X509 存储密钥以用于加密?
- python - 将列表中附加的帧写入 OpenCV 中的视频
- sapui5 - 页面放置在 SAPUI5 中的页面内时出现滚动问题
- javascript - 如何创建书签来调用包含一些内容的新弹出窗口?
- awk - 如何使 sed 命令处理非用户键入的输入参数中的转义字符?