如何有条件地运行Ansible playbook?
如何根据条件运行一些剧本。我将解释我目前的情况- 我有一个main yml-main.yml,它正在导入4个剧本,条件如下-如何有条件地运行Ansible playbook?,ansible,Ansible,如何根据条件运行一些剧本。我将解释我目前的情况- 我有一个main 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_releas
---
- 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
第一个playbook current-deployment-status.yml获取部署的当前版本,并注册一个变量current\u release。
同样,第二个剧本也会进行一些配置检查,并将其注册到变量config_var中
现在,基于这两个变量,我必须执行进一步的剧本。如果target_release_version==current_release,则我不想进行部署,因此仅在满足上述条件时执行full-deployment.yml
同样,如果只需要更改配置而不需要部署:config_var.changed==true和target_release_version==current_release,则只执行-config-change.yml
是否有办法仅在条件满足时执行剧本,否则跳过剧本并继续
请告诉我是否有人能为我指引正确的方向是的,您可以使用set_facts ansible模块实现 main.yml current-deployment-status.yml current-config-status.yml full-deployment.yml only-config-change.yml ansible playbook-i 172.31.6.248,main.yml-v-额外vars目标版本=2 ansible playbook-i 172.31.6.248,main.yml-v-extra vars target\u release\u version=3
这是不可能的。导入剧本不是一项任务。是的。我投票决定关闭它。这能回答你的问题吗@弗拉基米尔博特卡感谢弗拉德的回应。我查看了链接,但这并不能解决我的问题。你能不能建议一些其他的方法让我达到这个目的。基本上,在我的场景中可能有两种情况,一种是完全部署,包括代码+配置,另一种是配置。在just-config的情况下,我只想重新启动tomcat并退出播放,而不做其他事情。在完全部署的情况下,我的plaobook会做很多事情,比如下载并复制版本到远程主机,创建新的符号链接,停止/启动tomcat等。感谢您的响应,但在您的输出中,我可以看到剧本被跳过了播放[playbook full_deployment.yml]***************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************_原因:条件结果为假}但在我的剧本中我找不到类似的东西。虽然任务没有被执行,但是与剧本相关的所有内容都被打印出来了,正如Vladimir提到的,我们不能在导入剧本中列出条件,因为它不是任务,所以不能确定when语句是否有效,这是when命令的预期行为。即使任务被跳过,它也将作为跳过的任务打印在输出中。我不明白你还需要什么。
---
- 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
- 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"
- 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}}"
- name : Playbook
hosts: all
user: ubuntu
gather_facts: True
tasks:
- name: echo playbook name
shell: echo "full_deployment.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"
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
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