大网数据广告活动
服务器运维问答

Ansible uri modules changed_when check fails

· 52CE 我爱测

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 registe…

问题原文

原帖链接 · Madoc Comadrin · CC BY-SA 3.0

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 · CC BY-SA 3.0

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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-08-31)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。