Your "Restarting services services¨ task should be looping over the
registered _results array, not {{ sysservices }}. Your sysservices has no
"rc". _result should have a list, one item per loop of the registering
task. It's that list you want to process.
On Wednesday, October 27, 2021 at 1:48:27 PM UTC-4 [email protected]
wrote:
> Hi guys,
>
> I'm trying to create a lab file to restart a service only if it's running.
> I have troubles when using variables, register and loop together.
>
>
> - name: Restart a service only if it's active.
> hosts: LAB
> vars:
> sysservices:
> - serviceName: httpd
> - serviceName: smb
> tasks:
> - name: Checking if service is running
> command: /usr/bin/systemctl is-active "{{ item.serviceName }}"
> register: _result
> ignore_errors: yes
> loop: "{{ sysservices }}"
> - name: Restarting services services
> service:
> name: "{{ item.serviceName }}"
> state: restarted
> when: item.rc == 0 * ## *I tried: item.rc == 0 , item.stdout ==
> "active", _result.rc == 0, _result.stdout == "active". All of
> them didn't work
> loop: "{{ sysservices }}"
> # - debug:
> # msg: "Valor de result {{ _result }}"
>
>
> ################
>
> fatal: [ansible1]: FAILED! => {"msg": "The conditional check '_result.rc
> == 0' failed. The error was: error while evaluating conditional (_result.rc
> == 0): 'dict object' has no attribute 'rc'\n\nThe error appears to be in
> '/home/ansible/deploy/conditionals_restart_service_NOT_WORKING': line 14,
> column 7, but may\nbe elsewhere in the file depending on the exact syntax
> problem.\n\nThe offending line appears to be:\n\n loop: \"{{
> sysservices }}\"\n - name: Restarting services services\n ^ here\n"}
>
>
>
> Could you please help me ?
>
> Many thanks!
>
--
You received this message because you are subscribed to the Google Groups
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/6700870b-5988-4097-895f-227887330433n%40googlegroups.com.