Create a Dict key from a variable

14 views
Skip to first unread message

Spiro Mitsialis

unread,
Oct 10, 2019, 1:12:01 PM10/10/19
to Ansible Project
Hi, 

I hope I am using the right terminology here.
I'm trying to modify some cisco_ios facts.   I know how to use combine to merge the two together.  I'm having problems creating a list/dict that I can use to merge into the original dict 
The playbook is passed a variable called "port".  What I would like to create a fact as below

set_fact:
  temp_interface
:
   
"{{ port }}":
        name
: "{{ port }}"
        description
: free
        status
: shutdown

This above does not work.  As an example port = GigabitEthernet1/0/10

The result from above is:

TASK [debug] *******************************************************************************************************************************************************************
ok
: [switch-poc-sw8.gw.mcgill.ca] => {
   
"temp_interface": {
       
"{{ port_id }}": {
           
"description": "free",
           
"name": "GigabitEthernet1/0/10",
           
"status": "shutdown"

       
}
   
}
}

How can this be accomplished.
Thanks

Vladimir Botka

unread,
Oct 10, 2019, 1:53:45 PM10/10/19
to Spiro Mitsialis, ansible...@googlegroups.com
On Thu, 10 Oct 2019 10:12:01 -0700 (PDT)
Spiro Mitsialis <spiro.m...@mcgill.ca> wrote:

> The playbook is passed a variable called "port". What I would like to
> create a fact as below
>
> set_fact:
> temp_interface:
> "{{ port }}":
> name: "{{ port }}"
> description: free
> status: shutdown

Try this

- set_fact:
temp_interface: "{{ temp_interface|
default({})|
combine({port: {'name': port,
'description': 'free',
'status': 'shutdown'}})
}}"


Cheers,

-vlado

Spiro Mitsialis

unread,
Oct 10, 2019, 2:13:42 PM10/10/19
to Ansible Project
Thanks Vladamir,

Works great!

Wish there was some good docs out there.
Reply all
Reply to author
Forward
0 new messages