无法根据条件为Ansible注册变量分配默认值

问题描述 投票:0回答:1

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

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

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

我希望显示时间,即外壳模块返回输出的结果,否则显示“服务器未运行”。

这是我的剧本:

- 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 }}"

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

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

您能建议我如何处理吗?

variables ansible conditional-statements default-value
1个回答
1
投票

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

[一个人可以很容易地用set -o pipefail来规避这一点(这可能需要更新您的shell:以使用bash或另一个“现代”外壳)]]

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

另一种完全合理的方法是实际上测试

该文件是否存在:
- shell: |
    if [ ! -e {{fn}} ]; then exit 1; fi
    stat {{fn}} | grep Modify | cut -d' ' -f2,3
  vars:
    fn: /proc/1178/stat
  register: starttime
© www.soinside.com 2019 - 2024. All rights reserved.