Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

ansible create an array with set_fact

In one of my vSphere Ansible automation task, I was looking for to create a new array variable using set_fact. here I have achieved the same task using 2 different playbook with small way procedure. In the both methods on the vars I have defined infra variable where I have defined my VMware esxi servers info as example from line no 6 to 14. Next in the set_fact task with the help of condition in when function get the connected esxi hosts list only, from with_item loops (on infra variable) in the set_fact. In the same set_fact on the line no 19, I am using esxilist variable keep adding same esxilist variable (default filter that can be used to set a default value when one isn’t provided and if esxlilist is not defined it will use empty array to append) plus loop array [item] creating one Array.

On the last debug task it is printing the esxilist var result on the screen.

You can download this playbook from github.com or from here.

setfactarray_playbook01.yml

 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
---
- hosts: localhost
  gather_facts: False
  connection: local
  vars:
    infra:
      - name: ironman.vcloud-lab.com
        connection_state: CONNECTED
      - name: hulk.vcloud-lab.com
        connection_state: CONNECTED
      - name: captain.vcloud-lab.com
        connection_state: DISCONNECTED
      - name: hawkeye.vcloud-lab.com
        connection_state: DISCONNECTED

  tasks:   
  - name: Filter list of only connected esxi
    set_fact: 
      esxilist: "{{esxilist | default([]) + [item]}}" 
    with_items: "{{infra}}"
    when: item.connection_state == "CONNECTED"
  
  - name: Show connected esxi list information
    debug:
      var: esxilist

On the second below method will get the results, only the thing is on the line no 6, I am already defining empty array variable named esxilist. so on the line no 20 I dont have to use jinja2 default() function.

setfactarray_playbook02.yml

 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
---
- hosts: localhost
  gather_facts: False
  connection: local
  vars:
    esxilist: []
    infra:
      - name: ironman.vcloud-lab.com
        connection_state: CONNECTED
      - name: hulk.vcloud-lab.com
        connection_state: CONNECTED
      - name: captain.vcloud-lab.com
        connection_state: DISCONNECTED
      - name: hawkeye.vcloud-lab.com
        connection_state: DISCONNECTED

  tasks:   
  - name: Filter list of only connected esxi
    set_fact: 
      esxilist: "{{esxilist + [item]}}"
    with_items: "{{infra}}"
    when: item.connection_state == "CONNECTED"
  
  - name: Show connected esxi list information
    debug:
      var: esxilist

Both the above yaml scripts give me same results as below after executing with ansible-playbook, It skips disconnected hosts and show me the nice json array of host with connection_state of CONNECTED.

 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
[root@centos01 002_Get_esxi_advanced_settings]# ansible-playbook setfactsarray_playbook.yml 
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

/usr/lib/python2.7/site-packages/requests/__init__.py:91: RequestsDependencyWarning: urllib3 (1.24.3) or chardet (2.2.1) doesn't match a supported version!
  RequestsDependencyWarning)

PLAY [localhost] *****************************************************************************************************************************************

TASK [Filter list of only connected esxi] ****************************************************************************************************************
ok: [localhost] => (item={u'connection_state': u'CONNECTED', u'name': u'ironman.vcloud-lab.com'})
ok: [localhost] => (item={u'connection_state': u'CONNECTED', u'name': u'hulk.vcloud-lab.com'})
skipping: [localhost] => (item={u'connection_state': u'DISCONNECTED', u'name': u'captain.vcloud-lab.com'}) 
skipping: [localhost] => (item={u'connection_state': u'DISCONNECTED', u'name': u'hawkeye.vcloud-lab.com'}) 

TASK [Show connected esxi list information] **************************************************************************************************************
ok: [localhost] => {
    "esxilist": [
        {
            "connection_state": "CONNECTED", 
            "name": "ironman.vcloud-lab.com"
        }, 
        {
            "connection_state": "CONNECTED", 
            "name": "hulk.vcloud-lab.com"
        }
    ]
}

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

Useful Articles
Ansible selectattr The error was TemplateRuntimeError no test named 'equalto'
Getting started Ansible AWX tower for IT automation run first playbook
How to install Docker on Linux
Cannot connect to the Docker daemon at unix:var run docker.sock. Is the docker daemon running
Docker Error response from daemon i\o timeout internet proxy
How to install Ansible on Linux for vSphere configuration

Go Back

Comment

Blog Search

Page Views

4127962

Follow me on Blogarama