How can I pass extra variables to a shell script in my playbooks

30 views
Skip to first unread message

Conor Proud

unread,
Feb 11, 2019, 11:24:29 AM2/11/19
to Ansible Project
Hi,
This looks like the right place for posting questions- but please point me in the right direction if not.
I'm using the "extra variables" box on the awx web front-end to provide key-value pairs to a shell script in my playbook. The problem I'm having is how I can use those variables in my shell script, as they all appear as {u'key': u'value'} when I print them from within my shell script. This means that they don't conform to the standard JSON format (which uses double-quotes) and also as these are unicode strings I'm having other issues trying to pull out the information I want. What I would like is to easily put these key-value pairs into an associative array so I can access the values when they're required. Is there a good way to do this? At the moment I'm having to use sed to substitute out the bits I don't want (the single quotes have to be converted to double quotes and I have to remove the "u" character and then pass this to a package called jq to read the string as a JSON object). This feels like the wrong approach, so I'm look for some advice or examples of whether this can be done more elegantly?

Jon Spriggs

unread,
Feb 11, 2019, 3:38:14 PM2/11/19
to ansible...@googlegroups.com
Hi Conor, sounds like you're probably doing this (untested pseudocode):

- shell: "/path/to/script {{ var }}"
  vars:
    var:
      some_key: some_data

Instead, you need to do something like this (untested pseudocode):

- shell: "/path/to/script {% for item in var|dict2items %}{{ item.key }}={{ item.value }}{% endfor %}"
  vars:
    var:
      some_key: some_data

If not, if you can post some example script, that might help...

All the best,
--
Jon "The Nice Guy" Spriggs
@jontheniceguy everywhere...
https://jon.sprig.gs


--
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 post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/d04dc999-d042-46b2-af50-c543443d7190%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages