Skip to content

Instantly share code, notes, and snippets.

@hluaces
Created August 29, 2019 09:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hluaces/0b99c20ab6259a23a4e06b90bd7bb86d to your computer and use it in GitHub Desktop.
Save hluaces/0b99c20ab6259a23a4e06b90bd7bb86d to your computer and use it in GitHub Desktop.
---
- name: 'Example playbook to populate a dinamically created list list'
hosts: localhost
vars:
# The list of known VPNs
vpn_list:
- 'vpn-1'
- 'vpn-2'
- 'vpn-3'
- 'vpn-4'
# The list that will be populated
vpn_names: []
tasks:
- name: 'Include a subtask to populate the list'
include_tasks: '_testing_populated_loop_task.yml'
loop: "{{ vpn_list }}"
loop_control:
loop_var: _vpn
- name: 'Show the created list'
debug:
var: vpn_names
---
# This is an example, so I'm using a simple 'echo' command.
- name: 'Obtain the VPN name with an arbitrary command'
command: "echo 'VPN-NAME-{{ _vpn }}'"
register: _vpn_name
- name: 'Add to the list the newly obtained VPN name'
set_fact:
vpn_names: "{{ vpn_names + [_vpn_name.stdout] }}"

Execution

$ ansible-playbook -v --connection=local _testing_populated_loop.yml 

Output

Using /home/hector/repos/ansible-playbooks/ansible.cfg as config file
[WARNING]: No inventory was parsed, only implicit localhost is available

[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'


PLAY [Example playbook to populate a dinamically created list list] **************************************************************************************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************************************************************************************************************************
Thursday 29 August 2019  11:37:43 +0200 (0:00:00.065)       0:00:00.065 ******* 
ok: [localhost]

TASK [Include a subtask to populate the list] ************************************************************************************************************************************************************************************************
Thursday 29 August 2019  11:37:44 +0200 (0:00:00.622)       0:00:00.688 ******* 
included: /home/hector/repos/ansible-playbooks/_testing_populated_loop_task.yml for localhost
included: /home/hector/repos/ansible-playbooks/_testing_populated_loop_task.yml for localhost
included: /home/hector/repos/ansible-playbooks/_testing_populated_loop_task.yml for localhost
included: /home/hector/repos/ansible-playbooks/_testing_populated_loop_task.yml for localhost

TASK [Obtain the VPN name with an arbitrary command] *****************************************************************************************************************************************************************************************
Thursday 29 August 2019  11:37:44 +0200 (0:00:00.070)       0:00:00.758 ******* 
changed: [localhost] => changed=true 
  cmd:
  - echo
  - VPN-NAME-vpn-1
  delta: '0:00:00.001349'
  end: '2019-08-29 11:37:44.550232'
  rc: 0
  start: '2019-08-29 11:37:44.548883'
  stderr: ''
  stderr_lines: <omitted>
  stdout: VPN-NAME-vpn-1
  stdout_lines: <omitted>

TASK [Add to the list the newly obtained VPN name] *******************************************************************************************************************************************************************************************
Thursday 29 August 2019  11:37:44 +0200 (0:00:00.155)       0:00:00.913 ******* 
ok: [localhost] => changed=false 
  ansible_facts:
    vpn_names:
    - VPN-NAME-vpn-1

TASK [Obtain the VPN name with an arbitrary command] *****************************************************************************************************************************************************************************************
Thursday 29 August 2019  11:37:44 +0200 (0:00:00.026)       0:00:00.940 ******* 
changed: [localhost] => changed=true 
  cmd:
  - echo
  - VPN-NAME-vpn-2
  delta: '0:00:00.001360'
  end: '2019-08-29 11:37:44.667445'
  rc: 0
  start: '2019-08-29 11:37:44.666085'
  stderr: ''
  stderr_lines: <omitted>
  stdout: VPN-NAME-vpn-2
  stdout_lines: <omitted>

TASK [Add to the list the newly obtained VPN name] *******************************************************************************************************************************************************************************************
Thursday 29 August 2019  11:37:44 +0200 (0:00:00.090)       0:00:01.031 ******* 
ok: [localhost] => changed=false 
  ansible_facts:
    vpn_names:
    - VPN-NAME-vpn-1
    - VPN-NAME-vpn-2

TASK [Obtain the VPN name with an arbitrary command] *****************************************************************************************************************************************************************************************
Thursday 29 August 2019  11:37:44 +0200 (0:00:00.026)       0:00:01.057 ******* 
changed: [localhost] => changed=true 
  cmd:
  - echo
  - VPN-NAME-vpn-3
  delta: '0:00:00.001349'
  end: '2019-08-29 11:37:44.784794'
  rc: 0
  start: '2019-08-29 11:37:44.783445'
  stderr: ''
  stderr_lines: <omitted>
  stdout: VPN-NAME-vpn-3
  stdout_lines: <omitted>

TASK [Add to the list the newly obtained VPN name] *******************************************************************************************************************************************************************************************
Thursday 29 August 2019  11:37:44 +0200 (0:00:00.091)       0:00:01.148 ******* 
ok: [localhost] => changed=false 
  ansible_facts:
    vpn_names:
    - VPN-NAME-vpn-1
    - VPN-NAME-vpn-2
    - VPN-NAME-vpn-3

TASK [Obtain the VPN name with an arbitrary command] *****************************************************************************************************************************************************************************************
Thursday 29 August 2019  11:37:44 +0200 (0:00:00.026)       0:00:01.174 ******* 
changed: [localhost] => changed=true 
  cmd:
  - echo
  - VPN-NAME-vpn-4
  delta: '0:00:00.001419'
  end: '2019-08-29 11:37:44.903063'
  rc: 0
  start: '2019-08-29 11:37:44.901644'
  stderr: ''
  stderr_lines: <omitted>
  stdout: VPN-NAME-vpn-4
  stdout_lines: <omitted>

TASK [Add to the list the newly obtained VPN name] *******************************************************************************************************************************************************************************************
Thursday 29 August 2019  11:37:44 +0200 (0:00:00.092)       0:00:01.266 ******* 
ok: [localhost] => changed=false 
  ansible_facts:
    vpn_names:
    - VPN-NAME-vpn-1
    - VPN-NAME-vpn-2
    - VPN-NAME-vpn-3
    - VPN-NAME-vpn-4

TASK [Show the created list] *****************************************************************************************************************************************************************************************************************
Thursday 29 August 2019  11:37:44 +0200 (0:00:00.029)       0:00:01.296 ******* 
ok: [localhost] => 
  vpn_names:
  - VPN-NAME-vpn-1
  - VPN-NAME-vpn-2
  - VPN-NAME-vpn-3
  - VPN-NAME-vpn-4

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

Thursday 29 August 2019  11:37:44 +0200 (0:00:00.010)       0:00:01.307 ******* 
=============================================================================== 
Gathering Facts ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 0.62s
Obtain the VPN name with an arbitrary command ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 0.16s
Obtain the VPN name with an arbitrary command ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 0.09s
Obtain the VPN name with an arbitrary command ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 0.09s
Obtain the VPN name with an arbitrary command ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 0.09s
Include a subtask to populate the list ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ 0.07s
Add to the list the newly obtained VPN name ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 0.03s
Add to the list the newly obtained VPN name ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 0.03s
Add to the list the newly obtained VPN name ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 0.03s
Add to the list the newly obtained VPN name ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 0.03s
Show the created list ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 0.01s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment