Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible register multiple variables within a single task

Tags:

ansible

I have an ansible task and I want to register multiple variables inside it, how do I achieve this? It doesn't seem that a list or a comma separated string would work.

I want to do something like this:

- name: my task
  module_name:
    <some more params>
  register: [var1, var2]

If I add register: var1 \n register: var2 then only the second one get's registered.

EDIT: OK, I think my confusion lied in the bit how variable registration works. So when you do register: any_var_name, the newly created variable contains the whole output of the task. Then you can access it any way you want as in the accepted answer.

like image 343
eddyP23 Avatar asked Dec 11 '22 09:12

eddyP23


1 Answers

No, this is not possible.

Ansible (as of current version 2.4) does not allow to register partial output of the module (or several different parts of it).

You can register only full result and extract parts of it in the tasks to follow.

For example, if you want to get std_out and std_err of command module, you would do two tasks:

- command: myscript.sh
  register: cmd_res
- set_fact:
    std_out: "{{ cmd_res.stdout }}"
    std_err: "{{ cmd_res.stderr }}"

And you can't do this in single shot, like this:

- command: myscript.sh
  # this code does not work!
  register:
    std_out: result.stdout
    std_err: result.stderr
like image 82
Konstantin Suvorov Avatar answered Jan 05 '23 22:01

Konstantin Suvorov