简体   繁体   English

如何有条件地运行 Ansible 剧本?

[英]How to run Ansible playbook conditionally?

how do i run some playbooks based on condition.我如何根据条件运行一些剧本。 I will explain my current scenario - I have a main yml - main.yml which is importing 4 playbooks with conditions as below -我将解释我目前的情况 - 我有一个主要的 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

The 1st playbook current-deployment-status.yml gets the current release version deployed and register a variable current_release .第一个剧本current-deployment-status.yml获取部署的当前发布版本并注册一个变量current_release Likewise the 2nd playbook do some configuration checks and registers into a variable config_var .同样,第二个剧本做一些配置检查并注册到变量config_var中。

Now based on these 2 variables, I have to execute my further playbooks.现在基于这两个变量,我必须执行我进一步的剧本。 If target_release_version == current_release then I don't want to do the deployment so only execute full-deployment.yml when condition meets as above.如果target_release_version == current_release那么我不想进行部署,所以只有在满足上述条件时才执行full-deployment.yml

Likewise, if there is only configuration change required and no deployment: config_var.changed == true and target_release_version == current_release , then execute only-config-change.yml同样,如果只需要更改配置而无需部署: config_var.changed == true and target_release_version == current_release ,则执行only-config-change.yml

Is there a way to execute the playbook only when condition meets otherwise skip it and proceed further.有没有办法仅在条件满足时才执行剧本,否则跳过它并继续进行。

Please let me know if someone can guide me in the right direction请让我知道是否有人可以指导我正确的方向

Yes you can achieve it using set_facts ansible module.是的,您可以使用 set_facts ansible 模块来实现它。

main.yml主.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

current-deployment-status.yml当前部署状态.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"

current-config-status.yml当前配置状态.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}}"

full-deployment.yml完全部署.yml

- name : Playbook
  hosts: all 
  user: ubuntu 
  gather_facts: True

  tasks:
    - name: echo playbook name 
      shell: echo "full_deployment.yml"

only-config-change.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" 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" 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   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM