variable names of variables holding environment variables :)

57 views
Skip to first unread message

Dmitry Makovey

unread,
May 10, 2014, 7:10:24 PM5/10/14
to ansible...@googlegroups.com
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?

Mikhail Koshelev

unread,
May 11, 2014, 5:56:54 PM5/11/14
to ansible...@googlegroups.com
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

Dmitry Makovey

unread,
May 12, 2014, 3:01:23 PM5/12/14
to ansible...@googlegroups.com
that sounds like something I need. Thanks!

Dmitry Makovey

unread,
May 13, 2014, 12:18:40 AM5/13/14
to ansible...@googlegroups.com
after tinkering with above idea I've got things working exactly how I wanted. Thanks for the tip!
Reply all
Reply to author
Forward
0 new messages