# features of register:

**URL:** <https://forum.ansible.com/t/features-of-register/13973>\
**Category:** Ansible Project\
**Created:** [November 22, 2013, 2:27pm UTC](https://forum.ansible.com/t/features-of-register/13973 "2013-11-22T14:27:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jerome\_Wagner](https://avatars.discourse-cdn.com/v4/letter/j/76d3ee/32.png) [@Jerome\_Wagner](https://forum.ansible.com/u/Jerome_Wagner)\
**Post date:** [November 22, 2013, 2:27pm UTC](https://forum.ansible.com/t/features-of-register/13973/1 "2013-11-22T14:27:31Z")

</div>

Hello,

The feature

- name: capture output  
debug: msg=“hello world”  
register: myvar  
allows to capture the result of the task inside the variable myvar.

if a with\_item is added, myvar.results becomes a list with all the results

I have a few questions regarding this :

1/ register and role variables

is it possible to register inside myvar only a subpart of the result, like

register: myvar filter=subkey

2/ variable expansion

is it possible to name a variable by variable

vars:

- varname: myvar

tasks:

- name: register into variable myvar  
debug: msg=“hello world”  
register: “{{ varname }}”

I have tried some things but cannot make them work

thank you

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [November 22, 2013, 9:06pm UTC](https://forum.ansible.com/t/features-of-register/13973/2 "2013-11-22T21:06:26Z")

</div>

1. No, you must name a top level variable with register and cannot add to an existing variable.

2. Maybe you should try it and let us know 🙂 … However, a major goal of Ansible is readability and auditability, so I don’t think you should probably do that in most cases unless you’ve got a really good use case for it - it would probably make your playbooks much less transparent.

---

<div class="post-metadata">

**Author:** ![Jerome\_Wagner](https://avatars.discourse-cdn.com/v4/letter/j/76d3ee/32.png) [@Jerome\_Wagner](https://forum.ansible.com/u/Jerome_Wagner)\
**Post date:** [November 22, 2013, 10:23pm UTC](https://forum.ansible.com/t/features-of-register/13973/3 "2013-11-22T22:23:29Z")

</div>

regarding “variable expansion” it does not work currently I think (except if my test is wrong):

- hosts: localhost  
gather\_facts: no  
vars:
- varname: myvar  
tasks:
- name: register into myvar  
tags: expansion  
debug: msg=“hello world”  
register: “{{ varname }}”
- name: output myvar  
tags: expansion  
debug: var=myvar

PLAY [localhost] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK: [register into myvar] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [127.0.0.1] =\> {  
“msg”: “hello world”  
}

TASK: [output myvar] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [127.0.0.1] =\> {  
“myvar”: “{{ myvar }}”  
}

note that if i do

debug: var={{myvar}}

I get  
fatal: [127.0.0.1] =\> One or more undefined variables: ‘myvar’ is undefined

so it does not work currently. I can think of some use cases where it could be handy to have such expansions.

the main case I see “pro” expansion is the following :

1/ register: foo inside a role  
2/ call the role 2 times with different parameters  
you lost one of the 2 results.

it’s like programming  
my\_important\_result = role(params1)  
my\_important\_result = role(params2)

but I agree with you variable expansion is probably dangerous for readability.

Jerome

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [November 23, 2013, 3:56pm UTC](https://forum.ansible.com/t/features-of-register/13973/4 "2013-11-23T15:56:11Z")

</div>

Wrong:

register: “{{ varname }}”

Correct:

register: varname
