4th Ansible YML Playbook: Copy a script and run on remote server.

Today lets have a look at an ansible playbook to copy a shell script to remote server and then run it as root.

And finally get its output on our local system.

Seems interesting !!

First have a look at the YML file below.

1. Ansible Playbook to copy a file and then execute it.

$ cat playbooks/fourth_play.yml 
 - name: "Get System Information by copying shell script"
   gather_facts: no
   hosts: all
   - action: copy src="../scripts/system_info.sh" dest="~/"  
# We can also use mode with copy module to set permissions like mode=755  
 - name: "Execute the script"
   gather_facts: no
   become: true
   hosts: all
   - action: "shell chmod +x ~c5014038/system_info.sh; ~c5014038/system_info.sh"
     register: hello
   - debug: msg="{{ hello.stdout }}"

2. Explanation
1. “copy” module is used to copy the shell script from local system to remote system under user home directory.
2. “shell” module is used in next task to change the permission and then execute it.
3. Ideally we should use “mode=755” in copy module above to change file permissions while copying, however this way also we can do it.

3. Output

$ ansible-playbook -b -K playbooks/fourth_play.yml 
SUDO password: 

PLAY [Get System Information by copying shell script] ******************************************************************************************************************************************

TASK [copy] ************************************************************************************************************************************************************************************
ok: [r168-host1.ngelinux.com]
ok: [r005-host1.ngelinux.com]

PLAY [Execute the script] **********************************************************************************************************************************************************************

TASK [shell] ***********************************************************************************************************************************************************************************
 [WARNING]: Consider using the file module with mode rather than running chmod.  If you need to use command because file is insufficient you can add warn=False to this command task or set
command_warnings=False in ansible.cfg to get rid of this message.

changed: [r168-host1.ngelinux.com]
changed: [r005-host1.ngelinux.com]

TASK [debug] ***********************************************************************************************************************************************************************************
ok: [r005-host1.ngelinux.com] => {
    "msg": "\tVendor: HP\n\tVersion: P89\n\tRelease Date: 05/21/2018"
ok: [r168-host1.ngelinux.com] => {
    "msg": "\tVendor: HP\n\tVersion: P89\n\tRelease Date: 05/21/2018"

PLAY RECAP *************************************************************************************************************************************************************************************
r005-host1.ngelinux.com     : ok=3    changed=1    unreachable=0    failed=0   
r168-host1.ngelinux.com     : ok=3    changed=1    unreachable=0    failed=0   

Leave a Reply

Your email address will not be published.