Create a symbolic link based on ls output in Ansible

I am trying to convert an existing bash code snippet to Ansible but I am facing some challenges in implementing it.

What does this script do?

It is first checking if it a SUSE machine. If true, cd to /lib and run a complicated ls command. If there is an output then create a symbolic link using ln.

Here is my bash code snippet:

##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

Here is what I have tried so far:

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

Error messages:

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

Any help will be greatly appreciated!

Upvotes: 1

Views: 881

Answers (2)

I would like to thank @MCI for pointing me in the right direction. Here is the full working solution which meets my requirements.

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

Upvotes: 1

MCI
MCI

Reputation: 922

The command module doesn't run commands through the shell You could use the shell module instead. This would look something like

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

Upvotes: 2

Related Questions