How to use --extra-vars JSON to replace part of a complex variable data structure

524 views
Skip to first unread message

zihaoyu

unread,
Feb 25, 2014, 4:33:55 PM2/25/14
to ansible...@googlegroups.com
I have in my playbook this var:

- name: Deploy nodejs app
  vars:
    nodejs:
      name: myapp
      version: 0.1.1
      settings:
        port: 3000
        log: /var/log

I want to replace or override version and port in this nodejs variable at runtime. I've tried:
  1. --extra-vars 'nodejs.version=0.1.2 nodejs.settings.port=4000'
  2. --extra-vars 'nodejs["version"]=0.1.2 nodejs["settings"]["port"]=4000'
  3. --extra-vars '{"nodejs": {"version": "0.1.2", "settings": {"port": 4000}}}'

None of them worked. In 1) and 2), the values of version and port are not passed in, and in 3), nodejs variable was entirely replaced so that all the other values like name and log are gone.

So is there a way to use --extra-vars to override part of a complex variable?

Thanks.

Michael DeHaan

unread,
Feb 26, 2014, 7:51:32 AM2/26/14
to ansible...@googlegroups.com
JSON is the correct format to define a hash there.  In the first two examples you defined two seperate scalar variables with a "." in them.

You can turn on hash merging behavior in ansible.cfg if you wish hash elements to blend together versus override.

I'll warn you that not all places of the code throughly use it, so you might hit a kink here or there in 1.4.X, though 1.5 is quite better about it.


--
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/5ee29a64-fcce-4aba-8780-5f4ca59ea216%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages