Ansible playbook failed when running a shell script

Viewed 119

So I created this Ansible playbook to:

  1. copy a zip file and unzip it
  2. copy a zip file and unzip it
  3. make script inside the file executable
  4. run the script
  5. enable 2 services
- name: fideliplaybook
  hosts: k8scluster
  tasks:

    - name: copying file with playbook
      become: true
      copy:
        src: /home/lc/lc.zip
        dest: /home/lc/
        mode: 755

    - name: Update apt cache and install unzip
      become: true
      command: apt install unzip


    - name: unzip file
      become: true
      unarchive:
        src: /home/lc/lc.zip
        dest: /home/lc/

    - name: make script executable
      become: true
      file: dest=/home/lc/lc/install.sh mode=755

    - name: Execute the script
      become: true
      command: sh /home/lc/lc/install.sh

    - name: Enable service 1
      become: true
      command: systemctl enable service1.service

    - name: Enable service 2
      become: true
      command: systemctl enable service1.service

the issue that I am facing here that when ansible is trying to execut the script "install.sh" it somehow fails becuse script cant find 2 other scripts although the other scripts are in the same file.

so 3 scripts are in lc.zip but when running install.sh with ansible it cant find the 2 other scripts. I fixed the issue by typing the full path of the 2 scripts inside the first script. but anyone have an idea why this issue occuers.

one more quistion when i used ansible hosts file to define hosts and variables it didnt worked with INI like this:

[webservers]
www[01:50].example.com

It only woks In YAML:

...
  webservers:
    hosts:
      www[01:50].example.com:

Anyone know why?

1 Answers

About the script not finding the other two scripts, you should update the full path to the other two scripts if the path is always fixed OR if you want to use the relative path, you can try the following,

   - name: Execute the script
     become: true
     command: sh install.sh
     args:
       chdir: /home/lc/lc/

And the inventory looks fine. Here is a test to confirm.

# cat test.ini
[webservers]
tstsrv[7:8]


# ansible-playbook -i test.ini test.yaml -u admin -k
SSH password:

PLAY [webservers] ***********************************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************
ok: [tstsrv8]
ok: [tstsrv7]

TASK [print hostname] ***********************************************************************************************************************************************
changed: [tstsrv8]
changed: [tstsrv7]

PLAY RECAP **********************************************************************************************************************************************************
tstsrv7                 : ok=2    changed=1    unreachable=0    failed=0
tstsrv8                 : ok=2    changed=1    unreachable=0    failed=0

#

Consider script module dedicated for running scripts on targets, if possible.

Related