Nohup no such file or directory with Ansible Playbook - ansible

I am trying to execute a playbook that will execute sql script on 4 different databases.
In the playbook, I am using the command as
- name: Run sample script
shell: nohup ./S4D/wrapper.sh ./S4D/sample.sql > ./S4D/nohup.out 2>&1 &
The directory structure looks like
root/
└── SD4/
├── wrapper.sh
└── sample.sql
I am getting the error
"stderr" : "/bin/sh: ./S4D/nohup.out: No such file or directory"
already checked EOL conversion, set to Unix (LF).

If you want to be sure of where the task is going to execute, you can use the chdir parameter of the shell module:
- name: Run sample script
shell: nohup wrapper.sh sample.sql > nohup.out 2>&1 &
args:
chdir: /SD4
## chdir: /root/SD4
## ^--- since I am not sure from your question
## if root is / or
## if it is the /root folder

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

Playbook doesnt return any value

I am new to ansible and trying to write a new playbook which goes to
a) A certain folder
b) clean the env
c)pulls the code
d) and builds it
Here is my playbook, help is deeply appreciated. :)
- name: Shell examples
hosts: all
tasks:
- name: mulitline shell
shell: |
cd /home/ec2-user/node
echo -e "\n pulling the code in here" 'pwd'
git clean -d -f .
git pull
npm install

How to run vendor provided script from playbook to enable csh environment variables

I need to execute a vendor provided csh script to create a veritable plethora of environment variables in order to run application from playbook.
I have built an ad-hoc script that uses screen to inject the commands I need to run inside of a csh session. This succesfully allows me to run the application; but again, from an ad-hoc script and not a playbook.
### start the fsc
# launch a screen session with csh
ansible 10.1.1.103 -m shell -a "su - testdev -c 'screen -dmS testdev_fsc csh'" -b
# run vendor provided env variables script
ansible 10.1.1.103 -m shell -a "su - testdev -c 'screen -S testdev_fsc -X stuff '/export/home/testdev/tcdata/tc_cshvars^M''" -b
# execute the application
ansible 10.1.1.103 -m shell -a "su - testdev -c 'screen -S testdev_fsc -X stuff '/export/home/testdev/ccbin/fsc.sh^M''" -b
In the end, I'd like to be able to create a playbook snippet that allows the above to run/execute.
So basically you want to become user testdev and run two commands in a row in the same csh session. The following fictive playbook should do the job.
---
# Playbook example
- name: Play to run my script
hosts: my_inventory_group
remote_user: my_remote_connection_user
tasks
- name: run my script
become: true
become_user: testdev
shell: |-
./tcdata/tc_cshvars
./ccbin/fsc.sh
args:
chdir: /export/home/testdev/
executable: /bin/csh
|- is the yaml litteral block marker with trailing space control. All the following block is interpreted as a string with new lines preserved. Passed to the shell module, each line will be executed (as if entered in your own shell). Double check the path for your csh executable. You must provide an absolute path.
If you have several tasks in your playbook and need to run all or them as testdev, you can move become and become_user to play level rather that task level.

how to start the service in target server using ansible playbook

I'm trying to run the following ansible playbook to start the "nexus" service on remote server at path "nexux/bin" it gets failed :
- hosts: nexus
become: yes
become_user: nexus
become_method: sudo
tasks:
- name: changing dir and starting nexus service
shell:
chdir: nexux/bin
executable: ./nexus start
Can someone troubleshoot here to deduce the root cause ?
As the ansible output very clearly told you, in that syntax you did not provide a command. The executable: is designed to be the shell executable, not the "run this thing" argument. It is very clear in the examples section of the fine manual
- shell: cd /opt/nexus/bin && ./nexus start
If you want to use the chdir: option, you must put it under a sibling yaml key to the shell:, like so:
- shell: echo hello world
args:
chdir: /opt/nexus/bin
# I'm omitting the "executable:" key here, because you for sure
# do not want to do that, but if you did, then fine, put it here
Having said all of that, as the docs also indicate, what you really want is to use command: because you are not making use of any special shell characters (redirects, pipes, && phrases, etc), so:
- command: ./nexus start
args:
chdir: /opt/nexus/bin
Try use the shell module, i also recommend to run with nohup and send the output to a file
- shell: |
cd /opt/nexus/bin
nohup ./nexus start > /tmp/nexus.log 2>&1 &

ansible throwing error on script execution

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

Resources