# Ansible register multiple variables within a single task

**URL:** <https://forum.ansible.com/t/ansible-register-multiple-variables-within-a-single-task/31286>\
**Category:** Ansible Project\
**Tags:** rhel\
**Created:** [September 16, 2019, 1:23pm UTC](https://forum.ansible.com/t/ansible-register-multiple-variables-within-a-single-task/31286 "2019-09-16T13:23:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Keith\_Mills](https://avatars.discourse-cdn.com/v4/letter/k/b77776/32.png) [@Keith\_Mills](https://forum.ansible.com/u/Keith_Mills)\
**Post date:** [September 16, 2019, 1:23pm UTC](https://forum.ansible.com/t/ansible-register-multiple-variables-within-a-single-task/31286/1 "2019-09-16T13:23:20Z")

</div>

Hello All,

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.

This is what I have:

`

- name: Set SELinux to permissive mode | RHEL  
selinux:  
policy: targeted  
state: permissive  
register: set\_selinux, task\_result  
when: ansible\_distribution|lower == ‘redhat’  
`

I want to do something like this:

`

- name: my task  
module\_name:  
  
register: [var1, var2]

`

If I add `register: var1 \n register: var2` then I get the following Error:

`  
The field ‘register’ is supposed to be a string type, however the incoming data structure is a \<class ‘ansible.parsing.yaml.objects.AnsibleSequence’\>

`

How do I resolve this issue?

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [September 16, 2019, 1:30pm UTC](https://forum.ansible.com/t/ansible-register-multiple-variables-within-a-single-task/31286/2 "2019-09-16T13:30:54Z")

</div>

> Hello All,
> 
> 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.
> 
> This is what I have:
> 
> \>  
> -name:SetSELinuxto permissive mode |RHEL  
> selinux:  
> policy:targeted  
> state:permissive  
> register:set\_selinux,task\_result  
> when:ansible\_distribution|lower =='redhat'  
> \>
> 
> I want to do something like this:
> 
> \>  
> -name:mytask  
> module\_name:  
> \<some more params\>  
> register:[var1,var2]  
> \>
> 
> If I add |register: var1 \n register: var2| then I get the following Error:
> 
> \>  
> Thefield 'register'issupposed to be a stringtype,however the incoming data structure isa  
> \<class'ansible.parsing.yaml.objects.AnsibleSequence'\>
> 
> \>
> 
> How do I resolve this issue?

It is really unclear to me why you want to do this, so please explain.  
You will receive better answers when you explain your objectives.

At any rate, you can easily copy the registered variable in a separate set\_fact task.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Keith\_Mills](https://avatars.discourse-cdn.com/v4/letter/k/b77776/32.png) [@Keith\_Mills](https://forum.ansible.com/u/Keith_Mills)\
**Post date:** [September 16, 2019, 1:45pm UTC](https://forum.ansible.com/t/ansible-register-multiple-variables-within-a-single-task/31286/3 "2019-09-16T13:45:46Z")

</div>

Hi Stefan,

I’m working on a task for SELinux that I need to install dependencies, set SELinux to permissive mode, etc. This is for RHEL, SLES, and Debian. Also The policy for RHEL and Debian are named differently (RHEL = targeted) and (Debian = default), I currently don’t know what the poilicy for SLES is named. Also, I need to register set\_selinux and task\_result for this because if I don’t register them my task doesn’t work. I’m still quite new to Ansible so some things are still confusing. I don’t understand the set\_fact task!!! Here is my selinux.yaml:

`

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [September 17, 2019, 9:28am UTC](https://forum.ansible.com/t/ansible-register-multiple-variables-within-a-single-task/31286/4 "2019-09-17T09:28:57Z")

</div>

> Hi Stefan,
> 
> I'm working on a task for SELinux that I need to install dependencies, set  
> SELinux to permissive mode, etc. This is for RHEL, SLES, and Debian. Also  
> The policy for RHEL and Debian are named differently (RHEL = targeted) and  
> (Debian = default), I currently don't know what the poilicy for SLES is  
> named. Also, I need to register set\_selinux and task\_result for this  
> because if I don't register them my task doesn't work.

You have not explained why, "doesn't work" is not an explanation.

You can only use one register on a task, if you have several only the last one is set.

> I'm still quite new  
> to Ansible so some things are still confusing. I don't understand the  
> set\_fact task!!!! Here is my selinux.yaml:
> 
> ---  
> - name: install selinux dependencies when selinux is installed on Debian  
> &nbsp;&nbsp;apt:  
> &nbsp;&nbsp;&nbsp;&nbsp;name: ['policycoreutils', 'checkpolicy', 'selinux-basics',  
> 'python-selinux' ]  
> &nbsp;&nbsp;&nbsp;&nbsp;state: present  
> &nbsp;&nbsp;when: ansible\_distribution|lower == 'debian'  
> - name: Set SELinux to permissive mode | RHEL  
> &nbsp;&nbsp;selinux:  
> &nbsp;&nbsp;&nbsp;&nbsp;policy: targeted  
> &nbsp;&nbsp;&nbsp;&nbsp;state: permissive  
> &nbsp;&nbsp;register: set\_selinux  
> &nbsp;&nbsp;register: task\_result  
> &nbsp;&nbsp;when: ansible\_distribution|lower == 'redhat'  
> - name: Set SELinux to permissive mode | Debian  
> &nbsp;&nbsp;selinux:  
> &nbsp;&nbsp;&nbsp;&nbsp;policy: default  
> &nbsp;&nbsp;&nbsp;&nbsp;state: permissive  
> &nbsp;&nbsp;register: set\_selinux  
> &nbsp;&nbsp;register: task\_result  
> &nbsp;&nbsp;when:  
> &nbsp;&nbsp;&nbsp;&nbsp;- ansible\_selinux\_python\_present|bool  
> &nbsp;&nbsp;&nbsp;&nbsp;- ansible\_distribution|lower == 'debian'  
> - name: Reboot the server and wait for it to come back up.  
> &nbsp;&nbsp;reboot:  
> &nbsp;&nbsp;when: task\_result is changed

Here you are reusing the same variable in register, that will not work since it will be overwritten by the last task.

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [September 17, 2019, 9:58am UTC](https://forum.ansible.com/t/ansible-register-multiple-variables-within-a-single-task/31286/5 "2019-09-17T09:58:35Z")

</div>

> Hi Stefan,
> 
> I'm working on a task for SELinux that I need to install dependencies, set SELinux to permissive mode, etc. This is for  
> RHEL, SLES, and Debian. Also The policy for RHEL and Debian are named differently (RHEL = targeted) and (Debian =  
> default), I currently don't know what the poilicy for SLES is named. Also, I need to register set\_selinux and  
> task\_result for this because if I don't register them my task doesn't work. I'm still quite new to Ansible so some  
> things are still confusing. I don't understand the set\_fact task!!!! Here is my selinux.yaml:
> 
> \>  
> ---  
> -name:install selinux dependencies whenselinux isinstalled on Debian  
> apt:  
> name:['policycoreutils','checkpolicy','selinux-basics','python-selinux']  
> state:present  
> when:ansible\_distribution|lower =='debian'  
> -name:SetSELinuxto permissive mode |RHEL  
> selinux:  
> policy:targeted  
> state:permissive  
> register:set\_selinux  
> register:task\_result  
> when:ansible\_distribution|lower =='redhat'  
> -name:SetSELinuxto permissive mode |Debian  
> selinux:  
> policy:default  
> state:permissive  
> register:set\_selinux  
> register:task\_result  
> when:  
> -ansible\_selinux\_python\_present|bool  
> -ansible\_distribution|lower =='debian'  
> -name:Rebootthe server andwait forit to come back up.  
> reboot:  
> when:task\_result ischanged  
> ...

Hello Keith,

it is certainly possible to have only one task for setting the permissive mode by setting up the variable parts before  
that or use templating in the task arguments.

E.g. something like

selinux\_details:  
&nbsp;&nbsp;redhat:  
&nbsp;&nbsp;&nbsp;&nbsp;policy: targeted  
&nbsp;&nbsp;debian:  
&nbsp;&nbsp;&nbsp;&nbsp;policy: default

And in the task:

&nbsp;&nbsp;policy: "{{ selinux\_details[ansible\_distribution|lower] }}"

Disclaimer: not tested

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke
