Ansible: How to Execute Commands on remote Hosts and get command result (log) back?

Last updated
App Shah

Crunchify » Technology & Tools » Ansible: How to Execute Commands on remote Hosts and get command result (log) back?

Ansible Execute Command Result Details- Crunchify Tutorial

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:

  1. On Host1: Create file under folder /opt/ashah/
  2. On Host2: Create folder /opt/ashah/
  3. Copy file from Host1 to Host2 under folder /opt/ashah/
  4. Execute file on remote host using ansible-playbook command.
  5. Get command line complete result back


Create file under /opt/ashah/ folder.

  • This script will cd into folder /opt/ashah/
  • Extract jdk 11.0.2  using tar -zxvf command
  • Setup JAVA_HOME once extraction is finished.

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


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
     - name: Transfer executable script script
       copy: src=/opt/ashah/ dest=/opt/ashah mode=0777

     - name: Execute the script
       command: sh /opt/ashah/


crunchify-hosts file which has list of all remote hosts.

crunchify-hosts file




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 

PLAY [Transfer and execute a script.] *******************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************
ok: []

TASK [Transfer the script] ******************************************************************************************************
changed: [] => {"changed": true, "checksum": "37dd2b7bd24c04fca7b7c436f299541a1f813f17", "dest": "/opt/ashah/", "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: [] => {"changed": true, "cmd": ["sh", "/opt/ashah/"], "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/", "jdk-11.0.2/lib/server/", "jdk-11.0.2/lib/", "jdk-11.0.2/lib/tzdb.dat", "jdk-11.0.2/release"]}

PLAY RECAP **********************************************************************************************************************                : ok=3    changed=2    unreachable=0    failed=0   

Ansible Execute Command Result - Crunchify Tutorial

What’s next?

Check complete tutorial on how to copy Files on remote host with Ansible?

Leave a Comment