Your entire playbook could look like this:
---
- name: "Eliminamos dnsmasq y agregamos dns del endpoint de route53"
hosts: servers
vars:
package_names:
- dnsmasq
become: yes
gather_facts: no
tasks:
- name: "Borramos el paquete"
package:
name: "{{ package_names }}"
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
One addition note is that the last task appears to comment out every "
nameserver" line in /etc/resolv.conf including the ones you added in the
prior task. Is that what you intend?
--
Walter Rowe, Chief
Infrastructure Services
Office of Information Systems Management
National Institute of Standards and Technology
United States Department of Commerce
On Friday, August 12, 2022 at 2:11:57 PM UTC-4 Walter Rowe wrote:
> If you want to ensure a list of packages is absent, make a task that does
> only that.
>
> - name: "Borramos el paquete"
> package:
> name: "{{ package_names }}"
> state: absent
>
> Each Ansible task describes a desired state when the task is completed.
> You don't need to check whether the packages are installed. You only need
> to say you want them absent. If any (or all) of the packages are present,
> they will be removed, the task will report "changed", and the state will be
> changed. If none of the packages are present, the task will report "ok" and
> the state will be unchanged.
> --
> Walter Rowe, Chief
> Infrastructure Services
> Office of Information Systems Management
> National Institute of Standards and Technology
> United States Department of Commerce
> On Friday, August 12, 2022 at 2:02:13 PM UTC-4 [email protected]
> 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/f45da2d9-7f57-430f-8f84-60e064e134c7n%40googlegroups.com.