Setting fact name dynamically in set_fact?

1,594 views
Skip to first unread message

Frank Perks

unread,
Sep 21, 2015, 7:49:13 PM9/21/15
to Ansible Project
I am trying to shrink ~30+ roles into a single role to drastically reduce the amount of duplicate and copy and pasted code into a single nice and neat role. One of the problems is each role sets a specifically named fact basically like:

ec2_<appname>_foo

What i want to do is this:

set_fact:
 
"{{ some var }}" : "{{ some value }}"

The problem is that i can't seem to figure out how to do this. Now i know i could simply on the playbook task, copy the value into its corresponding var through set_fact. However a lot of the roles are run one after another, so that doesn't really work.

I was thinking i could just setup redis and push the fact there temporarily, however that seems like a lot of work. 

Frank Perks

unread,
Sep 22, 2015, 8:07:54 PM9/22/15
to Ansible Project
Any ideas? This is really blocking me. 

George Boobyer

unread,
Sep 23, 2015, 3:39:24 AM9/23/15
to Ansible Project
This might provide some inspiration, where a vars file is templated and then included allowing dynamic variable names and values.
I think it would be 'more elegant' to build up an in memory YAML/JSON  as opposed to creating a file - but haven't got a good example - so hope this helps! and it may prompt some 'better' suggestions! ; )
https://groups.google.com/d/msg/ansible-project/X1Q93QOk6Ew/ZygP5rXmfEkJ

Vikas Kumar

unread,
Sep 23, 2015, 5:18:26 AM9/23/15
to Ansible Project
Hello Frank,

This is not exactly what do you want, but certainly can give you some pointers.

Regards,
Vikas

Veny Tchistopolskii

unread,
Sep 25, 2015, 7:59:42 AM9/25/15
to Ansible Project
Hey,

Perhaps Registered Variable are what you want? (Still new to this, feel free to say I'm wrong)

According to Ansible documentation:
"Registered variables are valid on the host the remainder of the playbook run, which is the same as the lifetime of “facts” in Ansible. Effectively registered variables are just like facts."

Frank Perks

unread,
Sep 25, 2015, 5:46:49 PM9/25/15
to Ansible Project
Unfortunately it seems like include_vars only looks for the vars file on the ansible server, and will not fetch it on the remote server. Trying to elegantly handle these cases ended up becoming a huge mess. 

I ended up just creating my own action plugin that pulls the a key/value parameter and adds them as a fact. Unfortunately i can't actually figure out how to get Ansible to properly use my action plugin without shoving it in the ansible install directory. Action plugins in plugin directory seem to be ignored. 

Brian Coca

unread,
Sep 25, 2015, 6:08:18 PM9/25/15
to Ansible Project
first, you should be able to just write the var into a facts file on
the target machine and fact gathering shoudl automatically pick it up
(http://docs.ansible.com/ansible/setup_module.html read fact_path).


as for the action plugin, each plugin type has it's own directory
name, you cannot mix plugin types in the same directory, for action
plugins the directory is 'action_plugins'

--
Brian Coca

Frank Perks

unread,
Sep 27, 2015, 12:21:18 PM9/27/15
to Ansible Project
Thanks brian. action_plugins directory worked fine for me. :D
Reply all
Reply to author
Forward
0 new messages