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:
tasks:
- name: register into variable myvar
debug: msg=“hello world”
register: “{{ varname }}”
I have tried some things but cannot make them work
thank you
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
Wrong:
register: “{{ varname }}”
Correct:
register: varname