Ali Yousefi Sabzevar Ali Yousefi Sabzevar - 1 month ago 11
YAML Question

yaml syntax error when defining variables using WHEN

I defined these in defaults/main.yml:

- mode : production

- consul_server_address :192.168.1.5
when : mode == "production"

- consul_server_address :192.168.2.5
when : mode == "staging"


but I got:

The offending line appears to be:

- consul_server_address : 192.168.1.5
when : mode == "production"
^ here


When I add a space after colons on
consul_server_address
, the error changes to:

ERROR! The default/main.yml file for role 'dnsmasq' must contain a dictionary of
variables


I spent some time on yaml syntax but I think this idea is wrong and I should do using another ansible-playbook solution.

Answer

Someone on Ansible's IRC channel suggested me to use groupvars/hostvars for this purpose.

I edited my hosts inventory file and did something like this:

[nginx-staging]
IP_ADDRESS OR FQDN  

[nginx-staging:vars]
...
consul_server_address=192.168.2.5


[nginx-production]
IP_ADDRESS OR FQDN

[nginx-production:vars]
...
consul_server_address=192.168.1.5

and it worked for me with a simpler and more straight forward solution.

Comments