Ansible Comparison Membership Operators

Hello everyone are you working on Ansible Automations?!! 

This is post is for you to help, In this post you will find more interesting variable usages and their operations with different options such as comparison operators and membership operators.
  1. Comparison operators

  2. This is an example for Comparison operators
    ---
    # Filename: test_compare.yml
    
     - name: Comparison Operator example
       hosts: localhost
       gather_facts: false
       vars:
         a: "Vybhava"
         b: "Technologies"
         n: 100
         m: 500
       tasks:
       - debug:
           msg:
             - "This will compare numbers {{n}} and {{m}} "
             - " n == m: {{ n == m }}"
             - " n != m: {{ n != m }}"
             - " n > m: {{ n > m }}"
             - " n >= m: {{ n >= m }}"
             - " n <= m: {{ n <= m }}"
             - " n < m: {{ n < m }}"
             - "Compare Strings : a {{a}} and b {{b}}"
             - " a == b: {{ a == b }}"
             - " a != b: {{ a != b }}"
             - " a > b: {{ a > b }}"
             - " a >= b: {{ a >= b }}"
             - " a <= b: {{ a <= b }}"
             - " a < b: {{ a < b }}"
             - " not a == b: {{ not a == b }}"
    
     

    Testing
    ansible-playbook test_compare.yml
    Execution Image
    
    Ansible comparison operator examples
    Ansible boolean operators their execution outputs
  3. Membership operators

Learning new things make me more stronger!! Now we will see how to use the two more operators 'in' and 'not in', these are also called membership operators. which will operates on the lists in Python language. In the same fashion here in Ansible we can use the in and not in operators on list variable or facts.
---
 - name: membership operators
   hosts: localhost
   gather_facts: no
   vars:
     i: 5
     arrlist: [10, 5, 8,25]
     s: "web1"
     servers:
     - "app1"
     - "app2"
     - "web01"
     - "web1"
     f: 7.80
     my_seq: [9, 8, "Vybhava",7.80, 'app1']
   tasks:
   - name: Testing possible options with membership operator
     debug:
       msg:
         - "i = {{i}} and arrlist = {{ arrlist }}"
         - "check  i in arrlist : {{ i in arrlist }}"
         - "check  100 in arrlist : {{ 100 in arrlist }}"
         - "check  100 not in arrlist :  {{ 100 not in arrlist }}"
         - "check  i not in arrlist : {{ i not in arrlist }}"
         - "Testing membership with number list done!"
         - "s = {{s}} and servers={{servers}}"
         - "Check s in servers : {{ s in servers }}"
         - "Check s not in servers : {{ s not in servers }}"
         - "Check web2 in servers : {{ 'web2' in servers }}"
         - "Check app2 in servers : {{ 'app2' in servers }}"
         - "Testing membership with strings list done!!"
         - "f = {{f}} and my_seq = {{my_seq}}"
         - "Check 9 in my_seq : {{ 9 in my_seq }}"
         - "Check i not in my_seq : {{ i not in my_seq }}"
         - "Check 'app1' in my_seq : {{ 'app1' in my_seq }}"
         - "Check s not in my_seq : {{ s not in my_seq }}"
         - "Check f in my_seq : {{ f in my_seq }}"
         - "Testing membership with sequance done!!!"
You know how to run the this playbook
The execution output is as follows:

Ansible membership operator example
Ansible membership operators in, not in example


We will share it soon another new experiment soon.
 
Keep watching for the new Ansible learning updates...

Comments

Popular posts from this blog

Ansible 11 The uri module with examples

Jenkins Active choices parameter - Dynamic input

DevOps Weapons