# shlex.split and spaces in the value of a key=value pair

**URL:** <https://forum.ansible.com/t/shlex-split-and-spaces-in-the-value-of-a-key-value-pair/15436>\
**Category:** Ansible Developer\
**Created:** [June 10, 2014, 12:07pm UTC](https://forum.ansible.com/t/shlex-split-and-spaces-in-the-value-of-a-key-value-pair/15436 "2014-06-10T12:07:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stanley\_Kamithi](https://avatars.discourse-cdn.com/v4/letter/s/13edae/32.png) [@Stanley\_Kamithi](https://forum.ansible.com/u/Stanley_Kamithi)\
**Post date:** [June 10, 2014, 12:07pm UTC](https://forum.ansible.com/t/shlex-split-and-spaces-in-the-value-of-a-key-value-pair/15436/1 "2014-06-10T12:07:30Z")

</div>

Hello,

Building new modules. Having problem when creating a key=value pair where “value” is an array.  
I get the following error with the below config…

failed: [sw3] =\> (item={‘value’: {‘ipv4’: [‘10.100.1.1/24’, ‘10.200.1.1/24’]}, ‘key’: ‘swp2’}) =\> {“failed”: true, “item”: {“key”: “swp2”, “value”: {“ipv4”: [“10.100.1.1/24”, “10.200.1.1/24”]}}}  
msg: this module requires key=value arguments ([‘name=swp2’, ‘ipv4=[10.100.1.1/24,’, ‘10.200.1.1/24]’, ‘applyconfig=yes’])

This is my variables file.  
======== doesn’t work ======  
interfaces:  
sw3:  
swp2:  
ipv4:

- 10.100.1.1/24
- 10.200.1.1/24

If I change the variables file to say what is below… it works  
===== this works =====  
interfaces:  
sw3:  
swp2:  
ipv4: “10.100.1.1/24,10.200.1.1/24”

I notice the problem is that the first configuration puts a space between the 2 elements in the array of IPs, and ‘shlex.split’ splits it at the space between the array elements. Any way around this? I like the first format much better. Plus now have to be super careful of white spaces in values with arrays. Find this kinda brittle.

My AnsibleModule argument call is like so, maybe I’m doing something wrong there?

module = AnsibleModule(  
argument\_spec=dict(  
name=dict(required=True, type=‘str’),  
bridgemems=dict(type=‘list’),  
bondmems=dict(type=‘list’),  
ipv4=dict(type=‘list’),  
ipv6=dict(type=‘list’),  
applyconfig=dict(required=True, type=‘str’)  
),  
mutually\_exclusive=[  
[‘bridgemems’, ‘bondmems’]  
]  
)

Thanks

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [June 10, 2014, 2:30pm UTC](https://forum.ansible.com/t/shlex-split-and-spaces-in-the-value-of-a-key-value-pair/15436/2 "2014-06-10T14:30:50Z")

</div>

It looks like this is due to the way you’re passing the variable into the module. If you want to maintain the datastructure, you need to use “complex args”, which means specifying the variables as follows:

- name: do something  
module:  
foo: bar  
var2: “something”  
my\_list: “{{var.sub\_list}}”

It appears though, that you’re trying to call the module with regular args, which means they are passed through shlex.split():

- name: do something  
module: foo=bar var2=“something” my\_list=“{{var.sub\_list}}”

As you noted, the solution is to either use the simple variable syntax (specifying lists as comma-separated items) or use the complex variable form for specifying your parameters.

---

<div class="post-metadata">

**Author:** ![Ernest0x](https://avatars.discourse-cdn.com/v4/letter/e/ed655f/32.png) [@Ernest0x](https://forum.ansible.com/u/Ernest0x)\
**Post date:** [June 10, 2014, 2:58pm UTC](https://forum.ansible.com/t/shlex-split-and-spaces-in-the-value-of-a-key-value-pair/15436/3 "2014-06-10T14:58:42Z")

</div>

With little help from jinja one can use simple variable syntax and still keep his variable as a YAML list:

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [June 10, 2014, 4:17pm UTC](https://forum.ansible.com/t/shlex-split-and-spaces-in-the-value-of-a-key-value-pair/15436/4 "2014-06-10T16:17:13Z")

</div>

For those that don’t know you can use complex vars here and not deal with any of the string ops:

- module:  
foo: bar  
var2: baz  
my\_list: var.sub\_list

etc
