Ansible Inside Script Command Not Found

Error when executing a linux script by ansible

In environment you specify a PATH that does not include /bin (or /usr/bin), so sh is not found in the PATH.

Ansible wrongly reports 'command not found' error

Here is what I did to simulate what Ansible does (Based on remarks by @Zeitounator): ssh <user>@<machine> '/bin/bash -c "echo $PATH"'

I get my default PATH as explained in the manual page of bash. In my system sh links to bash.

I see that /etc/profile does the path manipulation that I need. However, it seems that because of the option -c, the bash is not started as login shell and therefore etc/profile is not sourced.

I end up doing the job manually:

---
- name: Test the execution of pcs command
hosts: UVMEL7

tasks:
- name: Call echo
ansible.builtin.shell: echo
- name: pcs
ansible.builtin.shell: source /etc/profile && pcs

Which executes pcs as expected.

To sum up, my executable was not executed because the folder holding it was not listed in my PATH environment variable. This was due to the fact that /bin/sh aka /bin/bash was called with the flag -c which prevents sourcing /etc/profile and other configuration files. The issue was 'solved' by sourcing manually the configuration file that correctly sets the PATH environment variable.

Ansible: 'shell' not executing

There are different topics in your question.

Regarding

to install a package downloaded from my local Artifactory repository, as I don't have access to download it straight from the internet.

you can use different approaches.

1. Direct download

- name: Make sure package becomes installed from internal repository
yum:
name: https://{{ REPOSITORY_URL }}/artifactory/kube/kubectl-{{ KUBE_VERSION }}.x86_64.rpm
state: present

2. Configure local repository

The next one is to provide a .repo template file like

[KUBE]
name = Kubectl - $basearch
baseurl = https://{{ REPOSITORY_URL }}/artifactory/kube/
username = {{ API_USER }}
password = {{ API_KEY }}
sslverify = 1
enabled = 1
gpgcheck = 1
gpgkey = file:///etc/pki/rpm-gpg/RPM-GPG-KEY-KUBE

and to perform

- name: Make sure package becomes installed from internal repository
yum:
name: kubectl
state: present

This is possible because JFrog Artifactory can provide local RPM repositories if configured correctly. For more information you research the documentation there since it is almost only about proper configuration.

Regarding

Nothing happens. It doesn't error.. It just doesn't install.

you can use several task to split up your steps, make them idempotent and get an better insight how they are working.

3. shell, rpm and debug

- name: Make sure destination folder for package download (/opt/packages) exists
file:
path: "/opt/packages/"
state: directory

- name: Download RPM to remote hosts
get_url:
url: "https://{{ REPOSTORY_URL }}/artifactory/kube/kubectl-{{ KUBE_VERSION }}.x86_64.rpm"
dest: "/opt/packages/kubectl-{{ KUBE_VERSION }}.x86_64.rpm"

- name: Check package content
shell:
cmd: "rpm -qlp /opt/packages/kubectl-{{ KUBE_VERSION }}.x86_64.rpm"
register: rpm_qlp

- name: STDOUT rpm_qlp
debug:
msg: "{{ rpm_qlp.stdout.split('\n')[:-1] }}"

- name: Install RPM using 'command: rpm -ivh'
shell:
cmd: "rpm -ivh /opt/packages/kubectl-{{ KUBE_VERSION }}.x86_64.rpm"
register: rpm_ivh

- name: STDOUT rpm_ivh
debug:
msg: "{{ rpm_ivh.stdout.split('\n')[:-1] }}"

Depending on the RPM package, environment and configuration, all may just work good.

Ansible playbook failed when running a shell script

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.

Command not found while trying to build package with ansible

Module joshualund.golang installs go to non-standart directory /usr/local/go (look at the sources) so a problem most likely because of this fact.

To resolve it you should somehow update $PATH variable which used by ansible. One of the way is to explicitly specify it:

- name: Build etcd
command: chdir={{repo_location}} ./build
environment:
PATH: /sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin:/usr/local/go/bin

Ansible shell command `pod2man` not found

As reported in comments, the problem is that when switching users sudo sanitizes PATH (see this ServerFault question for details).

The ad-hoc command to export the local PATH to hosts that I propose seems a bit hacky but it works (it uses the --extra-vars option of ansible command together with Jinja's variable injection)

ansible all -m shell -a "export PATH={{path}}; curl -s https://gist.githubusercontent.com/Tadly/0e65d30f279a34c33e9b/raw/pacaur_install.sh | bash" -e path=$PATH

You could also use the {{ lookup('env', 'PATH') }} to forward the PATH.



Related Topics



Leave a reply



Submit