Thursday, January 25, 2018

Ansible and Jinja2 template rendering


In this example an ansible playbook has some variables defined. These variables are then rendered by Jinja. 

The jinja template file here is jinjafile.j2  that gets saved after the rendering to the text file /tmp/ansible.all

The Jinja2 template file here looks clumsy. but please note that the multiline comment in Jinja2 is as {# .... #}. These are used in order as here in this template to inline explain the Jinja syntax. 

Once the playbook is in place and the jinja template file with the name of  jinjafile.j2 with the contents as in the mention, you can run the playbook and then see the contents of the rendered file /tnp/ansible.all

The Ansible playbook looks like 


---
- name: JinjAs
  hosts: localhost

  vars:

    foo: { "bar": 200, "abc": 1000, "sujit": 20000 }
    list1: [ 1, 2, 3 ]
    list2: [ 3, 4, 5 ]
    list3: { "x": "a", "y": "b" }

    production:
      - { name: "server1", ip: "1.1.1.1" }
      - { name: "server2", ip: "2.2.2.2" }

  tasks:
    - name: jinja render save to a file
      template: src=jinjafile.j2 dest=/tmp/ansible.all force=true


Please note that the actual JINJA syntax is highlighted in Blue and the remaining are the explanations of the syntax exampe.

The corresponding Jinja2 template file looks like

{#
## Select a random output from the list
## for selection of a random value from the list ["a", "b", "c"]
## the jinja syntax for the same is
##{{[ "a", "b", "c" ] | random }}
##
## The result is "b"
##

{{[ "a", "b", "c" ] | random }}

{#
## print the entire list here list1 is a list variable as list1 = [ 1, 2, 3 ]
## ## the jinja syntax for the same is
##
##{{ list1 }}
##
## The result is [1, 2, 3]
##
#}
{{ list1 }}


{#
## print each of the elements in a simple list
## here the list given is as list1 = [ 1, 2, 3 ]
## ## the jinja syntax for the same is
##
#}

{#
##{% for item in list1 %}
##{{ item }}
##{% endfor %}
##
## The result is
1
2
3
##
#}

{% for item in list1 %}
{{ item }}
{% endfor %}

{#
## printing of each of the element of a simple list variable where list1 is a variable like list1 = [ 1, 2, 3 ]
## This will union the 2 lists
## to union 2 lists where list1 = [ 1, 2, 3] and list2 = [ 3, 4, 5 ]
## the jinja syntax for the same is
##
##
##{{ list1 | union(list2) }}
## The result is [1, 2, 3, 4, 5]
##
#}

{{ list1 | union(list2) }}



{#
## This will show the length of the list
## This shows the length of the list list1 where list1 = [ 1, 2, 3 ]
## the jinja syntax for the same is
##
##
##{{ list1 | length }}
## The result is 3
##
#}
{{ list1 | length }}

{#
## This is to extract the elements at the list indexes of 0 and 1 from the list list11 where list1 = [ 1, 2, 3 ]
## extraction of the list index elements at the index of 0 and 1 from list list1 = [ 1, 2, 3 ]
## the jinja syntax for the same is
##
##
##{{ [0,2]  | map('extract',list1)| list }}
## The result is [1, 3]
##
#}
{{ [0,2]  | map('extract',list1)| list }}

{#
## This is to extract the values of the keys 'x' and 'y' from the dictionary { "x": 22 , "y" 44 }
## the jinja syntax for the same is
##
##{{ ['x', 'y'] | map('extract', { 'x': 22, 'y': 44 }) | list }}
##
## The result is [22, 44]
##
#}
{{ ['x', 'y'] | map('extract', { 'x': 22, 'y': 44 }) | list }}

{#
## This is again an example to know the number of elements but this time the object is a dict where
## the dict foo is as foo: { "bar": 200, "abc": 1000, "sujit": 20000 }
##
## ## the jinja syntax for the same is
##
##The lengh of the variable or the number of elements in the variable foo is
##{{ foo | length }}
##
##
## The result is 3
##
#}
{{ foo | length }}

{#
## for the dict variable foo where foo is foo: { "bar": 200, "abc": 1000, "sujit": 20000 }
## this prints each of the key and the value pairs iterating over all the elements of the dict
## notice the use of 'iteritems' here
##
## ## the jinja syntax for the same is
##
##
##
##{% for key, value  in foo.iteritems() %}
##{{ key }} : "{{ value }}"
##{% endfor %}
##
##
## The result is
##
sujit : "20000"
abc : "1000"
bar : "200"
#}
{% for key, value  in foo.iteritems() %}
{{ key }} : "{{ value }}"
{% endfor %}

{#
## for the variable foo which is a dict as foo: { "bar": 200, "abc": 1000, "sujit": 20000 }
## with the multiple elements
## this prints the value of the element having key as "bar"
## ## the jinja syntax for the same is
##
##
##{{ foo.bar }}
##
## The result is 200
##
##
##
#}
{{ foo.bar }}

{#
## this is the same as the example previous
##
##
## the jinja syntax for the same is
##
##{{ foo['bar'] }}
##
##
## The result is 200
##
#}
{{ foo['bar'] }}


{#
## This is for the example of evaluating the value of foo.bar is equal to 100 or not where foo is a dict as foo: { "bar": 200, "abc": 1000, "sujit": 20000 }
## the jinja syntax for the same is
##
##{% if foo.bar == 100 %}
##value is 100
##{% else %}
##value is not 100
##{% endif %}
##
## The result is "value is not 100"
##
#}
{% if foo.bar == 100 %}
value is 100
{% else %}
value is not 100
{% endif %}

{#
## See the length of or the list variable production where each of the entity in the variable production is a dict in itself
## Here the production variable looks like below.
## the same "production" variable has also been used in the subsequent examples
##    production:
##      - { name: "server1", ip: "1.1.1.1" }
##      - { name: "server2", ip: "2.2.2.2" }
## the jinja syntax for the same is
##
##
##{{ production | length }}
##
##
## The result is 2
##
#}
{{ production | length }}

{#
## for the production variable above that is a list of dictionaries, the list shows the items of the list
## The result will be a list of dictionaries
##
##
## the jinja syntax for the same is
##
##
##{{ production | list }}
##
## The result is [{u'ip': u'1.1.1.1', u'name': u'server1'}, {u'ip': u'2.2.2.2', u'name': u'server2'}]
##
#}
{{ production | list }}

{#
## also the way to iterate through the elements of the variable production as production is a list of dicts
##
## the jinja syntax for the same is
##
##{% for item in production %}
##      {{ item }}
##{% endfor %}
##
## The result is 
        {u'ip': u'1.1.1.1', u'name': u'server1'}
        {u'ip': u'2.2.2.2', u'name': u'server2'}

##
#}
{% for item in production %}
        {{ item }}
{% endfor %}

{#
## for the list of dictionaries values variable production this iterates to each dict element
## then this iterates through each of the elements in the dict and prints the key value pairs.
##
##
## the jinja syntax for the same is
##
##{% for item in production|list %}
##{% for key, value in item.iteritems() %}
##{{key}}: "{{value}}"
##{% endfor %}
##{% endfor %}
##
## The result is
ip: "1.1.1.1"
name: "server1"
ip: "2.2.2.2"
name: "server2"

##
#}

{% for item in production|list %}
{% for key, value in item.iteritems() %}
{{key}}: "{{value}}"
{% endfor %}
{% endfor %}

Ansible with_dict variables


Here is another example of how the referencing can take place. As can be seen "vsports" being pulled in the variable of ports


---
- name: test variables
  hosts: localhost
  gather_facts: no

  vars:
    pools: [ "pool1", "pool2" ]
    vsports: [ "8443" , "443", "8089", "8184" ]
    clusters:
      cluster1:
        server1:
          server_port: 8081
          service: apache
          pools:
            pool1:
              virtual_servers:
                - { name: "vs1", fq: "vs1.example.com", ports: "{{ vsports[0] }}" }
                - { name: "vs2", fq: "vs2.example.com", ports: "{{ vsports[1] }}" }

            pool2:
              virtual_servers:
                - { name: "vs3", fq: "vs3.example.com", ports: "{{ vsports[2] }}" }
                - { name: "vs4", fq: "vs4.example.com", ports: "{{ vsports[3] }}" }

  tasks:

    - name: see the variables for server names
      debug: msg="{{ item }}"
      with_dict: "{{ clusters }}"

The Run of the playbook as seen here.

[root@ansible testplaybooks]# ansible-playbook 6play.yaml

PLAY [test variables] **************************************************************************************************************************

TASK [see the variables for server names] ******************************************************************************************************
ok: [localhost] => (item={'value': {u'server1': {u'pools': {u'pool2': {u'virtual_servers': [{u'fq': u'vs3.example.com', u'name': u'vs3', u'ports': u'8089'}, {u'fq': u'vs4.example.com', u'name': u'vs4', u'ports': u'8184'}]}, u'pool1': {u'virtual_servers': [{u'fq': u'vs1.example.com', u'name': u'vs1', u'ports': u'8443'}, {u'fq': u'vs2.example.com', u'name': u'vs2', u'ports': u'443'}]}}, u'service': u'apache', u'server_port': 8081}}, 'key': u'cluster1'}) => {
    "changed": false,
    "item": {
        "key": "cluster1",
        "value": {
            "server1": {
                "pools": {
                    "pool1": {
                        "virtual_servers": [
                            {
                                "fq": "vs1.example.com",
                                "name": "vs1",
                                "ports": "8443"
                            },
                            {
                                "fq": "vs2.example.com",
                                "name": "vs2",
                                "ports": "443"
                            }
                        ]
                    },
                    "pool2": {
                        "virtual_servers": [
                            {
                                "fq": "vs3.example.com",
                                "name": "vs3",
                                "ports": "8089"
                            },
                            {
                                "fq": "vs4.example.com",
                                "name": "vs4",
                                "ports": "8184"
                            }
                        ]
                    }
                },
                "server_port": 8081,
                "service": "apache"
            }
        }
    },
    "msg": {
        "key": "cluster1",
        "value": {
            "server1": {
                "pools": {
                    "pool1": {
                        "virtual_servers": [
                            {
                                "fq": "vs1.example.com",
                                "name": "vs1",
                                "ports": "8443"
                            },
                            {
                                "fq": "vs2.example.com",
                                "name": "vs2",
                                "ports": "443"
                            }
                        ]
                    },
                    "pool2": {
                        "virtual_servers": [
                            {
                                "fq": "vs3.example.com",
                                "name": "vs3",
                                "ports": "8089"
                            },
                            {
                                "fq": "vs4.example.com",
                                "name": "vs4",
                                "ports": "8184"
                            }
                        ]
                    }
                },
                "server_port": 8081,
                "service": "apache"
            }
        }
    }
}

PLAY RECAP *************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0

Wednesday, January 24, 2018

Ansible with_dict to iterate over items in a dictionary type variable

Iterating the nested variables using "with_dict" provides great flexibility of addressing the dictionary type variables. Here is a simple playbook to illustrate that.

Here the variables are defined as normally using vars in the playbook play.  The variable "production" as seen in the playbook  is a YAML dictionary type variable.


  • The way the three servers 'server1', 'server2' and 'server3' are defined are the same even though they are written not in the same manne
  • YAML reads [...] as a list and { ... } as a dictionary as a key-value pairs.


The task at the and of the playbook using the keyword "with_dict" and variable as " {{ production }}" reads through all the member variables that are key value pairs.

In the last task that is a debug task you can also see by replacing the with_dict with the value of  "{{production.server1}} and see the results. The same works still as even the variable "server1" is a dictionary.

---
- name: test playbook
  hosts: localhost
  become: true
  become_user: root

  vars:
    production:
      server1:
        name: "server1"
        ip: "192.1.1.1"
        port: 443
        pools:
          - pool1
          - pool2
        vss:
          - vs1
          - vs2

      server2:
        name: "server2"
        ip: "192.168.1.2"
        port: 443
        pools: [ "pool1", "pool2" ]
        vss: [ "vs1", "vs2" ]

      server3: { name: "server3", ip: "192.168.100.200", port: 443, pools: [ "pool1", "pool2" ], vss: [ "vs1", "vs2" ] }


  tasks:
    - name: see the variables for production
      debug: msg="{{item.key}}"
      with_dict: "{{ production }}"

----

As the play book is run the following is observed as to how Ansible reads through each of the key-value pairs in the dictionary and the sub-values also


[root@ansible testplaybooks]# ansible-playbook 2play.yaml

PLAY [test playbook] *************************************************************************************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************************************************************************
ok: [localhost]

TASK [see the variables for production] ******************************************************************************************************************************************************
ok: [localhost] => (item={'value': {u'pools': [u'pool1', u'pool2'], u'ip': u'192.1.1.1', u'vss': [u'vs1', u'vs2'], u'name': u'server1', u'port': 443}, 'key': u'server1'}) => {
    "changed": false,
    "item": {
        "key": "server1",
        "value": {
            "ip": "192.1.1.1",
            "name": "server1",
            "pools": [
                "pool1",
                "pool2"
            ],
            "port": 443,
            "vss": [
                "vs1",
                "vs2"
            ]
        }
    },
    "msg": "server1"
}
ok: [localhost] => (item={'value': {u'pools': [u'pool1', u'pool2'], u'ip': u'192.168.1.2', u'vss': [u'vs1', u'vs2'], u'name': u'server2', u'port': 443}, 'key': u'server2'}) => {
    "changed": false,
    "item": {
        "key": "server2",
        "value": {
            "ip": "192.168.1.2",
            "name": "server2",
            "pools": [
                "pool1",
                "pool2"
            ],
            "port": 443,
            "vss": [
                "vs1",
                "vs2"
            ]
        }
    },
    "msg": "server2"
}
ok: [localhost] => (item={'value': {u'pools': [u'pool1', u'pool2'], u'ip': u'192.168.100.200', u'vss': [u'vs1', u'vs2'], u'name': u'server3', u'port': 443}, 'key': u'server3'}) => {
    "changed": false,
    "item": {
        "key": "server3",
        "value": {
            "ip": "192.168.100.200",
            "name": "server3",
            "pools": [
                "pool1",
                "pool2"
            ],
            "port": 443,
            "vss": [
                "vs1",
                "vs2"
            ]
        }
    },
    "msg": "server3"
}

PLAY RECAP ***********************************************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0