How to read variable file each iteration of a task?

52 views
Skip to first unread message

Nick Tkach

unread,
Oct 18, 2016, 3:02:43 PM10/18/16
to Ansible Project
So, the general idea is I've got a playbook that has a set of "property" files (yaml format).  Different filenames, but the variable names inside are the same:

group_vars/siteA.yml
---
site_name
: appA
port
: 8080

group_vars
/siteB.yml
---
site_name
: appB
port
: 8090



and so on.  I want to run a task that does work using each of those in turn like

- name: print out the value of site_name and port for each of the group_vars files
  debug
: msg="My site {{ site_name }} is on port {{ port }}"



So you'd expect output like

My site appA is on port 8080
My site appB is on port 8090

I'm not finding a way to get this result.  You can't apply a with_fileglob to a task block so I'm not sure how to do something to *both* "reset" the variables (something like var_files?) *and* do some kind of work with the values from *that* file.  I'm assuming I'm missing something about how to look at this?

Alexander H. Laughlin

unread,
Oct 18, 2016, 4:56:52 PM10/18/16
to Ansible Project
Have you considered conditional includes? Along with include_vars I think you should be able to achieve the desired effect.

Something like the below:

---

- name: include_vars example

  hosts: all

  tasks:

    - name: Debug hostname.

      debug:

        var: inventory_hostname

      # Different filenames with the same variables *should* override the variables for each instance of playbook execution.

      # But precedence is tricky and I've had trouble getting things like this to work in the past, so ymmv.
           - name: Include variables based on hostname.

      include_vars: "vars/{{ inventory_hostname }}.yml"

    - name: Print site name and port number.
             debug:

        msg: "My site {{ site_name }} is running on port {{ port_number }}."

...

# vim: ft=ansible:


Sorry if the formatting is a little wonky the code widget is a little weird sometimes.

Hope this helps, best of luck.

Alex

Nick Tkach

unread,
Oct 19, 2016, 9:39:35 AM10/19/16
to Ansible Project
Yes, thanks I think that does help some.  I think the part I'm missing still is how to somehow pull in each file name one at a time.  I know in pseudo-code it's something like

for( one_filename in fileglob( vars/*.yml) {

- include_vars "{{ one_filename ))"
- debug: msg="{{ site_name }} on port {{ port }}"
}

I know you can't just do something like

- include_vars "{{ item }}"
  with_fileglob
:
   
- "vars/*.yml"

since that's going to just pull in the whole list immediately and print out the results at the end.

Nick Tkach

unread,
Oct 19, 2016, 10:09:26 AM10/19/16
to Ansible Project
I just realized I left out one key piece of info on this.  It's all running on localhost always.  The idea with this is we're generating a series of config files locally that a separate script later is going to deal with actually pushing out (for various reasons it needs to be outside of Ansible for now).  So unfortunately I don't think we can take advantage of inventory or group.
Reply all
Reply to author
Forward
0 new messages