
On Crunchify, we have published quite a few Ansible articles before which includes, installation of Ansible, Copying file from one host to remote host and more.
In this tutorial we will go over steps on how to execute script on remote host after copying it.
This technique is very helpful if you are a IT admin and want to upgrade thousands of VMs and hosts at the same time with just single command.
Ansible is the only togo tool for us at Crunchify, as we deal with lots of hosts for our clients and patch up OS in regular basis.
Let’s get started
We will do below tasks with simple 1 Ansible command:
- On Host1: Create file
crunchify-script.shunder folder/opt/ashah/ - On Host2: Create folder /opt/ashah/
- Copy crunchify-script.sh file from Host1 to Host2 under folder /opt/ashah/
- Execute file crunchify-script.sh on remote host using
ansible-playbookcommand. - Get command line complete result back
Step-1
Create crunchify-script.sh file under /opt/ashah/ folder.
- This script will cd into folder /opt/ashah/
- Extract jdk 11.0.2 using
tar -zxvfcommand - Setup JAVA_HOME once extraction is finished.
crunchify-script.sh
cd /opt/ashah/ tar -zxvf jdk-11.0.2_linux-x64_bin.tar.gz export JAVA_HOME=/opt/ashah/jdk-11.0.2 export PATH=$JAVA_HOME/bin:$PATH
Step-2
Create .yml file for Ansible.
crunchify_execute_command.yml file
---
- name: Let's copy our executable script to remote location, execute script and get result back.
remote_user: root
sudo: yes
hosts: crunchify-group
tasks:
- name: Transfer executable script script
copy: src=/opt/ashah/crunchify-script.sh dest=/opt/ashah mode=0777
- name: Execute the script
command: sh /opt/ashah/crunchify-script.sh
Step-3
crunchify-hosts file which has list of all remote hosts.
crunchify-hosts file
#crunchify-group
[crunchify-group]
192.66.129.83
Step-4
Execute command ansible-playbook.
ansible-playbook -b -v -u root crunchify_execute_command.yml -kkkk --extra-vars "crunchify-group" -i crunchify-hosts
Here is a result:
root@localhost:/opt/ashah# ansible-playbook -b -v -u root crunchify_execute_command.yml -kkkk --extra-vars "crunchify-group" -i crunchify-hosts
Using /etc/ansible/ansible.cfg as config file
SSH password:
/opt/ashah/crunchify-hosts did not meet host_list requirements, check plugin documentation if this is unexpected
/opt/ashah/crunchify-hosts did not meet script requirements, check plugin documentation if this is unexpected
[DEPRECATION WARNING]: Instead of sudo/sudo_user, use become/become_user and make sure become_method is 'sudo' (default). This
feature will be removed in version 2.9. Deprecation warnings can be disabled by setting deprecation_warnings=False in
ansible.cfg.
PLAY [Transfer and execute a script.] *******************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************
ok: [192.66.129.83]
TASK [Transfer the script] ******************************************************************************************************
changed: [192.66.129.83] => {"changed": true, "checksum": "37dd2b7bd24c04fca7b7c436f299541a1f813f17", "dest": "/opt/ashah/crunchify-script.sh", "gid": 0, "group": "root", "md5sum": "140a200fbb7a12cbd6e1f57d3e14794f", "mode": "0777", "owner": "root", "size": 129, "src": "/root/.ansible/tmp/ansible-tmp-1553138812.28-91041260033433/source", "state": "file", "uid": 0}
TASK [Execute the script] *******************************************************************************************************
changed: [192.66.129.83] => {"changed": true, "cmd": ["sh", "/opt/ashah/crunchify-script.sh"], "delta": "0:00:02.713976", "end": "2019-03-21 03:26:56.151494", "rc": 0, "start": "2019-03-21 03:26:53.437518", "stderr": "", "stderr_lines": [], "stdout": "jdk-11.0.2/README.html\njdk-11.0.2/bin/jaotc\njdk-11.0.2/bin/jar\njdk-11.0.2/bin/jarsigner\njdk-11.0.2/bin/java\njdk-11.0.2/bin/jsted.certs", "jdk-11.0.2/lib/security/cacerts", "jdk-11.0.2/lib/security/default.policy", "jdk-11.0.2/lib/security/public_suffix_list.dat", "jdk-11.0.2/lib/server/Xusage.txt", "jdk-11.0.2/lib/server/libjsig.so", "jdk-11.0.2/lib/server/libjvm.so", "jdk-11.0.2/lib/src.zip", "jdk-11.0.2/lib/tzdb.dat", "jdk-11.0.2/release"]}
PLAY RECAP **********************************************************************************************************************
192.66.129.83 : ok=3 changed=2 unreachable=0 failed=0
root@localhost:/opt/ashah#

What’s next?
Check complete tutorial on how to copy Files on remote host with Ansible?
The post Ansible: How to Execute Commands on remote Hosts and get command result (log) back? appeared first on Crunchify.
0 Commentaires