下面是我的带有shell模块的剧本:
注意:mypid变量是在目标主机上运行的进程的进程id。
- shell: |
if [ -z {{tm}} ]; then mypid="wrongpid"; else mypid= {{tm}}; fi;
if [ ! -e /proc/$mypid/status ]; then exit 1; fi
istat {{fn}} | grep -i Modif | cut -d' ' -f3,4,5,6
vars:
fn: "/proc/{{ mypid }}/status"
tm: "{{ mypid }}"
tags: always
ignore_errors: yes
register: starttime
这在Linux系统上运行良好。在AiX上,尽管这个外壳模块失败,并出现以下错误:
TASK [shell] *******************************************************************
[1;30mtask path: /app/playbook/startstop.yml:318[0m
[0;31mfatal: [10.9.9.131]: FAILED! => {"changed": true, "cmd": "if [ -z 12386652 ]; then mypid="wrongpid"; else mypid= 12386652; fi;nif [ ! -e /proc/$mypid/status ]; then exit 1; finistat /proc/12386652/status | grep -i Modif | cut -d' ' -f3,4,5,6n", "delta": "0:00:00.108227", "end": "2019-11-21 14:09:35.064768", "msg": "non-zero return code", "rc": 1, "start": "2019-11-21 14:09:34.956541", "stderr": "/bin/sh: 12386652: not found.", "stderr_lines": ["/bin/sh: 12386652: not found."], "stdout": "", "stdout_lines": []}[0m
[0;36m...ignoring[0m
我添加了以下shell参数,但错误仍然存在:
args:
executable: /bin/ksh
将shell作为shell脚本在目标服务器上运行也很好。不知道为什么是失败的答案。
请提出建议。
您的shell var赋值表达式中有一个额外的空间,导致sh尝试执行命令,而不是赋值var:
else mypid= {{tm}} => else mypid={{tm}}
此外,您应该稍微保护整个表达式,以确保不会出现其他意外(例如,使用空参数…(
# before
if [ -z {{tm}} ]; then mypid="wrongpid"; else mypid= {{tm}}; fi;
# after
if [ -z "{{tm}}" ]; then mypid="wrongpid"; else mypid="{{tm}}"; fi;