Ansible Remote Shell Examples

To execute remote commands or get access to a remote server using ansible, you can do:

source =prdeu4spl002 
destination = prdus1ans105 
auth = ssh-keys already installed (not part of the example)
command1 = pwd
command2 = ls -lah
command3 = /usr/local/sbin/schedule-downtime -s "now" -d 1 -r "Test Downtime"

Example


[spluser@prdeu4spl002 ansible]$
[spluser@prdeu4spl002 ansible]$
[spluser@prdeu4spl002 ansible]$ ansible all -i ,prdus1ans105 -b -m shell -a 'pwd'
prdus1ans105 | CHANGED | rc=0 >>
/home/spluser
[spluser@prdeu4spl002 ansible]$
[spluser@prdeu4spl002 ansible]$ ansible all -i ,prdus1ans105 -b -m shell -a 'ls -lah'
prdus1ans105 | CHANGED | rc=0 >>
total 16K
drwx------. 5 spluser spluser 130 Jul 25 2024 .
drwxr-xr-x. 12 root root 183 May 8 15:20 ..
drwx------ 3 spluser spluser 17 Jun 25 2024 .ansible
-rw------- 1 spluser spluser 182 Jun 4 12:55 .bash_history
-rw-r--r--. 1 spluser spluser 18 Jun 20 2022 .bash_logout
-rw-r--r--. 1 spluser spluser 141 Jun 20 2022 .bash_profile
-rw-r--r--. 1 spluser spluser 376 Jun 20 2022 .bashrc
drwxr-x--- 5 spluser spluser 39 Jun 25 2024 .puppetlabs
drwx------. 2 spluser spluser 29 Jun 25 2024 .ssh
[spluser@prdeu4spl002 ansible]$
[spluser@prdeu4spl002 ansible]$
[spluser@prdeu4spl002 ansible]$ ansible all -i ,prdus1ans105 -b -m shell -a '/usr/local/sbin/schedule-downtime -s "now" -d 1 -r "Test Downtime"'
prdus1ans105 | CHANGED | rc=0 >>

Now scheduling a downtime from 14:59:02 until 15:00:02 for prdus1ans105 in Mon01 and nIcinga because of "Test Downtime" on behalf of user ROOT.

contacting the Mon01 satellite REST API...
OK

contacting the nIcinga REST API...
OK
[spluser@prdeu4spl002 ansible]$
[spluser@prdeu4spl002 ansible]$



Ansible Remote Shell Examples

To execute remote commands or get access to a remote server using ansible, you can do: source =prdeu4spl002  destination = prdus1ans105  aut...