'rc' check failure - 'dict object' has no attribute 'rc'

3,706 views
Skip to first unread message

Imam Toufique

unread,
Jul 7, 2018, 3:14:37 AM7/7/18
to Ansible Project
Hi,

I am new in ansible world, trying to figure out playbook where I use 'grep' to check for netmask address from a text file and capture the exit code with 'register' ( see below).  


My vars/main.yml looks like this:

ipmitool_lan:
  root:
    netmask: 255.0.0.0
    defgw: 10.1.1.1


My tasks/main.yml looks like this:
---
####
- name: HP server netmask setup check
  command: grep -q {{ item.value.netmask }} /tmp/hponcfg.out
  register: ipmask
  with_dict: "{{ ipmitool_lan }}"
  ignore_errors: yes

#- debug: msg= "{{ ipmask.rc }}"

- block:
    - name: HP server setup netmask
      replace:
        path: "/tmp/hponcfg.out"
        regexp: >-
          (<SUBNET_MASK.VALUE.)(=)(.*\b)("\/>)
        replace: >-
          \1 \2 "{{ item.value.netmask }}\4
      with_dict: "{{ ipmitool_lan }}"

  when:
    - ansible_system_vendor == 'HP'
    - ipmask.rc != 0

My /tmp/hponcfg.out file looks like this: 

--------snip-------
<SPEED_AUTOSELECT VALUE = "Y"/>
    <NIC_SPEED VALUE = "10"/>
    <FULL_DUPLEX VALUE = "N"/>
    <IP_ADDRESS VALUE  = "10.3.254.221"/>
    <SUBNET_MASK VALUE = "255.255.0.0"/>
    <GATEWAY_IP_ADDRESS VALUE = "10.1.1.1"/>

-------snip-----------


when the above playbook is run, I get the following error:

TASK [ipmi : HP server setup netmask] ****************************************************************************
task path: /data/ansible-hpc/production/global/pkgs/20180703_2227/roles/ipmi/tasks/main.yml:14
fatal: [compute-13-11]: FAILED! => {
    "msg": "The conditional check 'ipmask.rc != 0' failed. The error was: error while evaluating conditional (ipmask.rc != 0): 'dict object' has no attribute 'rc'\n\nThe error appears to have been in '/data/ansible-hpc/production/global/pkgs/20180703_2227/roles/ipmi/tasks/main.yml': 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- block:\n    - name: HP server setup netmask\n      ^ here\n"

data structure of this run looks like this, where is clear that 'ipmask.rc != 0' 

 "item": {
        "key": "root", 
        "value": {
            "defgw": "10.1.1.1", 
            "netmask": "255.0.0.0"
        }
    }, 
    "msg": "non-zero return code", 
    "rc": 1, 
    "start": "2018-07-06 23:44:31.061066", 
    "stderr": "", 
    "stderr_lines": [], 
    "stdout": "", 
    "stdout_lines": []

It looks like "rc": value is '1' , doesn't it?  Well, I am no expert at this but at least that's what I can see.  

Not sure what I am doing wrong, I am out of ideas.  I need to get this resolved to move forward, so, any help will be greatly appreciated!

Daria Serkova

unread,
Jul 8, 2018, 11:43:54 AM7/8/18
to Ansible Project
Hi Imam Toufique

ipmask variable contains complex data structure, just try 
- debug: var="{{ ipmask }}"
to see it

so you need to use
when: ipmask.results[0].rc != 0
in your condition.

HIWH,
Daria

суббота, 7 июля 2018 г., 10:14:37 UTC+3 пользователь Imam Toufique написал:

Imam Toufique

unread,
Jul 8, 2018, 2:36:15 PM7/8/18
to Ansible Project
Hi Daria, 

that worked!  thanks.  But, I am not sure how it did though :-)  '-debug' output for ipmask produces the following:

TASK [ipmi : debug] **********************************************************************************************
task path: /data/ansible-hpc/production/global/pkgs/20180703_2227/roles/ipmi/tasks/main.yml:15
ok: [compute-13-11] => {
    "<type 'dict'>": "VARIABLE IS NOT DEFINED!: "
}

Would you mind explaining your solution a bit?

thanks again!  Very much appreciated!

Karl Auer

unread,
Jul 8, 2018, 7:22:13 PM7/8/18
to ansible...@googlegroups.com
When outputting a variable with debug, you do NOT use the curly braces.

If you use the curly braces, you are asking debug to output a variable with the name contained in ipmask, rather than the value of ipmask itself. Because the value contained in ipmask is not the name of a variable, debug complains that it is not defined.

Run this to see what I am talking about:

---
- hosts: localhost
  gather_facts: false
  become: false

  # fred contains a value, mary contains fred's NAME
  vars:
     fred: "123"
     mary: "fred"

  tasks:
     - debug:
         var: fred

     - debug:
         var: "{{ mary }}"


​S​
o
​ to output the value of ipmask, you need this​
:

- debug:
   var: ipmask

Regards, K.


--
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-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/2018a7c3-41b5-47ea-8d49-6a3582c47cf6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Karl Auer

Email  : ka...@2pisoftware.com
Website: 
http://2pisoftware.com

GPG/PGP : 958A 2647 6C44 D376 3D63 86A5 FFB2 20BC 0257 5816
Previous: F0AB 6C70 A49D 1927 6E05 81E7 AD95 268F 2AB6 40EA

Ramesh AR

unread,
Jul 8, 2018, 7:24:08 PM7/8/18
to ansible...@googlegroups.com
Does anyone know how to do vmotion all the vms  from one host to another hosts by using ansible.






To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.



--
Karl Auer

Email  : ka...@2pisoftware.com
Website: 
http://2pisoftware.com

GPG/PGP : 958A 2647 6C44 D376 3D63 86A5 FFB2 20BC 0257 5816
Previous: F0AB 6C70 A49D 1927 6E05 81E7 AD95 268F 2AB6 40EA

--
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 post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CA%2B%2BT08Q6LLjGnp_nwUpy8g9dS0Tb0W7TpfYQHs820knu7Qp4NA%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages