# Register module output as host variables

**URL:** <https://forum.ansible.com/t/register-module-output-as-host-variables/16159>\
**Category:** Ansible Project\
**Created:** [September 9, 2014, 3:18pm UTC](https://forum.ansible.com/t/register-module-output-as-host-variables/16159 "2014-09-09T15:18:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Abubakr-Sadik\_Nii\_Na](https://avatars.discourse-cdn.com/v4/letter/a/58f4c7/32.png) [@Abubakr-Sadik\_Nii\_Na](https://forum.ansible.com/u/Abubakr-Sadik_Nii_Na)\
**Post date:** [September 9, 2014, 3:18pm UTC](https://forum.ansible.com/t/register-module-output-as-host-variables/16159/1 "2014-09-09T15:18:48Z")

</div>

Hello gentle people,

I think being able to register the output of commands/modules as host variables will be very useful.  
I got this idea writing a playbook to do;

1. create a root user key pair for a couple hosts
2. authorizing the root user public key of all hosts to connect to all other hosts.

My approach is to use the ‘user’ module to create generate the key pair, then use the output  
of the user module for each host as the key parameter to the ‘authorized\_key’ module.

- name: generate SSH key pair for root user  
user:  
name: root  
generate\_ssh\_key: yes  
ssh\_key\_comment: “{{inventory\_hostname}}”  
register: root\_key

- name: add authorized keys for root  
authorized\_key:  
user: root  
key: “{{hostvars[item][‘root\_key’][‘ssh\_public\_key’]}}”  
with\_items: groups[‘allservers’]

Unless I am not using ‘register’ right. The above obviously failed because register can not be  
associated with the host on which the module run. This will definitely be a good thing to have.

Any thoughts on my approach to generating and authorizing key pairs for all hosts?  
Is the ability to register module output like a host variable useful or can such be accomplished by  
other means?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [September 9, 2014, 3:43pm UTC](https://forum.ansible.com/t/register-module-output-as-host-variables/16159/2 "2014-09-09T15:43:01Z")

</div>

“. The above obviously failed because register can not be  
associated with the host on which the module run.”

I’m not understanding this part entirely, but I’d recommend authorizing keys for specific users and setting up sudo in general in that case.

For our own ansible infrastructure, this involves a list of users and their keyfiles, and then something like what’s below.

Checking your authorized keys into source control can be a good idea.

You may also wish to template the authorized\_key file using a list of keys, rather than using the authorized\_key module.

| |
| | |
| | 
- name: create/managed authorized users

 |
| | 

user: name={{ item.username }} groups=wheel append=yes

 |
| | 

with\_items: authorized\_users

 |
| | |
| | |
| | 
- name: remove unauthorized user accounts

 |
| | 

user: name={{ item.username }} state=absent

 |
| | 

with\_items: unauthorized\_users

 |
| | |
| | 
- name: add user keys

 |
| | 

authorized\_key: user={{ item.username }} key=“{{ lookup(‘file’, item.key) }}”

 |
| | 

with\_items: authorized\_users

 |

---

<div class="post-metadata">

**Author:** ![Abubakr-Sadik\_Nii\_Na](https://avatars.discourse-cdn.com/v4/letter/a/58f4c7/32.png) [@Abubakr-Sadik\_Nii\_Na](https://forum.ansible.com/u/Abubakr-Sadik_Nii_Na)\
**Post date:** [September 9, 2014, 4:09pm UTC](https://forum.ansible.com/t/register-module-output-as-host-variables/16159/3 "2014-09-09T16:09:31Z")

</div>

Wonderful! Thank you.

“. The above obviously failed because register can not be  
associated with the host on which the module run.”

For the above, say I run a command in a task on every host in a play. I was thinking it wil be nice to register the output of the command such that the  
output of the command can be accessed as it run on the host in a later task.

In other words when the output of the command is registered, it can be saved along with the host variables for the remote host on which the command  
run. This way at a later time we can always access the registered output using a syntax like ‘{{hostvars[hostA][result]}}’. For example

- name: a task  
command: some cmd  
register: result

- name: another task  
some\_module:  
parm1: “{{hostvars[item][result]”  
parm2: “another param”  
with\_items: groups[‘somehosts’]

---

<div class="post-metadata">

**Author:** ![Abubakr-Sadik\_Nii\_Na](https://avatars.discourse-cdn.com/v4/letter/a/58f4c7/32.png) [@Abubakr-Sadik\_Nii\_Na](https://forum.ansible.com/u/Abubakr-Sadik_Nii_Na)\
**Post date:** [September 10, 2014, 12:13pm UTC](https://forum.ansible.com/t/register-module-output-as-host-variables/16159/4 "2014-09-10T12:13:17Z")

</div>

> I’m not understanding this part entirely, but I’d recommend authorizing keys for specific users and setting up sudo in general in that case.
> 
> For our own ansible infrastructure, this involves a list of users and their keyfiles, and then something like what’s below.
> 
> Checking your authorized keys into source control can be a good idea.
> 
> You may also wish to template the authorized\_key file using a list of keys, rather than using the authorized\_key module.

Thank you for the The user + key file approach, and checking in the authorized\_keys work great for authorizing users. However this gets complicated when you have hosts which carry out tasks on different hosts, such as a server rsyncing files to be backed up to a backup server. These tasks are generally scripted and so keys are usually passwordless. Committing these keys with your code means that anyone who gets access to your code, gets access to your servers.

This is why I will like to be able to generate the root key pair and authorize for all hosts it in a play. This way I do not have to worry about the storage of such a sensitive key potentially exposing my hosts.

- Nii Nai

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [September 10, 2014, 12:37pm UTC](https://forum.ansible.com/t/register-module-output-as-host-variables/16159/5 "2014-09-10T12:37:14Z")

</div>

If you’re on the development branch, we identified a bug yesterday where registered variables were not accessible via hostvars, which was a result of the new “Fact Caching” additions.

This is now resolved as of yesterday.

Can you let me know what Ansible version you were using and whether this resolves the problem?

---

<div class="post-metadata">

**Author:** ![Abubakr-Sadik\_Nii\_Na](https://avatars.discourse-cdn.com/v4/letter/a/58f4c7/32.png) [@Abubakr-Sadik\_Nii\_Na](https://forum.ansible.com/u/Abubakr-Sadik_Nii_Na)\
**Post date:** [September 11, 2014, 11:33am UTC](https://forum.ansible.com/t/register-module-output-as-host-variables/16159/6 "2014-09-11T11:33:07Z")

</div>

I am on the development branch, and the ansible 1.8 (devel 1dc11c9752) release resolves the problem for me.  
Thank you very much.
