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.
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’>
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’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:
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
apt:
name: ['policycoreutils', 'checkpolicy', 'selinux-basics',
'python-selinux' ]
state: present
when: ansible_distribution|lower == 'debian'
- name: Set SELinux to permissive mode | RHEL
selinux:
policy: targeted
state: permissive
register: set_selinux
register: task_result
when: ansible_distribution|lower == 'redhat'
- name: Set SELinux to 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: Reboot the server and wait for it to come back up.
reboot:
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.
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.