Vidya
Vidya

Reputation: 657

Ansible run role based on changed status

I am calling all the roles in order and now I have to add condition check before running other roles,

Current main.yml

- hosts: all
  gather_facts: no
  roles:
     - artifacts_copy
     - app_build
     - db_build
     - log_build

artifacts_copy: copies code

Is it possible to add a condition, if artifacts_copy role changed then run remaining roles otherwise just skip remaining roles

  something like this

  roles:
     - artifacts_copy
     when: artifacts_copy.chnaged=true # then run below roles
           - app_build
           - db_build
           - log_build

Upvotes: 1

Views: 553

Answers (1)

Vidya
Vidya

Reputation: 657

I just figured it out and it may help others so posting this answer

roles:

       - artifacts_copy
       - app_build
       - db_build
       - log_build

artifacts_copy: tasks/main.yml added set fact like this

---
# tasks file for artifacts
- name: copy artifacts
  copy:
     src: files
     dest: /root/mycode/
  register: artifacts_copy_status

- set_fact:
    artifacts_copy_status={{ artifacts_copy_status }}
       

then I called role like below

---
- hosts: all
  gather_facts: no
  roles:
     - artifacts_copy
     - role: app_build
       when: artifacts_copy_status.changed | bool ==  true
     - role: db_build
       when: artifacts_copy_status.changed | bool == true
     - role: log_build
       when: artifacts_copy_status.changed | bool == true

Upvotes: 3

Related Questions