3

I'm trying to append new tunnel interface to empty list, but I'm getting the below error.

- name: empty list
  set_fact:
          list_tunnel: []

- name: create new list for tunnel
  set_fact:
        list_tunnel_new: "{{ list_tunnel + ['tunnel.{{item}}'] }}"
  loop: "{{ range(1,10)|list}}"

Error:

fatal: [SJL]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'item' is undefined\n\nThe error appears to be in '/etc/ansible/aws/interface_palo_facts.yaml': line 34, column 12, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: print facts\n ^ here\n"}

Abhijeet Kasurde
  • 3,937
  • 1
  • 24
  • 33
Mike
  • 61
  • 1
  • 3
  • Does this answer your question? [How can I make Ansible interpret a variable inside a variable?](https://stackoverflow.com/questions/67408680/how-can-i-make-ansible-interpret-a-variable-inside-a-variable) – β.εηοιτ.βε Jul 19 '22 at 08:45
  • Also mind that you are getting this error in a task that you are not showing here `- name: print facts` – β.εηοιτ.βε Jul 19 '22 at 08:47
  • And so, your `set_fact` should read `list_tunnel_new: "{{ list_tunnel + ['tunnel.' ~ item] }}"` – β.εηοιτ.βε Jul 19 '22 at 08:47
  • here is the print_facts: - name: print facts debug: msg: "{{ list_tunnel_new }}" – Mike Jul 19 '22 at 08:56
  • also , i tried to use your direction but getting below o/p , looks like it is not appending: ok: [SJL] => { "msg": [ "tunnel.9" ] } what i need is : [tunnel.1, tunnel.2, tunnel.3.......,tunnel.9 ] – Mike Jul 19 '22 at 09:00
  • Because you made a mess in your variables names: `list_tunnel: "{{ list_tunnel + ['tunnel.' ~ item] }}"` – β.εηοιτ.βε Jul 19 '22 at 09:24

2 Answers2

3

You can do it like this:

- name: create new list for tunnel
  set_fact:
    list_tunnel: "{{ list_tunnel | default([]) + ['tunnel.' + item | string] }}"
  loop: "{{ range(1,10) | list}}"

- debug:
    msg: "{{ list_tunnel }}"

or

- set_fact:
    list_tunnel: "{{ result }}"
  vars:
    prefix: tunnel
    a_list: "{{ range(1, 10) | list }}"
    result: "{{ [prefix] | product(a_list) | map('join', '.') | list }}"
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
Abhijeet Kasurde
  • 3,937
  • 1
  • 24
  • 33
  • How does Ansible works w that 'map(join, .) part? It's so weird to see that. And why is prefix required, why can't we simply put the string 'tunnel' as a string in the result variable. – Kevin C Aug 23 '23 at 14:18
1

Create the list you want to add

  list_tunnel_add: "{{ ['tunnel']|product(range(1,10))|
                                  map('join','.')|
                                  list }}"

gives

  list_tunnel_add:
  - tunnel.1
  - tunnel.2
  - tunnel.3
  - tunnel.4
  - tunnel.5
  - tunnel.6
  - tunnel.7
  - tunnel.8
  - tunnel.9

Then concatenate the lists

  list_tunnel_new: "{{ list_tunel + list_tunnel_add }}"

Example of a complete playbook

- hosts: localhost
  vars:
    list_tunel: []
    list_tunnel_add: "{{ ['tunnel']|product(range(1,10))|
                                    map('join','.')|
                                    list }}"
    list_tunnel_new: "{{ list_tunel + list_tunnel_add }}"
  tasks:
    - debug:
        var: list_tunnel_new

gives

  list_tunnel_new:
  - tunnel.1
  - tunnel.2
  - tunnel.3
  - tunnel.4
  - tunnel.5
  - tunnel.6
  - tunnel.7
  - tunnel.8
  - tunnel.9
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63