Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 基于Ansible中的ls输出创建符号链接_Bash_Ansible - Fatal编程技术网

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

Bash 基于Ansible中的ls输出创建符号链接,bash,ansible,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 ]; the

我试图将现有的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
    }
}
任何帮助都将不胜感激

命令模块 您可以使用该模块。这看起来像

- 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}}"
命令模块 您可以使用该模块。这看起来像

- 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 }}"

我要感谢@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 }}"