Vyacheslav
Vyacheslav

Reputation: 11

How to combine variables into new variable?

I have Ansible task defined in custom role:

- name: Connect to db
  script: "shell.sh --url={{ db_conn }}"

Variable db_conn defined in playbook:

 vars : 
  db_host: "localhost"
  db_port: 5432
  db_conn: "http://{{ db_host }}:{{ db_port }}"

Ansible terminates with error:

A variable inserted a new parameter into the module args. Be sure to quote variables if they contain equal signs (for example: "{{var}}").

Upvotes: 1

Views: 1649

Answers (1)

Jonathan
Jonathan

Reputation: 705

This is a problem with variable precedence. I'm guessing that you defined the db_conn variable in your role's vars/main.yaml file? If so, then db_conn is considered a role variable, and has higher precedence than playbook vars (will not be overwritten by playbook vars).

When creating roles with variables you want to have overwritten, make sure they are defined as defaults to ensure that they not only get overwritten, but to clearly communicate to others that you intend them to be overwritten.

Just in case the docs are not enough for you, here is the source code which defines variable precedence from github.

Upvotes: 2

Related Questions