Operators in Ansible
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed variables in Ansible.
https://cloudaffaire.com/variables-in-ansible/
In this blog post, we will discuss operators in Ansible.
What is operator?
Programming languages typically support a set of operators: constructs which behave generally like functions, but which differ syntactically or semantically from usual functions. Common simple examples include arithmetic (addition with +), comparison (with >), and logical operations (such as AND or &&). More involved examples include assignment (usually = or :=), field access in a record or object (usually .), and the scope resolution operator (often ::). Languages usually define a set of built-in operators, and in some cases allow users to add new meanings to existing operators or even define completely new operators.
As already referenced in the variables section, Ansible uses Jinja2 templating to enable dynamic expressions and access to variables. Ansible supports all the operators provided by Jinja2 like math, comparison, logical and other special Jinja2 operators.
Next, we are going to explain each operator type with a demo.
Math Operator: Math operators are used to perform mathematical operations like addition, multiplication etc.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
##-------- ## Math ## ##-------- # + : Adds two objects together. {{ 3 + 2 }} is 5. for strings, + can be used for concatination. # - : Subtract the second number from the first one. {{ 3 - 2 }} is 1. # / : Divide two numbers. The return value will be a floating point number. {{ 1 / 2 }} is {{ 0.5 }}. # // : Divide two numbers and return the truncated integer result. {{ 20 // 7 }} is 2. # % : Calculate the remainder of an integer division. {{ 11 % 7 }} is 4. # * : Multiply the left operand with the right one. {{ 2 * 2 }} would return 4. # This can also be used to repeat a string multiple times. {{ '=' * 80 }} would print a bar of 80 equal signs. # ** : Raise the left operand to the power of the right operand. {{ 2**3 }} would return 8. ## Create playbook vi myplaybook.yml ---------------------- - hosts: localhost gather_facts: false tasks: - name: Math Operators debug: msg: - "+ : {{ 7 + 2 }}" - "- : {{ 7 - 2 }}" - "/ : {{ 7 / 2 }}" - "//: {{ 7 // 2 }}" - "% : {{ 7 % 2 }}" - "* : {{ 7 * 2 }}" - "* : {{ 'Ansible' * 2 }}" - "**: {{ 7 ** 2 }}" ---------------------- :wq ## Execute the playbook ansible-playbook myplaybook.yml |
Comparison Operators: Comparison operators are used to compare between two values.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
##--------------- ## Comparisons ## ##--------------- # == : Compares two objects for equality. # != : Compares two objects for inequality. # > : true if the left hand side is greater than the right hand side. # >= : true if the left hand side is greater or equal to the right hand side. # < : true if the left hand side is lower than the right hand side. # <= : true if the left hand side is lower or equal to the right hand side. ## Create playbook vi myplaybook.yml ---------------------- - hosts: localhost gather_facts: false vars: a: 12 b: 3 c: 'Ansible' tasks: - name: Comparision Operators debug: msg: - "== : {{ a if (c == 'Ansible') else b }}" - "!= : {{ a if (c != 'Docker') else b }}" - "> : {{ a if (a > b) else b }}" - ">=: {{ a if (a >= b) else b }}" - "< : {{ b if (a < b) else a }}" - "<= : {{ b if (a <= b) else a }}" ----------------------- :wq ## Execute the playbook ansible-playbook myplaybook.yml |
Logical Operators: Logical operators are used to perform logical operations like AND, OR etc.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
##--------- ## Logic ## ##--------- # and : Return true if the left and the right operand are true. # or : Return true if the left or the right operand are true. # not : negate a statement. # (expr): group an expression. ## Create playbook vi myplaybook.yml ---------------------- - hosts: localhost gather_facts: false tasks: - name: Logical Operators vars: a: 12 b: 3 c: 5 debug: msg: "{{ a }} is > than {{ b }} and {{ a }} is > than {{ c }}" when: a > b and a > c ----------------------- :wq ## Execute the playbook ansible-playbook myplaybook.yml |
Special Operators: Jinja2 provides a set of special operators like filter to perform special operations.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
##------------------- ## Other Operators ## ##------------------- # in : Perform a sequence / mapping containment test. Returns true if the left operand is contained in the right. # {{ 1 in [1, 2, 3] }} would, for example, return true. # is : Performs a test. # | : Applies a filter. # ~ : Converts all operands into strings and concatenates them. # {{ "Hello " ~ name ~ "!" }} would return (assuming name is set to 'John') Hello John!. # () : Call a callable: {{ post.render() }}. Inside of the parentheses you can use positional arguments and # keyword arguments like in Python: {{ post.render(user, full=true) }}. # . / []: Get an attribute of an object. ## Create playbook vi myplaybook.yml ---------------------- - hosts: localhost gather_facts: false tasks: - name: Other Operators vars: a: ['bob','deb','alex'] b: 'bob' debug: msg: "{{ a ~ ' contains ' ~ b }}" when: b in a ----------------------- :wq ## Execute the playbook ansible-playbook myplaybook.yml |
Hope you have enjoyed this article. In the next blog post, we will discuss conditions in Ansible.
To get more details on Ansible, please refer below Ansible documentation.