【问题标题】:Shell "|&" syntax not recognized in ansible在ansible中无法识别Shell“|&”语法
【发布时间】:2019-10-22 05:19:38
【问题描述】:

我在 ansible playbook 中运行一个 shell 命令,我想在其中存储输出的最后一行但出现以下错误:

致命:[网络服务器]:失败! => {"更改": true, "cmd": "./hash.sh -p admin123 |& tail -1", "delta": "0:00:00.002563", "end ": "2019-06-06 16:25:56.597951", "failed": true, "msg": "非零返回码", "rc": 2, "start": "2019-06-06 16 :25:56.595388", "stderr": "/bin/sh: 1: 语法错误: \"&\" 意外", "stderr_lines": ["/bin/sh: 1: 语法错误: \"&\"意外”],“stdout”:“”,“stdout_lines”:[]}

ma​​in.yaml

- name: Get hash value of admin password
  shell: "./hash.sh -p {{admin_password}} |& tail -1"
  args:
    chdir: /usr/share/tools
  register: admin_password_hash

当我直接在远程机器上运行该命令 (./hash.sh -p admin123 |& tail -1) 时,它会给出正确的输出。

【问题讨论】:

  • |& 无效 /bin/sh 语法。

标签: shell ansible


【解决方案1】:

假设您的意思是 |& 通过管道重定向 stderr,则符合 POSIX 的 cmd1 |& cmd2 替代方案(因此,与 /bin/sh 一起使用的方式)是 cmd1 2>&1 | cmd2:

- name: "Get hash value of admin password"
  shell: "./hash.sh -p {{admin_password | quote}} 2>&1 | tail -1"
  args:
    chdir: /usr/share/tools
  register: admin_password_hash

| quote 很重要:如果没有它,您可能会出现重大安全问题,因为选择$(rm -rf /*) 作为密码的人可能会导致您的整个系统被擦除。 (我敢说这是Bobby Tables 使用的密码!)

【讨论】:

    猜你喜欢
    • 2020-09-06
    • 1970-01-01
    • 1970-01-01
    • 2018-03-23
    • 1970-01-01
    • 1970-01-01
    • 2018-03-13
    • 2019-09-23
    • 1970-01-01
    相关资源
    最近更新 更多