基于Ansible中的ls输出创建符号链接

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

我正在尝试将现有的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
    }
}

任何帮助将不胜感激!

bash ansible
2个回答
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}}"

1
投票

我要感谢@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 }}"
© www.soinside.com 2019 - 2024. All rights reserved.