我正在尝试将现有的bash代码片段转换为Ansible,但在实现它时遇到了一些挑战。
这个脚本的作用是什么?
它首先检查它是否是SUSE机器。如果为true,则将cd
设置为/lib
,并运行复杂的ls
命令。如果有输出,则使用ln
创建符号链接。
这是我的bash代码片段:
##addlink ld-lsb.so.3->ld-2.11.1.so in /lib on sles11.x,12.x for lmutil in cct2000739233
if [ -f /etc/SuSE-release ]; then
cd /lib
ldso=`ls ld-*.so|grep -v lsb|head -n 1`
if [ -e $ldso -a ! -h ld-lsb.so.3 -a ! -f ld-lsb.so.3 -a "$ldso" != "" ]; then
ln -sf $ldso ld-lsb.so.3
fi
cd /lib64
if [ ! -e ld-lsb-x86-64.so.3 ]; then
ln -sf ld-linux-x86-64.so.2 ld-lsb-x86-64.so.3
fi
fi
以下是我迄今为止所尝试的:
- name: addlink ld-lsb.so.3->ld-2.11.1.so in /lib on sles11.x,12.x for lmutil
command: "ls ld-*.so|grep -v lsb|head -n 1"
args:
chdir: /lib
register: ldso
# file:
# src: ldso
# dest: /lib/ld-lsb.so.3
# state: link
when:
- ansible_distribution == 'SLES'
- debug: msg="{{ldso}}"
错误消息:
TASK [qsc/hack/v1 : addlink ld-lsb.so.3->ld-2.11.1.so in /lib on sles11.x,12.x for lmutil] ********************************************
fatal: [ansible-poc-rhel6]: FAILED! => {"changed": true, "cmd": ["ls", "ld-*.so|grep", "-v", "lsb|head", "-n", "1"], "delta": "0:00:00.005374", "end": "2020-05-07 17:16:04.042633", "msg": "non-zero return code", "rc": 2, "start": "2020-05-07 17:16:04.037259", "stderr": "ls: cannot access ld-*.so|grep: No such file or directorynls: cannot access lsb|head: No such file or directorynls: cannot access 1: No such file or directory", "stderr_lines": ["ls: cannot access ld-*.so|grep: No such file or directory", "ls: cannot access lsb|head: No such file or directory", "ls: cannot access 1: No such file or directory"], "stdout": "", "stdout_lines": []}
TASK [qsc/hack/v1 : debug] ************************************************************************************************************
ok: [ansible-poc-cos6] => {
"msg": {
"changed": false,
"skip_reason": "Conditional result was False",
"skipped": true
}
}
ok: [ansible-poc-centos7] => {
"msg": {
"changed": false,
"skip_reason": "Conditional result was False",
"skipped": true
}
}
任何帮助都将不胜感激!
command
模块不通过shell运行命令您可以使用shell模块。这看起来像
- name: addlink ld-lsb.so.3->ld-2.11.1.so in /lib on sles11.x,12.x for lmutil
shell: "ls ld-*.so|grep -v lsb|head -n 1"
args:
chdir: /lib
register: ldso
# file:
# src: ldso
# dest: /lib/ld-lsb.so.3
# state: link
when:
- ansible_distribution == 'SLES'
- debug: msg="{{ldso}}"
我要感谢@MCI为我指明了正确的方向。这是符合我要求的完整工作解决方案。
- name: Addlink ld-lsb.so.3->ld-2.11.1.so in /lib on sles11.x,12.x for lmutil
shell: "ls ld-*.so|grep -v lsb|head -n 1"
args:
chdir: /lib
register: ldso
- stat:
path: /lib64/ld-lsb-x86-64.so.3
register: lib64_result
- stat:
path: /lib/ld-lsb.so.3
register: lib_result
- block:
- file:
src: "/lib/{{ ldso.stdout }}"
dest: /lib/ld-lsb.so.3
state: link
force: true
- file:
src: /lib64/ld-linux-x86-64.so.2
dest: /lib64/ld-lsb-x86-64.so.3
state: link
force: true
when:
- ansible_distribution == 'SLES'
# - ansible_distribution == 'CentOS'
- not lib64_result.stat.exists|bool
- not lib_result.stat.exists|bool
- ldso.stdout != ''
- debug:
msg: "{{ ldso.stdout }}"