ansible throwing error on script execution - ansible

I am trying to run the script (foo) which is present under my home directory (/home/ubuntu) using ansible.
if i manually move to /home/ubuntu and run the script as below
./foo --arg1=aaa --arg2=xxx --arg3=yyy
the script is working fine in command line.
However, when i try to run the same script using ansible as below
- name: Running Config Script
command: chdir=/home/ubuntu ./foo --arg1=aaa --arg2=xxx --arg3=yyy
The script is failing . And i also tried using script tag instead of command. Its not working .

I didn't tested but please try using shell instead of command
- name: Running Config Script
shell: ./foo --arg1=aaa --arg2=xxx --arg3=yyy
args:
chdir: /home/ubuntu

Related

Ansible: Why won't this script log when run through ansible?

At the start of a script I have:
exec 3>&1 4>&2
trap 'exec 2>&4 1>&3' 0 1 2 3
exec 1>patch_log.out 2>&1
(From https://serverfault.com/questions/103501/how-can-i-fully-log-all-bash-scripts-actions)
which when the script is run in the terminal it gives the log file patch_log.out I expect
but when running the script from ansible using the shell module it does not (yet I know the rest of the script works correctly)
I imagine it is something to do with my understanding of how exec works, and how I could get it to work through ansible
Running the script in Ansible
Needed to pass argument to ensure using bash (not sh) (thanks to #U880D)
Make sure to set destination directory
So in playbook where I run the script:
args:
executable:
/bin/bash
chdir:
/home/user/directory

The command was not found or was not executable: .sh // /bin/chmod: No such file or directory

I am trying to run a shell script and become an error:
"The command was not found or was not executable: chdir=/home/cloudera/Documents/TPCx-BB/bin/TPCxBB_Benchmarkrun.sh."
The script is in this folder, so it has to be that it is not executable. It has the following permission:
chmod 777 -R /home/cloudera/Documents/TPCx-BB/bin/TPCxBB_Benchmarkrun.sh
The script begins with:
#!/usr/bin/env bash
and I am running it from ansible as follows:
command: chdir=/home/cloudera/Documents/TPCx-BB/bin/TPCxBB_Benchmarkrun.sh
I have found a possible solution to run the command like this:
But then I get the following error:
"/bin/chmod: cannot access 'chdir=/home/cloudera/Documents/TPCx-BB/bin/TPCxBB_Benchmarkrun.sh': No such file or directory"
Anyone sees what could be the problem?
chdir=/home/cloudera/Documents/TPCx-BB/bin/TPCxBB_Benchmarkrun.sh is obviously not a command you can pass to a shell/command.
Either pass the command full path directly:
- name: run my command
command: /home/cloudera/Documents/TPCx-BB/bin/TPCxBB_Benchmarkrun.sh
Or use the chdir option correctly:
- name: run my command
command: ./TPCxBB_Benchmarkrun.sh
args:
chdir: /home/cloudera/Documents/TPCx-BB/bin
For more info and examples: https://docs.ansible.com/ansible/latest/modules/command_module.html
Resolved the issue with: chmod +x ./TPCxBB_Benchmarkrun.sh

Unable to execute bashrc function using ANSIBLE

I am trying to execute a bashrc function "enableSsh" using ANSIBLE. I am using below ANSIBLE code to get this done but getting error: enableSsh command not found.
- name: Switch to user root and enable ssh
become: yes
become_user: root
become_method: su
shell: . .bashrc && enableSsh
args:
executable: /bin/bash
Note: I tested it removing enableSsh and reloading bashrc is working fine. enableSsh is a function defined in remote server's bashrc file and works fine if execute directly from command line.
This is not an issue with Ansible, this is a shortcoming of the alias command as explained here.

Shell command doesn't function, but echo'ing the command and manually running it works?

I'm trying to run the following lines of shell script:
var_files=$(var_file_selector)
echo ${var_files}
terraform apply ${var_files} deploy/$1
Where var_files resolves to "deploy/vars/vars.tfvars". When I run the script, I get the following error:
invalid value "\"deploy/vars/vars.tfvars\"" for flag -var-file: Error reading "deploy/vars/vars.tfvars": open "deploy/vars/vars.tfvars": no such file or directory
However if I echo out the whole command:
echo terraform apply ${var_files} deploy/$1
I get:
terraform apply -var-file="deploy/vars/vars.tfvars" deploy/cluster
Which I can run manually from the terminal (in the same working directory that I'm running the script from) and it works just fine. What am I not understanding here?

How to determine whether a script has previously run using Ansible?

I'm using Ansible to deploy (Git clone, run the install script) a framework to a server. The install step means running the install.sh script like this:
- name: Install Foo Framework
shell: ./install.sh
args:
chdir: ~/foo
How can I determine whether I have executed this step in a previous run of Ansible? I want to add a when condition to this step that only executes if the install.sh script hasn't been run previously.
The install.sh script does a couple of things (replacing some files in the user's home directory), but it's not obvious whether the script was run before from just taking a look at the files. The ~/foo.sh file might have existed before, it's not clear whether it was replaced by the install script or was there before.
Is there a way in Ansible to store a value on the server that let's me determine whether this particular task has been executed before? Or should I just create a marker file in the user's home directory (e.g. ~/foo-installed) that I check in later invocations of the playbook?
I suggest to use the script module instead. This module has a creates parameter:
a filename, when it already exists, this step will not be run. (added in Ansible 1.5)
So your script then could simply touch a file which would prevent execution of the script in subsequent calls.
Here's how I solved it in the end. The pointer to using the creates option helped:
- name: Install Foo Framework
shell: ./install.sh && touch ~/foo_installed
args:
chdir: ~/foo
creates: ~/foo_installed
Using this approach, the ~/foo_installed file is only created when the install script finished without an error.

Resources