error with appending list in Ansible

171 views
Skip to first unread message

maulik patel

unread,
Jul 19, 2022, 4:47:46 AM7/19/22
to Ansible Project

I'm trying to append new tunnel interface to empty list, but i'm got below error. Can you please help me out here ?

tasks:         
        - 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"}

thank you..!!

Abhijeet Kasurde

unread,
Jul 19, 2022, 5:30:39 AM7/19/22
to ansible...@googlegroups.com
You can do this --

```
      - set_fact:
          list_tunnel: "{{ result }}"
        vars:
          prefix: tunnel
          a_list: "{{ range(1, 10) | list }}"
          result: "{{ [prefix] | product(a_list) | map('join', '.') | list }}"

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

or


```
      - 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 }}"
```

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/86f78a44-cb98-46bd-9db2-e2237c8b07a9n%40googlegroups.com.


--
Thanks,
Abhijeet Kasurde

maulik patel

unread,
Jul 19, 2022, 8:19:24 AM7/19/22
to Ansible Project
hi @abhijeet,

thanks for sharing a solution... first solution works like a champ...2nd is only giving last element..

Abhijeet Kasurde

unread,
Jul 19, 2022, 12:04:20 PM7/19/22
to ansible...@googlegroups.com
I get correct output with 2nd solution as well

TASK [debug] **********************************************************************************
ok: [localhost] => {
    "msg": [
        "tunnel.1",
        "tunnel.2",
        "tunnel.3",
        "tunnel.4",
        "tunnel.5",
        "tunnel.6",
        "tunnel.7",
        "tunnel.8",
        "tunnel.9"
    ]
}




--
Thanks,
Abhijeet Kasurde
Reply all
Reply to author
Forward
0 new messages