根据Ansible中的ls输出创建符号链接

时间:2020-05-08 00:24:23

标签: bash ansible

我正在尝试将现有的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 directory\nls: cannot access lsb|head: No such file or directory\nls: 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
    }
}

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:2)

command模块doesn't run commands through the 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}}"