File not created in ansible - ansible

I have installed ansible on one machine and trying to execute commands on another (remote) machinge.
Ansible successfully installed
Able to reach all hosts (local and remote). Tested with
ansible all -m ping
This was successful
Trying to execute a simple command again
ansible all -a 'echo "hello world" > ~/test'
Executed successfuly. But the file test is not created.
Cannot find the reason why?

Executing a command via ansible -a is equivalent to the command module, see
command module.
It is not processed via shell, therefore, >> (as well as other redirection operators) and $HOME are not available
In your case I would use
ansible -m 'shell' --args 'echo "hello world">>/home/ansibleremoteuser/test' all
In this case you would use the shell module which allows redirections.

Related

How to execute Linux bash command in Apache NiFi in Windows 10

I use NiFi on Windows 10 machine with installed Linux subsystem (Ubuntu).
My task is to execute bash scripts and commands using NiFi. I tried to use ExecuteProcess and ExecuteStreamCommand with selected commands like simply 'bash' or 'bash ls' for test purposes, but all I got was:
ExecuteProcess[id=4f530725-0171-1000-d1b1-7df587eada7e] /bin/ls:
/bin/ls: cannot execute binary file
If I try to pass basic Windows commands everything is OK.
Is there a way to run bash commands in my case?
I'm not a Windows user but according to the docs, in order to get to the Linux-style commands you have to run Bash.exe, so I'm guessing you'll need to specify -c as an argument followed by the Linux bash command you want to run (as a string), something like:
bash -c "ls"

how to run multiple command in a single line in ansible

I need to a run two ansible command in a single line. While am running the command its taking only the second command .
ansible -i list cdlk -a "touch /tmp/a" -a "touch /tmp/b" --private-key=/tmp/id_rsa
I have create a file called list and after running this command only /tmp/b file is getting created .How I can run multiple commands in single line?
By default ansible CLI uses command module, which does not feed its arguments trough shell.change your parameter according to requirement.An example is shared below
You want to use shell module instead:
ansible host -m shell -a 'echo hello && echo world'

ansible adhoc debug message (Not to run a playbook)

I'd like to ask how to show the output of debug as if I used register using the ansible command, not play a playbook.
For an example, I just to do below, and do a playbook:
$ ansible -m raw -a "df -h"
Thank you.
I assume you want to see the output of the command, so instead of using the module raw, use the shell module -m shell and it will work.
If you don't specify a module, ansible uses the module command so it will works also.
The module shell is missing:
$ ansible -m shell raw -a "df -h"

Running sub shell command in Ansible ad-hoc

I would like to run a sub shell command in an ad-hoc Ansible command.
Here is what I want to do :
sudo ansible myservers -m shell -a "touch /var/tmp/$(uname -n)"
It creates the remote file but with the name of the local host, it doesn't execute the uname command on remote servers.
I found the solution :
sudo ansible myservers -m shell -a '/bin/bash -c "toto=`uname -n` ; touch /var/tmp/\$toto.json;"'
Seems that I have to start a shell to execute sub shell commands, but it works.
The problem comes from the double-quotes in your command.
They mean that the content between the double-quotes (including your $(...)) will be interpreted by the shell that executes this command (your Ansible control node), so you get the ansible control node name.
If you replace the double-quotes with simple-quotes, the shell of the control node will not interpret the content, and will pass it "as-is" to the ansible hosts. Afterwards, these ansible hosts will interpret the $(...), so you'll get the target ansible hostname.
See http://www.gnu.org/software/bash/manual/html_node/Single-Quotes.html and https://www.gnu.org/software/bash/manual/html_node/Double-Quotes.html
So it could be fixed with :
ansible myservers -m shell -a 'touch /var/tmp/$(uname -n)'
(I don't see why you had to use sudo)
Or (if the name of your host in your inventory is good enough), you could use :
ansible myservers -m shell -a 'touch /var/tmp/{{ inventory_hostname }}'
By the way, if you end-up using this command in a playbook, another solution is to use the Ansible facts : ansible_hostname or ansible_fqdn for example

sbt (Scala) via SSH results in command not found, but works if I do it myself

So I'm trying to do something that involves running sbt over an SSH command, and this is what I'm trying:
ssh my_username#<server ip> "cd <project folder>; sbt 'run-main Foo' "
When I do that however, I get an error message: bash: sbt: command not found
Then I go SSH into the server myself, cd to the project folder, and run sbt 'run-main Foo' and everything works nicely. I have checked to make sure sbt is on the $PATH variable on the remote server via ssh my_username#<server ip> "echo $PATH" and it shows the correct value.
I feel like this is a simple fix, but cannot figure it out... help?
Thanks!
-kstruct
When you log in, bash is run as an interactive shell. When you run commands directly through ssh, bash is run as a non-interactive shell, and therefore different initialization files are sourced (see the bash manual pages for which exactly). There are a number of ways to fix this, e.g.:
Use the full path to sbt when calling it directly through ssh
Edit .bashrc and add the missing directories to the PATH environment variable
Note that your test ssh my_username#<server ip> "echo $PATH" actually prints PATH on your client, not your server, because of the double quotes. Use ssh my_username#<server ip> 'echo $PATH' or ssh my_username#<server ip> env to print PATH from the server's environment. When checking using env, you will see that PS1 is only set in interactive shells.

Resources