variable names of variables holding environment variables :)

I know subject sounds kind of crazy, but here’s the code to explain it a bit:

  • hosts: all
    vars:
  • djbdns_var:
    FOO: “DJ DNS”
  • vnstat_var:
    FOO: “VN, stat!”
    tasks:
  • name: Test flexible vars
    command: echo “$FOO”
    environment: “{{ item }}_var”
    with_items:
  • djbdns
  • vnstat

which should be equivalent to something like:

tasks:

  • name: Test flexible vars (step 1)
    command: echo “$FOO”
    environment: djbdns_var"

  • name: Test flexible vars (step 2)
    command: echo “$FOO”
    environment: vnstat_var"

so what I’m trying to do is define different set of environment variables for each step of iteration, and the best way I figured was something like above. However it doesn’t work. Ansible (1.5.3) recognizes passed argument as a string and throws an error rather than interpret resulting variable name.

Question is: can something be done to accomplish what I’m trying to do?

It depends on context of your actual task. Adding top-level hash will do if you can put definitions for all *_var data in a single unit.
For multiple units (f.e. per-role definitions) you may try to set “hash_behaviour=merge” in ansible.cfg, though this is generally not recommended.

`

  • hosts: localhost
    connection: local
    vars:
  • cmd_envs:
    djbdns: { FOO: “DJ DNS” }
    vnstat: { FOO: “VN, stat!” }
    tasks:
  • name: Test flexible vars
    command: echo “$FOO”
    environment: cmd_envs[item]
    with_items:
  • djbdns
  • vnstat

`

that sounds like something I need. Thanks!

after tinkering with above idea I’ve got things working exactly how I wanted. Thanks for the tip!