I'm porting a module from ansible 1.9 to 2.x, and the args file content seems.. odd? After the shlex simple strings are fine but lists and dictionaries look like over-escaped garbage. An example:

my call:

test: "packages={{test.var}}"

and test.var is a list of packages, ['one', 'two', 'three', 'foo']

ansible 1.9 args content:

packages=['one', 'two', 'three', 'foo']

ansible 2.3 args content:

packages='[u'"'"'one'"'"', u'"'"'two'"'"', u'"'"'three'"'"', u'"'"'foo'"'"']'"'"]'"'"]'"'"]'

My question: "is there a way to unserialize this args data elegantly or simply?" I can strip out the offending escapes and quotes but that's gross and brittle. I didn't find anything in the porting guide that seemed relevant.

  • I suspect something else is going on here. The brackets aren't balanced. – Mark Wagner Jun 8 '18 at 20:54
  • yeah, all the other list items in the args file have matched brackets and that one just doesn't. i've not seen something like that. I've tried it in two versions of 2.x ansible and got the same in both. – rj bertsche Jun 8 '18 at 21:13

Alright, I didn't find an answer, but I came up with a decent way to get what I want. I'll put my answer here, in case anyone stumbles on the question with a need.

First off, the unbalanced brackets disappeared based on how you called the module:

test: "blip={{testpackages}}"

would result in the unbalanced brackets, but

test: blip="{{testpackages}}"

resulted in balanced brackets.

Secondly, the entire thing becomes moot if you just ask ansible to pass the variable through a 'to_json' filter and then read that. Playbook example:

- name: run test module (ansible 2.x)
  test: packages='{{ testpackages| to_json }}'
  when: ansible_version.major|int > 1

In-module example:

packages_to_test = [x.encode('utf-8') for x in json.loads(packages)]

Anyway, good luck to you, Random Person Who Did The Same Thing We Did.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.