So, your overall playbook should be like.

---
- name: "Eliminamos dnsmasq y agregamos dns del endpoint de route53"
 hosts: servers
 vars:
   package_names:
     - dnsmasq
 become: yes
 gather_facts: no
 tasks:
   - name: "Verificamos si el paquete esta instalado"
         shell:
           cmd: |
            rpm -q "{{ item }}"
         with_items: "{{ package_names }}"
         register: package_check
         
   - block:
       - name: "Borramos el paquete"
         yum:
           name: "{{ item }}"
           state: absent
         with_items: "{{ package_names }}"
       - name: "Agregamos IPs de los nuevos DNS de route53"
         lineinfile:
           path: /etc/resolv.conf
           backup: yes
           state: present
           line: "{{ item }}"
         with_items:
           - 'nameserver 10.54.130.237'
           - 'nameserver 10.54.131.106'
           - 'nameserver 169.254.169.253'
       - name: "Comentamos el primer nameserver ya que no usamos mas"
         shell:
           cmd: |
            sed -i '/nameserver/{s/.*/#&/;:A;n;bA}' /etc/resolv.conf
     when: package_check is succeeded

On Friday, August 12, 2022 at 11:02:13 PM UTC+5 farrukh ahmed wrote:

> Firstly, you have defined the play that is verifying the packages, which 
> needs to be outside the block:
>
> - name: "Verificamos si el paquete esta instalado"
>          shell:
>            cmd: |
>             rpm -q "{{ item }}"
>          with_items: "{{ package_names }}"
>          register: package_check
>
> So, it should execute before the *block*, save the output to the register 
> variable that is *package_check. *And then check the condition for the 
> block to execute *when: package_check is succeeded*
>
> Second, Inside the block on this play, you need to define the with_items 
> on this play, to tell it which packages to remove.
>
> - name: "Borramos el paquete"
>          yum:
>            name: "{{ item }}"
>            state: absent
>          with_items: "{{ package_names }}"
>
> On Friday, August 12, 2022 at 7:38:25 PM UTC+5 [email protected] wrote:
>
>> Additionally, you're looping over package_names.  I think you may need to 
>> instead pull this out to another task_list and include_tasks while looping 
>> instead.
>>
>> On Fri, Aug 12, 2022 at 9:35 AM Paul Manno <[email protected]> wrote:
>>
>>> You need to move this out of the block
>>>        - name: "Verificamos si el paquete esta instalado"
>>>          shell:
>>>            cmd: |
>>>             rpm -q "{{ item }}"
>>>          with_items: "{{ package_names }}"
>>>          register: package_check
>>>
>>> On Fri, Aug 12, 2022 at 9:29 AM SysAdmin EM <[email protected]> wrote:
>>>
>>>> Hi, i create a playbook to remove a yum package:
>>>>
>>>> --- 
>>>> - name: "Eliminamos dnsmasq y agregamos dns del endpoint de route53" 
>>>>  hosts: servers
>>>>  vars: 
>>>>    package_names: 
>>>>      - dnsmasq 
>>>>  become: yes 
>>>>  gather_facts: no 
>>>>  tasks: 
>>>>    - block: 
>>>>        - name: "Verificamos si el paquete esta instalado" 
>>>>          shell: 
>>>>            cmd: | 
>>>>             rpm -q "{{ item }}" 
>>>>          with_items: "{{ package_names }}" 
>>>>          register: package_check 
>>>>        - name: "Borramos el paquete" 
>>>>          yum: 
>>>>            name: "{{ item }}" 
>>>>            state: absent 
>>>>        - name: "Agregamos IPs de los nuevos DNS de route53" 
>>>>          lineinfile: 
>>>>            path: /etc/resolv.conf 
>>>>            backup: yes 
>>>>            state: present 
>>>>            line: "{{ item }}" 
>>>>          with_items: 
>>>>            - 'nameserver 10.54.130.237' 
>>>>            - 'nameserver 10.54.131.106' 
>>>>            - 'nameserver 169.254.169.253' 
>>>>        - name: "Comentamos el primer nameserver ya que no usamos mas" 
>>>>          shell: 
>>>>            cmd: | 
>>>>             sed -i '/nameserver/{s/.*/#&/;:A;n;bA}' /etc/resolv.conf 
>>>>      when: package_check is succeeded
>>>>
>>>> i see this error:
>>>>
>>>> FAILED! => {"msg": "The conditional check 'package_check is succeeded' 
>>>> failed. The error was: The 'failed' test expects a dictionary\n\nThe error 
>>>> appears to be in '/etc/ansible/mod_replace/playbook/comment_inser.yaml': 
>>>> line 11, column 11, but may\nbe elsewhere in the file depending on the 
>>>> exact syntax problem.\n\nThe offending line appears to be:\n\n    - 
>>>> block:\n        - name: \"Verificamos si el paquete esta instalado\"\n     
>>>>      ^ here\n"}
>>>>
>>>> any helps?
>>>>
>>>> -- 
>>>> 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/CAGUDtnmnbfGuOjgUbTqm3Oa358Z%2B1htBoE6HyyEj2uB8yHXhmw%40mail.gmail.com
>>>>  
>>>> <https://groups.google.com/d/msgid/ansible-project/CAGUDtnmnbfGuOjgUbTqm3Oa358Z%2B1htBoE6HyyEj2uB8yHXhmw%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>>> .
>>>>
>>>

-- 
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/d6e0d5e1-6dc7-4354-928f-f0a08db7931en%40googlegroups.com.

Reply via email to