how to run multiple command in a single line in ansible - 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'

Related

Run a powershell script and set the result in command line argument docker

I want to run a powershell command on windows to fetch the contents of a file (private SSH key) and then pass it to a build command argument for Docker. such as :
docker build --build-arg SSH_PRIVATE_KEY="powershell cat id_ed25519" -t buildtools2019:latest .
where powershell cat id_ed25519 should be the result returned by this powershell script. Clearly this is not the correct syntax, so how should I write it to get the result in the SSH_PRIVATE_KEY argument. Thanks

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

Suggest how to use scp and ssh command in ansible with shell or command moudle, also unable to run the scp command from shell script

Below is the code i need to run using ansible -
I need to include the scp and ssh command in shell module, is it possible?
The list file contains the ip address of the server where we need to copy the files.
How can i include the following part of code in my playbook.
for i in `cat /tmp/list`
do
echo $i
scp HLC_auth $i:.ssh/authorized_keys
scp known_hosts.LLC $i:.ssh/known_hosts
ssh $i "rm -rf .ssh/id_dsa*"
ssh $i "ssh-keygen -d"
done
I am successfully able to perform the for loop task as below -
shell: for i in `cat /home/dp794d/temp/list`;
do echo $i;
done
This gives me the list of ip, but when i try to include the scp command under the same, it do not works.
I thought to dump all this code in the script and try to run the script, but still it is giving me some error -
For testing purpose i created one script having an scp command -
My script file(abc.sh) contains below scp command -
!/bin/bash
scp /home/capio/ansible/pmossWipm/day1/playbooks/xyz.txt abc#130.6.50.131:/home/user
My yml looks like -
---
- hosts: testserver
tasks:
- name: "run the script"
script: /home/capio/ansible/pmossWipm/day1/scripts/abc.sh
But still it is not working , it do not give any response. we need to terminate it.-
Playbook error screenshot
Using your for-loop as the basis for an Ansible playbook...
Define an inventory file using your "/tmp/list" file.
For each of the "scp" lines, use the Ansible "copy" module: http://docs.ansible.com/ansible/copy_module.html
For the "rm" line, use the Ansible "file" module pointing to the id_dsa files and setting the "state" to "absent": http://docs.ansible.com/ansible/file_module.html
I'm not sure what "ssh-keygen -d" does, but it's probably something that can be done with a pure Ansible module.

File not created in 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.

Resources