无法根据条件将默认值分配给Ansible注册变量 [英] Unable to assign default value to Ansible registered variable based on condition

查看:86
本文介绍了无法根据条件将默认值分配给Ansible注册变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用ansible shell模块命令获取修改日期

I try to get modification date using ansible shell module command

stat /proc/1178/stat | grep Modify  | cut -d' ' -f2,3

注意:shell的输出(即starttime.rc)将始终为true,即无论是否由于命令中的pipe cut -d而返回输出,都为0.

Note: the output of the shell i.e starttime.rc will always be true i.e 0 whether an output is returned or not because of pipe cut -d in the command.

我希望显示时间,即shell模块返回输出后的结果,否则显示"Server NOT RUNNING".

I wish to display the time i.e the result of shell module if it returns output else display "Server NOT RUNNING".

这是我的剧本:

- hosts: test_test
  any_errors_fatal: true
  user: user1
  gather_facts: false
  tasks:
    - shell: "stat /proc/1178/stat | grep Modify  | cut -d' ' -f2,3"
      register: starttime

    - name: Status of Server
      set_fact:
        starttime: "{{ starttime | default('Server NOT RUNNING') }}"

    - debug:
        msg: "STARTTIME:{{ starttime }}"

下面是我没有得到预期结果的详细输出.

Below is the verbose output where I'm not getting the expected results.

TASK [shell] ************************************************************************************************************************************************
changed: [10.9.9.111] => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "cmd": "stat /proc/1178/stat | grep Modify  | cut -d' ' -f2,3",
    "delta": "0:00:00.118151",
    "end": "2019-11-08 10:46:28.345448",
    "invocation": {
        "module_args": {
            "_raw_params": "stat /proc/1178/stat | grep Modify  | cut -d' ' -f2,3",
            "_uses_shell": true,
            "argv": null,
            "chdir": null,
            "creates": null,
            "executable": null,
            "removes": null,
            "stdin": null,
            "stdin_add_newline": true,
            "strip_empty_ends": true,
            "warn": true
        }
    },
    "rc": 0,
    "start": "2019-11-08 10:46:28.227297",
    "stderr": "stat: cannot stat â/proc/1178/statâ: No such file or directory",
    "stderr_lines": [
        "stat: cannot stat â/proc/1178/statâ: No such file or directory"
    ],
    "stdout": "",
    "stdout_lines": []
}

TASK [Status of Server] ****************************************************************************************************************************
task path: /app/script/condition_test.yml:14
ok: [10.9.9.111] => {
    "ansible_facts": {
        "starttime": {
            "ansible_facts": {
                "discovered_interpreter_python": "/usr/bin/python"
            },
            "changed": true,
            "cmd": "stat /proc/1178/stat | grep Modify  | cut -d' ' -f2,3",
            "delta": "0:00:00.118151",
            "end": "2019-11-08 10:46:28.345448",
            "failed": false,
            "rc": 0,
            "start": "2019-11-08 10:46:28.227297",
            "stderr": "stat: cannot stat â/proc/1178/statâ: No such file or directory",
            "stderr_lines": [
                "stat: cannot stat â/proc/1178/statâ: No such file or directory"
            ],
            "stdout": "",
            "stdout_lines": []
        }
    },
    "changed": false
}

TASK [debug] ************************************************************************************************************************************************
task path: /app/script/condition_test.yml:19
ok: [10.9.9.111] => {
    "msg": "STARTTIME:{'stderr_lines': [u'stat: cannot stat \\u2018/proc/1178/stat\\u2019: No such file or directory'], u'changed': True, u'end': u'2019-11-08 10:46:28.345448', u'stdout': u'', u'cmd': u\"stat /proc/1178/stat | grep Modify  | cut -d' ' -f2,3\", u'rc': 0, u'start': u'2019-11-08 10:46:28.227297', 'failed': False, u'stderr': u'stat: cannot stat \\u2018/proc/1178/stat\\u2019: No such file or directory', u'delta': u'0:00:00.118151', 'stdout_lines': [], 'ansible_facts': {u'discovered_interpreter_python': u'/usr/bin/python'}}"
}
META: ran handlers
META: ran handlers

PLAY RECAP **************************************************************************************************************************************************
10.9.9.111                  : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

您能建议我如何处理吗?

Can you please suggest how can I handle this ?

推荐答案

注意:shell的输出(即starttime.rc)将始终为true,即无论是否由于命令中的pipe cut -d而返回输出,都为0.

Note: the output of the shell i.e starttime.rc will always be true i.e 0 whether an output is returned or not because of pipe cut -d in the command.

使用set -o pipefail可以轻松地规避这一点(可能需要更新您的shell:以使用bash或其他现代"外壳)

One can easily circumvent that with set -o pipefail (which may require updating your shell: to use bash, or another "modern" shell)

- shell: "set -o pipefail; stat /proc/1178/stat | grep Modify  | cut -d' ' -f2,3"
  register: starttime

另一种完全合理的方法是实际上测试该文件是否存在:

Another perfectly reasonable approach would be to actually test that the file exists:

- shell: |
    if [ ! -e {{fn}} ]; then exit 1; fi
    stat {{fn}} | grep Modify | cut -d' ' -f2,3
  vars:
    fn: /proc/1178/stat
  register: starttime

这篇关于无法根据条件将默认值分配给Ansible注册变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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