conditional error in playbook

301 views
Skip to first unread message

SysAdmin EM

unread,
Aug 12, 2022, 10:29:50 AM8/12/22
to ansible...@googlegroups.com
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?

Paul Manno

unread,
Aug 12, 2022, 10:36:27 AM8/12/22
to ansible...@googlegroups.com
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
--
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 ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAGUDtnmnbfGuOjgUbTqm3Oa358Z%2B1htBoE6HyyEj2uB8yHXhmw%40mail.gmail.com.

Paul Manno

unread,
Aug 12, 2022, 10:38:25 AM8/12/22
to ansible...@googlegroups.com
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.

farrukh ahmed

unread,
Aug 12, 2022, 2:02:13 PM8/12/22
to Ansible Project
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 }}"

farrukh ahmed

unread,
Aug 12, 2022, 2:06:19 PM8/12/22
to Ansible Project
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

Walter Rowe

unread,
Aug 12, 2022, 2:11:57 PM8/12/22
to Ansible Project
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

Walter Rowe

unread,
Aug 12, 2022, 2:36:13 PM8/12/22
to Ansible Project
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
Reply all
Reply to author
Forward
0 new messages