1

I am using uri module of Ansible 2.0.

I have code like this to make POST on a api.

  - name: Example POST
      uri:
        url: http://example.com:8001/api
        method: POST
        status_code: 201
        body_format: json
        body:
          key1: value
          key2: value2
      register: response
      changed_when: response.status == 201

I have 2 target servers in different inventories. This works againts server A. On server B is fails with error:

fatal: [ip]: FAILED! =>
{"failed": true, "msg": "ERROR! The conditional check 'response.status == 201' failed. 
The error was: ERROR! error while evaluating conditional (response.status == 201):
ERROR! 'dict object' has no attribute 'status'"}

If I do the POST manually with curl both server A and B give same response. Servers A and B have identical configuration.

If I run the play book with full verbosity I see only these lines before the error for the task in question:

TASK [role : Example POST] ***************************************
task path: /home/ansible/roles/role/tasks/example.yaml:15
<ip> ESTABLISH SSH CONNECTION FOR USER: ansible
<ip> SSH: EXEC ssh -C -vvv -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ansiblr -o ConnectTimeout=10 -o ControlPath=/home/ansible/.ansible/cp/ansible-ssh-%h-%p-%r 10.100.105.22 '/bin/sh -c '"'"'sudo -H -S -n -u root /bin/sh -c '"'"'"'"'"'"'"'"'echo BECOME-SUCCESS-cvyeydaqaapnnwuaukxffwmgvinzoxaq; LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python'"'"'"'"'"'"'"'"''"'"'' 

I can't figure out why this test fails for Ansible and why it happens only for the other server.

Madoc Comadrin
  • 570
  • 4
  • 11
  • 29
  • 2
    `ERROR! 'dict object' has no attribute 'status'` -- so what does the `response` dict contain? send it to `debug` and view the contents to find out why it doesn't have a `.status`. – jscott Aug 31 '17 at 12:08
  • 1
    That showed the problem. I was wrong when I thought the environments were identical. Got this message `httplib2 >= 0.7 is not installed`. Installing python-httplib2 solved the problem. So the problem was missing package instead of Ansible. – Madoc Comadrin Aug 31 '17 at 12:18
  • Great, glad you resolved it. Be sure to post your results as an answer so you may mark it as accepted. – jscott Aug 31 '17 at 12:19

1 Answers1

2

As suggested in the comment I used debug module to see the variable:

- debug:
    msg: '{{ response }}'

Content of variable was httplib2 >= 0.7 is not installed.

So my environment was missing package that is required by uri module. According to docs this is only needed for Ansible versions older than 2.1.

Installing package python-httplib2solved the problem.

Madoc Comadrin
  • 570
  • 4
  • 11
  • 29