Ansible Handlers, Conditions, and Loops

  • A handler is exactly the same as a task, but it will run when called by another task

OR

  • Handlers are just like regular tasks in an ansible playbook but are only run if the task contains a notify directive and also indicates that it changed something.
# Handlers example

---  
- hosts: localhost
  user: ansible
  become: yes
  connection: ssh
  gather_facts: no
  tasks:
   - name: Install httpd service
     yum:
      name: httpd
      state: present
      notify: restart httpd  # Same 'restart httpd; should be name of handler 
  handlers:                
   - name: restart httpd   # handler name and notify must be same
     service:
      name: httpd
      state: restarted

Playbook dry-run

  • Check whether the playbook is formatted correctly or not.

  • It will not execute the playbook only check for any error or not

  • Add --check flag after playbook name during running playbook

  • ansible-playbook <playbook-name> --check

Ansible Conditions

  • Whenever we have different scenarios we put a condition according to the scenario.

  • Using when statement we apply conditions in the playbook

  • Conditionals are useful when we want to skip a particular command in a particular node

# Ansible conditional playbook

---
- hosts: localhost
  user: ansible
  become: yes
  connection: ssh
  gather_facts: no
  tasks:
    - name: Install apache on Debian
      command: apt-get -y install apache2
      when: ansible_os_family == "Debian"
    - name: Install apache on RedHat
      command: yum -y install httpd
      when: ansible_os_family == "RedHat"

Ansible conditions based on register variables

To create a conditional based on a register variable

  • Register the outcome of the earlier task as a variable

  • Create a conditional test based on the register variable

# Ansible conditional based on register variable

---
- hosts: all
  user: ansible
  become: yes
  connection: ssh
  gather_facts: no
  tasks:
    - name: Install apache
      apt:
        name: apache2
        state: latest
        ignore_error: yes
        register: results
    - name: Install httpd
      yum:  
        name: httpd
        state: latest
        failed_when: "'FAILED' in results" # If 'FAILED' is present in results httpd package will not install

Ansible Loops

  • Sometimes we want to repeat a task multiple times then we use loops. Common Ansible loops include changing ownership on several files and/or directories with the file module, creating multiple users with the user module, and repeating a polling step until a certain result is reached

  • This playbook will create 3 users in all node

  • User details present in - /etc/passwd file

# This playbook will create 3 users

---
- hosts: all
  user: ansible
  become: yes
  connection: ssh
  gather_facts: no
  tasks:
    - name: Add a list of user
      user:
        name: '{{item}}'
        state: present
        with_items:
          - John
          - CJ
          - Mike

Ansible loops with_file

# Playbook to show the content of the file using loops 

- hosts: all
  user: ansible
  become: yes
  connection: ssh
  gather_facts: no
  tasks:
    - name: Show file content
      debug:
        msg: {{item}}
        with_file:
          - demo_file1.txt
          - demo_file2.txt

Ansible loops - with_sequence

# Playbook to generate a sequence between 2 number using loops 

- hosts: all
  user: ansible
  become: yes
  connection: ssh
  gather_facts: no
  tasks:
    - name: Generate sequence between two numbers
      debug:
        msg: {{item}}
        with_sequence:
          start: 1
          end: 6

I hope you enjoyed this article, if you have any questions, comments, or feedback or if I made a mistake feel free to comment down below.


Did you find this article valuable?

Support DevOps With CJ by becoming a sponsor. Any amount is appreciated!