Ansible execute remote shell script with source to other files

37 views
Skip to first unread message

SysAdmin EM

unread,
Feb 10, 2022, 8:49:28 AM2/10/22
to ansible...@googlegroups.com
Hello,
I’m trying to run a playbook which executes a shell script in a remote instance.

Playbook
---
- name: Test SH
  hosts: schedule-dev
  become: yes
  gather_facts: no
  tasks:
    - name: Test Shell Ansible
      shell: |
        sh create_partition_db.sh {{ carrier }}
      args:
        chdir: /opt/app/automation
        executable: /bin/bash
      register: output
    - name: ouput
      debug:
        var: output.stdout
The value of the variable {{ carrier }} is sent through the command line:

ansible-playbook -i hosts-dev --extra-vars "carrier=dev" playbook/step-2.yaml

When I run the playbook I see this error:

fatal: [schedule01-dev]: FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "cmd": "sh create_partition_db.sh 00CN\n", "delta": "0:00:00.003453", "end": "2022-02-10 13:27:23.244718", "msg": "non-zero return code", "rc": 2, "start": "2022-02-10 13:27:23.241265", "stderr": "create_partition_db.sh: 3: create_partition_db.sh: source: not found\ncreate_partition_db.sh: 4: create_partition_db.sh: source: not found\ncreate_partition_db.sh: 6: create_partition_db.sh: get_partitions: not found\ncreate_partition_db.sh: 8: create_partition_db.sh: function: not found\ncreate_partition_db.sh: 34: create_partition_db.sh: Syntax error: \"}\" unexpected", "stderr_lines": ["create_partition_db.sh: 3: create_partition_db.sh: source: not found", "create_partition_db.sh: 4: create_partition_db.sh: source: not found", "create_partition_db.sh: 6: create_partition_db.sh: get_partitions: not found", "create_partition_db.sh: 8: create_partition_db.sh: function: not found", "create_partition_db.sh: 34: create_partition_db.sh: Syntax error: \"}\" unexpected"], "stdout": "", "stdout_lines": []}

PLAY RECAP *****************************************************************************************************************************************
schedule01-dev             : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0  
When I run the script from the instance I don’t see any errors, I think it has to do with the fact that in the script I use the source command to call other files:

#!/bin/bash

source config-dev.cfg
source functions.sh


Any sugestion?

Stefan Hornburg (Racke)

unread,
Feb 10, 2022, 9:57:07 AM2/10/22
to ansible...@googlegroups.com
Looks like the default shell on the target doesn't know about "source". Use "." instead or force the usage of bash with:

executable: "/bin/bash"

in your shell task.

Regards
Racke

>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAGUDtn%3DHeuyr5kyXPYODJKCvn06d6QhmMne9USsxWxD%3D8O%2B-zw%40mail.gmail.com <https://groups.google.com/d/msgid/ansible-project/CAGUDtn%3DHeuyr5kyXPYODJKCvn06d6QhmMne9USsxWxD%3D8O%2B-zw%40mail.gmail.com?utm_medium=email&utm_source=footer>.


--
Automation expert - Ansible and friends
Linux administrator & Debian maintainer
Perl Dancer & conference hopper

OpenPGP_signature

SysAdmin EM

unread,
Feb 10, 2022, 10:15:39 AM2/10/22
to ansible...@googlegroups.com
I changed source by "." inside the shell script and it’s still not running.

#!/bin/bash

. config-dev.cfg
. functions.sh

Within the playbook I am already using the parameter "executable: /bin/bash".


Playbook
---
- name: Test SH
  hosts: schedule-dev
  become: yes
  gather_facts: no
  tasks:
    - name: Test Shell Ansible
      shell: |
        sh create_partition_db.sh {{ carrier }}
      args:
        chdir: /opt/app/automation
        executable: /bin/bash
      register: output
    - name: ouput
      debug:
        var: output.stdout
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/04e71d13-c95f-fc60-14d6-9ff8e9d21c7b%40linuxia.de.

Stefan Hornburg (Racke)

unread,
Feb 10, 2022, 10:24:31 AM2/10/22
to ansible...@googlegroups.com
On 10/02/2022 16:15, SysAdmin EM wrote:
> I changed source by "." inside the shell script and it’s still not running.
>
> #!/bin/bash
>
> . config-dev.cfg
> . functions.sh
>
> Within the playbook I am already using the parameter "executable: /bin/bash".

That's true, but you again calling the shell again with "sh ..". Makes no sense to me.

Regards
Racke

>
> Playbook
> ---
> - name: Test SH
>   hosts: schedule-dev
>   become: yes
>   gather_facts: no
>   tasks:
>     - name: Test Shell Ansible
>       shell: |
>         sh create_partition_db.sh {{ carrier }}
>       args:
>         chdir: /opt/app/automation
>         executable: /bin/bash
>       register: output
>     - name: ouput
>       debug:
>         var: output.stdout
>
> > To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com> <mailto:ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>>.
> > To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAGUDtn%3DHeuyr5kyXPYODJKCvn06d6QhmMne9USsxWxD%3D8O%2B-zw%40mail.gmail.com <https://groups.google.com/d/msgid/ansible-project/CAGUDtn%3DHeuyr5kyXPYODJKCvn06d6QhmMne9USsxWxD%3D8O%2B-zw%40mail.gmail.com> <https://groups.google.com/d/msgid/ansible-project/CAGUDtn%3DHeuyr5kyXPYODJKCvn06d6QhmMne9USsxWxD%3D8O%2B-zw%40mail.gmail.com?utm_medium=email&utm_source=footer <https://groups.google.com/d/msgid/ansible-project/CAGUDtn%3DHeuyr5kyXPYODJKCvn06d6QhmMne9USsxWxD%3D8O%2B-zw%40mail.gmail.com?utm_medium=email&utm_source=footer>>.
>
>
> --
> Automation expert - Ansible and friends
> Linux administrator & Debian maintainer
> Perl Dancer & conference hopper
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/04e71d13-c95f-fc60-14d6-9ff8e9d21c7b%40linuxia.de <https://groups.google.com/d/msgid/ansible-project/04e71d13-c95f-fc60-14d6-9ff8e9d21c7b%40linuxia.de>.
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAGUDtn%3Dm5-fbF50Pjhd4tMNXe2CVqrbU6Zi6SDhkG%3DOOHa1GzA%40mail.gmail.com <https://groups.google.com/d/msgid/ansible-project/CAGUDtn%3Dm5-fbF50Pjhd4tMNXe2CVqrbU6Zi6SDhkG%3DOOHa1GzA%40mail.gmail.com?utm_medium=email&utm_source=footer>.
OpenPGP_signature
Reply all
Reply to author
Forward
0 new messages