Ansible Playbook when statement added in playbook

159 views
Skip to first unread message

DEEPANSHU DANG

unread,
Nov 1, 2022, 9:01:35 AM11/1/22
to Ansible Project
Hi Team
I have created a Playbook for helm deployment and need to make resiliency and exception handling with the help of when condition I got confused in that when adding multiple conditions can any one please help.
```
---
#Header Section
- name: This playbook will deploy the kafka service in Cluster
  hosts: host
  gather_facts: true
  vars:
     helm_chart_url: "https://charts.bitnami.com/bitnami"
     pv_file: ~/yamls/kafka/persistent_volume_kafka.yml
     release_name: kafka
     namespace: default
     values_files: ~/yamls/kafka/
     helm_chart_name: bitnami    
#Task Section

  tasks:
  - name: Checking bitnami repo
    ansible.builtin.shell: helm repo list | grep -i "bitnami" | awk '{print $1}'
    register: repo_result
    failed_when: # repo already exit if not exsit next task perform to add it
    ignore_errors: yes

  - name: Add helm bitnami/kafka repo
    kubernetes.core.helm_repository:
     name: bitnami
     repo_state: present
     repo_url: "{{ helm_chart_url }}"
    when: # If first task failed this task will add repo
     
  - name: Updating helm bitnami/kafka repo
    ansible.builtin.command: helm repo update {{helm_chart_name}}
    register: result_repo
  - name: Output of updating helm repo
    debug:
      var: result_repo.stdout

 - name: Create a persistence Volume by reading the definition from a local file
    kubernetes.core.k8s:
     state: present
     src: "{{pv_file}}"
     namespace: "{{namespace}}"
     wait: yes

  - name: listing helm {{release_name}} release
    shell: helm ls | grep -i {{release_name}} | awk '{print $1}'
    ignore_errors: true
    register: result
    failed_when: '"release_name" not in result.stdout' or # if already release not available will install it if available skip the task
  - debug:
       var: result.stdout
     
  - name: Deploy Kafka chart using values files
    command: helm install {{release_name}} -f values.yaml bitnami/kafka
    args:
      chdir: "{{values_files}}"
    when: '"{{release_name}}" not in result.stdout'  # This task perform if pervisious task not found any release
    register: command_output
 
  - name: Output of the Helm kafka release
    debug:
      var: command_output.stdout_lines
    when: #This task will run when pervisious task execute well

    # and the output is coming in list if we can filter the release
   
    #command_output.stdout_lines[0]["NAME"]=="kafka"
   
  - debug:
      var: command_output.stdout_lines[0]

  - name: Checking helm {{release_name}} release
    shell: helm ls | grep -i {{release_name}} | awk '{print $1}'
    register: result

  - debug:
      msg: "In Helm '{{result.stdout}}' release available"
    when: 'result.stdout != "release_name"'

------------

I have mention comments where we need to add the when conditions

task1: checking bitnami chart is added or not if not added task will failed Task2: will based on the task one if failed this task will add the repo or skip it task3: updating the help repo task4: creating persistence volume task5 list helm chart if release available task skip task6: will deploy if prevision task could not find release

Reply all
Reply to author
Forward
0 new messages