Reputation: 119
On my ansible play I use vars_prompt to get values for switch port interfaces. The goal of the play is to receive the data from prompt (the method I'm using now but open to cli) and append to a list. I can achieve this no problem when I only have one port but when I want more than one port it doesn't split into multiple items as it should in a list.
See my code below:
'''
hosts: all
gather_facts: false
vars:
port_list: []
vars_prompt:
- name: SwitchPort
prompt: "Please enter port name(format e.g, Ethernet1/1,Ethernet2/1)"
tasks:
- name: add port items to port_list
set_fact:
ports: "{{port_list}} + ['{{SwitchPort}}']"
- name: print ports
debug:
var: item
loop: "{{ports}}" '''
When I pass to vars_prompt: SwitchPort var Ethernet1/1 I get this which I can use. When I pass it "Ethernet1/1,Ethernet2/1" it returns Ethernet1/1,Ethernet2/1 and not two items in the list as I wanted. Can someone help me with adding data to an empty list from a cli variable?
Upvotes: 1
Views: 6307
Reputation: 311416
You're not doing anything to split SwitchPort
up into a list. You probably want something like this:
- name: add port items to port_list
set_fact:
ports: "{{ port_list + SwitchPort.split(',') }}"
With that change, if I enter Ethernet1/1,Ethernet2/1
at the prompt, the output is:
TASK [print ports] ***************************************************************************
ok: [localhost] => (item=Ethernet1/1) => {
"ansible_loop_var": "item",
"item": "Ethernet1/1"
}
ok: [localhost] => (item=Ethernet2/1) => {
"ansible_loop_var": "item",
"item": "Ethernet2/1"
}
Upvotes: 2