在ansible条件中评估返回码 [英] Evaluating return code in ansible conditional

查看:127
本文介绍了在ansible条件中评估返回码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在自动执行一项任务,该任务需要将最新版本的软件附加到文件中.我不想针对同一版本多次执行此操作.

I'm working on automating a task which needs to append the latest version of software to a file. I don't want to it do this multiple times for the same version.

它查看以下示例文件:

var software releases = new Array(
    "4.3.0",
    "4.4.0",
    "4.5.0",
    "4.7.0",
    "4.8.0",
    "4.11.0",
    "4.12.1",
    "4.14.0",
    "4.15.0",
    "4.16.0",
);

默认的main.yml会传递类似的信息

the defaults main.yml would pass in something like

VERSION: 4.16.2

代码

- name: register version check
  shell: cat /root/versions.js | grep -q {{VERSION}}
  register: current_version

- debug: msg="The registered variable output is {{ current_version.rc }}"

- name: append to versions.js
  lineinfile:
    dest: /root/versions.js
    regexp: '^\);'
    insertbefore: '^#\);'
    line: "    \"{{VERSION}}\",\n);"
    owner: root
    state: present
    when: current_version.rc == 1

问题:调试消息正在评估current_version.rc并根据grep命令输出向我显示布尔值,但我不能在when条件下重复使用此值来确定是否应运行任务.

problem: the debug message is evaluating current_version.rc and showing me boolean values based on the grep commands output, but I cant re-use this in the when conditional to determine if the task should be run.

输出:

PLAY [localhost] **************************************************************

GATHERING FACTS ***************************************************************
ok: [localhost]

TASK: [test | register version check] *****************************************
failed: [localhost] => {"changed": true, "cmd": "cat /root/versions.js | grep -q 3.19.2", "delta": "0:00:00.003570", "end": "2015-12-17 00:24:49.729078", "rc": 1, "start": "2015-12-17 00:24:49.725508", "warnings": []}

FATAL: all hosts have already failed -- aborting

PLAY RECAP ********************************************************************
           to retry, use: --limit @/root/site.retry

localhost                  : ok=1    changed=0    unreachable=0    failed=1

推荐答案

nikobelia 所述,<当href ="http://www.gnu.org/software/grep/manual/html_node/Exit-Status.html" rel ="noreferrer"> grep不匹配时,返回退出代码1行.然后,Ansible将此错误(实际上是外壳程序/命令任务中的状态代码,除了0以外的任何状态代码)解释为错误,因此立即失败.

As nikobelia pointed out in the comments, grep returns an exit code of 1 when it doesn't match any lines. Ansible then interprets this (actually any status code other than 0 from a shell/command task) as an error and so promptly fails.

您可以使用 ignore_errors .虽然使用grep会忽略实际错误(返回码为2),所以您可能想使用

You can tell Ansible to ignore the response code from the shell/command task by using ignore_errors. Although with grep this will ignore actual errors (given by a return code of 2) so instead you might want to use failed_when like this:

- name: register version check
  shell: cat /root/versions.js | grep -q {{VERSION}}
  register: current_version
  failed_when: current_version.rc == 2

这篇关于在ansible条件中评估返回码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆