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

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.

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.