jmreicha
jmreicha

Reputation: 3354

How to pass a list as extra variable in Ansible CLI?

Is there a way to override an Ansible variable by passing it as an --extra-var/-e from the CLI. The catch is that the variable I want to override is a list and I can't figure out the correct syntax.

Here's what the variable looks like in my group_vars file:

my_variable:
    - { var1: "value1", var2: "value2" }
    ...

Here's what my command looks like:

ansible-playbook -i inventory playbook.yml -e 'my_variable={ var1: value1, var2: value2 }' --limit 1.2.3.4 --diff --check

I get the following error:

fatal: [1.2.3.4] => with_items expects a list or a set

Is this possible? What am I doing wrong?

Upvotes: 3

Views: 7134

Answers (1)

techraf
techraf

Reputation: 68649

This is a JSON, so you need to enclose your dictionary in square brackets to be passed as an element of a list (an array in JSON parlance). Moreover, you need to quote it properly:

-e 'my_variable="[ { \"var1\": \"value1\", \"value2\": \"value2\" } ]"'

Upvotes: 3

Related Questions