Ansible distribution when upgrading centos 6 dict object' has no attribute error

351 views
Skip to first unread message

Dave tackyon

unread,
Oct 14, 2022, 10:43:19 AM10/14/22
to Ansible Project
Please could someone give some help to this problem. I have I cant see anything wrong.
I have what looks like a bug in ansible_distribution. where it looks like its not getting picked up properly. Not sure how it obtains this information under the covers but it is returning correct info. Even although /etc/redhat-release has different content. to this
I'm running ansible 2.9.23

Ansible facts of the server I want to upgrade seem to be correct:

"ansible_distribution": "CentOS",

        "ansible_distribution_file_parsed": true,

        "ansible_distribution_file_path": "/etc/redhat-release",

        "ansible_distribution_file_variety": "RedHat",

        "ansible_distribution_major_version": "6",

        "ansible_distribution_release": "Final",

        "ansible_distribution_version": "6.10",

THE ERROR IS :

 FAILED! => {"msg": "The conditional check 'resultupdateallcentos.rc > 0 and ansible_distribution == \"CentOS\" or \"ALOS\" and ansible_distribution_major_version == \"6\"' failed. The error was: error while evaluating conditional (resultupdateallcentos.rc > 0 and ansible_distribution == \"CentOS\" or \"ALOS\" and ansible_distribution_major_version == \"6\"): 'dict object' has no attribute 'rc'\n\nThe error appears to be in '/tmp/d20221006-1291-1dvfink/project/playbook.yml': line 63, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: centos6logupdateissue\n ^ here\n"}

CODE excerpt is:

---
- name: Check the correct repos
  hosts: all
  vars:
    distribution_list:
      - ALOS
      - CentOS
      - OracleLinux
  tasks:
    - name: check it is a recognised distro
      fail:
      when: ansible_distribution not in distribution_list      

    - name: Centos6 Install yum-utils
      yum:
        name: "yum-utils"
        disablerepo: "*"
        enablerepo: "CentOS6_Base*"
        state: present
      register: resultyumutilscentos6
      when:
        ansible_distribution == "CentOS" and ansible_distribution_major_version == "6"

    - name: Centos7 Install yum-utils
      yum:
        name: "yum-utils"
        disablerepo: "*"
        enablerepo: "CentOS7_Base*"
        state: present
      register: resultyumutilscentos7
      when:
        ansible_distribution == "CentOS" and ansible_distribution_major_version == "7"

    - name: Centos6 update
      yum:
        name: "*"
        disablerepo: "*"
        enablerepo: "CentOS6_Base*"
        state: latest
      register: resultupdateallcentos
      ignore_errors: true
      when:
        ansible_distribution == "CentOS" and ansible_distribution_major_version == "6"
      notify: check for need to reboot

    - name: Centos7 update
      yum:
        #name: "xxsxxcreen"
        #name: "screen"
        name: "*"
        disablerepo: "*"
        enablerepo: "CentOS7_Base*"
        state: latest
      register: resultupdateallcentos
      ignore_errors: true
      when:
        ansible_distribution == "CentOS" and ansible_distribution_major_version == "7"
      notify: check for need to reboot

    - name: centos6logupdateissue
      command: logger -t GIS_ALERT_P3 -p local1.info "Subject=Error with yum update via autopatch; Body=See foreman server for more details"
      when: resultupdateallcentos.rc > 0 and ansible_distribution == "CentOS" and ansible_distribution_major_version == "6"

    - name: centos7logupdateissue
      command: logger -t GIS_ALERT_P3 -p local1.info "Subject=Error with yum update via  autopatch; Body=See foreman server for more details"
      when: resultupdateallcentos.rc > 0 and ansible_distribution == "CentOS" and ansible_distribution_major_version == "7"

    - name: centos6showtask # As the update has ignore_errors so that above logger can run, need to ensure entire job is marked as failed if rc >0
      debug: msg="{{ resultupdateallcentos }}"
      failed_when: resultupdateallcentos.rc > 0
      when: ansible_distribution == "CentOS" and ansible_distribution_major_version == "6"

    - name: centos7showtask # As the update has ignore_errors so that above logger can run, need to ensure entire job is marked as failed if rc >0
      debug: msg="{{ resultupdateallcentos }}"
      failed_when: resultupdateallcentos.rc > 0
      when: ansible_distribution == "CentOS" and ansible_distribution_major_version == "7"

  handlers:
    - name: check for need to reboot
      command: needs-restarting -r
      register: result
      ignore_errors: yes
      notify:  Alert on reboot from syslog
      failed_when: result.rc > 1  # rc 1 means rebooted needed, not that the command failed
    - name: Alert on reboot from syslog
      command: logger -p local1.info "System rebooted by  autopatch"
      when: result.rc == 1
      notify: Reboot client if required
    - name: Reboot client if required
      reboot:
      when: result.rc == 1 # rc 1 = needs reboot

Rowe, Walter P. (Fed)

unread,
Oct 14, 2022, 12:47:10 PM10/14/22
to ansible...@googlegroups.com
These two tasks are registering their result to the same variable. Even if a task condition isn't true the register variable is set.

    - name: Centos6 update
      yum: 
        name: "*"
        disablerepo: "*"
        enablerepo: "CentOS6_Base*"
        state: latest
      register: resultupdateallcentos
      ignore_errors: true
      when:
        ansible_distribution == "CentOS" and ansible_distribution_major_version == "6"
      notify: check for need to reboot

    - name: Centos7 update
      yum: 
        #name: "xxsxxcreen"
        #name: "screen"
        name: "*"
        disablerepo: "*"
        enablerepo: "CentOS7_Base*"
        state: latest
      register: resultupdateallcentos
      ignore_errors: true
      when:
        ansible_distribution == "CentOS" and ansible_distribution_major_version == "7"
      notify: check for need to reboot

At least that has been my experience. The CentOS 7 task likely is overwriting the registered result of the CentOS 6 task.

Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

--
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/c9cd6a5d-4038-407e-bc6c-a45e76e9992cn%40googlegroups.com.

Message has been deleted

Dave Tackyon

unread,
Oct 19, 2022, 5:44:53 AM10/19/22
to Ansible Project
Hi I made the changes as suggested but Is still failing. any ideas ?

PLAY [Check the correct repos] *************************************************
   3:

   4:
TASK [Gathering Facts] *********************************************************
   5:
[WARNING]: Platform linux on host *hostname* is using the
   6:
discovered Python interpreter at /usr/bin/python, but future installation of
   7:
another Python interpreter could change this. See https://docs.ansible.com/ansi
   8:
ble/2.9/reference_appendices/interpreter_discovery.html for more information.
   9:
ok: [*hostname*]
  10:

  11:
TASK [check it is a recognised distro] *****************************************
  12:
skipping: [*hostname*]
  13:

  14:
TASK [Centos6 Install yum-utils] ***********************************************
  15:
skipping: [*hostname*]
  16:

  17:
TASK [Centos7 Install yum-utils] ***********************************************
  18:
skipping: [*hostname*]
  19:

  20:
TASK [Centos6 update] **********************************************************
  21:
skipping: [*hostname*]
  22:

  23:
TASK [Centos7 update] **********************************************************
  24:
skipping: [*hostname*]
  25:

  26:
TASK [centos6logupdateissue] ***************************************************
  27:
fatal: [*hostname*]: FAILED! => {"msg": "The conditional check 'resultupdateallcentos6.rc > 0 and ansible_distribution == \"CentOS\" and ansible_distribution_major_version == \"6\"' failed. The error was: error while evaluating conditional (resultupdateallcentos6.rc > 0 and ansible_distribution == \"CentOS\" and ansible_distribution_major_version == \"6\"): 'dict object' has no attribute 'rc'\n\nThe error appears to be in '/tmp/d20221019-10194-1tbi3t/project/playbook.yml': line 63, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - name: centos6logupdateissue\n      ^ here\n"}
  28:
PLAY RECAP *********************************************************************
  29:
*hostname* : ok=1    changed=0    unreachable=0    failed=1    skipped=5    rescued=0    ignored=0
  30:
Exit status: 2

      register: resultupdateallcentos6

      ignore_errors: true
      when:
        ansible_distribution == "CentOS" and ansible_distribution_major_version == "6"
      notify: check for need to reboot

    - name: Centos7 update
      yum:
        #name: "xxsxxcreen"
        #name: "screen"
        name: "*"
        disablerepo: "*"
        enablerepo: "CentOS7_Base*"
        state: latest
      register: resultupdateallcentos7

      ignore_errors: true
      when:
        ansible_distribution == "CentOS" and ansible_distribution_major_version == "7"
      notify: check for need to reboot

    - name: centos6logupdateissue
      command: logger -t GIS_ALERT_P3 -p local1.info "Subject=Error with yum update via Keyloop autopatch; Body=See foreman server for more details"
      when: resultupdateallcentos6.rc > 0 and ansible_distribution == "CentOS" and ansible_distribution_major_version == "6"

    - name: centos7logupdateissue
      command: logger -t GIS_ALERT_P3 -p local1.info "Subject=Error with yum update via Keyloop autopatch; Body=See foreman server for more details"
      when: resultupdateallcentos7.rc > 0 and ansible_distribution == "CentOS" and ansible_distribution_major_version == "7"


    - name: centos6showtask # As the update has ignore_errors so that above logger can run, need to ensure entire job is marked as failed if rc >0
      debug: msg="{{ resultupdateallcentos6 }}"
      failed_when: resultupdateallcentos6.rc > 0

      when: ansible_distribution == "CentOS" and ansible_distribution_major_version == "6"

    - name: centos7showtask # As the update has ignore_errors so that above logger can run, need to ensure entire job is marked as failed if rc >0
      debug: msg="{{ resultupdateallcentos7 }}"
      failed_when: resultupdateallcentos7.rc > 0

      when: ansible_distribution == "CentOS" and ansible_distribution_major_version == "7"

  handlers:
    - name: check for need to reboot
      command: needs-restarting -r
      register: result
      ignore_errors: yes
      notify:  Alert on reboot from syslog
      failed_when: result.rc > 1  # rc 1 means rebooted needed, not that the command failed
    - name: Alert on reboot from syslog
      command: logger -p local1.info "System rebooted by Keyloop autopatch"

      when: result.rc == 1
      notify: Reboot client if required
    - name: Reboot client if required
      reboot:
      when: result.rc == 1 # rc 1 = needs reboot

Rowe, Walter P. (Fed)

unread,
Oct 19, 2022, 7:25:39 AM10/19/22
to ansible...@googlegroups.com
Your error message tells you what is wrong - 'dict object' has no attribute 'rc'

Place a debug statement before the failing task and display resultupdateallcentos6.

- debug: var=resultupdateallcentos6

Look at the output to see what is returned. Also note that ALL of you prior tasks are "skipping". Maybe there is no 'rc' attribute because they are not executing. Figure out why they are all skipping. That might tell you why you have no 'rc' attribute.

Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123
Reply all
Reply to author
Forward
0 new messages