Ansible variables

1 minute read

Been doing some abstraction refactoring for work and ran into a situation where I had a task that used the command module in a loop and I needed to be able to use the stdout value in a template later on. My first thought was to use dynamic variables (e.g., register: _output) but Ansible treats the variable name as a literal string for both register and add_fact so that was an utter failure. After much doc trawling and searching, I used the debug module to dump the variable registered and found it was a gnarly (and deeply nested) dataset consisting of a dictionary of with both lists and more dictionaries of lists. The upside is that nearly all the data about the task is there, including the items used in the loop as well as the results I needed.

The task (cleaned up a bit):

- name: record git revision of repositories
    command git rev-parse HEAD
    - ""
  sudo: no
  register: repo_checkouts

The relevant portion of the template:

{% for checkouts in repo_checkouts.results %}
Repository: {{ checkouts.item.git_location }}
Targeted Branch/Tag/Commit: {{ checkouts.item.git_reference }}
Actual Revision: {{ checkouts.stdout }}

Note that the repo_checkouts.results includes a dictionary in each list named ‘item’ that includes all the info passed via with_items, and stdout is the output from the command. Each list also includes a ‘cmd’ dict with the input to the command module, an ‘invocation’ dict with the module and params details, and various other details like return code and time stamps.



Leave a Comment