简体   繁体   中英

How to parse variables in Ansible group_vars dictionary?

I have previously been placing all of my variables within the inventory file, such as

dse_dir=/app/dse
dse_bin_dir={{ dse_dir }}/bin
dse_conf_dir={{ dse_dir }}/resources/dse/conf
dse_yaml_loc={{ dse_conf_dir }}/dse.yaml
cass_conf_dir={{ dse_dir }}/resources/cassandra/conf
cass_yaml_loc={{ cass_conf_dir }}/cassandra.yaml
cass_bin_dir={{ dse_dir }}/resources/cassandra/bin

I did not need to use any quotes for these variables in the inventory file and it worked quite well.

Now I am trying to make use of the group_vars functionality, to separate variables per group of hosts. This has a different format, being a dictionary. So now I have:

dse_dir: "/app/dse"
dse_bin_dir: "{{ dse_dir }}/bin"
dse_conf_dir: "{{ dse_dir }}/resources/dse/conf"
dse_yaml_loc: "{{ dse_conf_dir }}/dse.yaml"
cass_conf_dir: "{{ dse_dir }}/resources/cassandra/conf"
cass_yaml_loc: "{{ cass_conf_dir }}/cassandra.yaml"
cass_bin_dir: "{{ dse_dir }}/resources/cassandra/bin"

In order to avoid parsing complains, I need to place quotes around these parameters. But now when I have a playbook such as the following:

---
# Copy CQL files across
- include: subtasks/copy_scripts.yml

- name: Create users
  command: '{{ cass_bin_dir })/cqlsh'

I get the following error. Omitting the single quotes or replacing them with double quotes does not work either.

ERROR: There was an error while parsing the task 'command {{ cass_bin_dir })/cqlsh'.
Make sure quotes are matched or escaped properly

All of the documentation that I could find only shows hardcoded values in the dictionary, ie without variables including other variables, but I would assume that Ansible would support this.

Any advice on how to parse these properly?

See the “Gotchas” section here for understanding why you needed to add the quotes in your group_vars . (It's the yaml/ansible problematic : {{ combo.)

To address the error in your command , fix the typo: you have a }) instead of }} .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM