
Ansible Playbooks: Shell
Create a Yaml file with shell commands as follows
cat shell.yml
---
- hosts: localhost
tasks:
- name: Execute shell command
shell: |
pwd
ls
df -h
- name: Execute shell script
shell: /home/ansible/Desktop/shell.sh
How to run a playbook?
Execute following command to run a playbook in Ansible
ansible-playbook -v shell.yml
or
ansible-playbook -vvv shell.yml
ansible@ansible-VirtualBox:~/Desktop$ ansible-playbook -v shell.yml
No config file found; using defaults
PLAY
[localhost]
******************************************************************************************************************************************************************************************
TASK
[Gathering Facts]
************************************************************************************************************************************************************************************
ok: [localhost]
TASK [Execute shell command] ******************************************************************************************************************************************************************************
changed:
[localhost] => {"changed": true, "cmd": "pwd\nls\ndf -h\n", "delta":
"0:00:00.003099", "end": "2021-04-23 13:49:30.938095", "rc": 0,
"start": "2021-04-23 13:49:30.934996", "stderr": "", "stderr_lines": [],
"stdout": "/home/ansible/Desktop\ndocker.yml\nshell.sh\nshell.yml\nFilesystem Size Used Avail Use%
Mounted on\ntmpfs 199M 1.4M 198M 1%
/run\n/dev/sda3 31G 9.1G 21G 32% /\ntmpfs 992M
128K 992M 1% /dev/shm\ntmpfs 5.0M 4.0K 5.0M 1%
/run/lock\ntmpfs 4.0M 0 4.0M 0%
/sys/fs/cgroup\n/dev/sda2 512M 7.9M 505M 2%
/boot/efi\ntmpfs 199M 104K 199M 1%
/run/user/1000\n/dev/sr0 59M 59M 0 100%
/media/ansible/VBox_GAs_6.1.18", "stdout_lines":
["/home/ansible/Desktop", "docker.yml", "shell.sh", "shell.yml",
"Filesystem Size Used Avail Use% Mounted on", "tmpfs
199M 1.4M 198M 1% /run", "/dev/sda3 31G 9.1G 21G 32% /",
"tmpfs 992M 128K 992M 1% /dev/shm", "tmpfs 5.0M
4.0K 5.0M 1% /run/lock", "tmpfs 4.0M 0 4.0M 0%
/sys/fs/cgroup", "/dev/sda2 512M 7.9M 505M 2% /boot/efi",
"tmpfs 199M 104K 199M 1% /run/user/1000",
"/dev/sr0 59M 59M 0 100% /media/ansible/VBox_GAs_6.1.18"]}
TASK [Execute shell script] *******************************************************************************************************************************************************************************
changed: [localhost] => {"changed": true, "cmd": "/home/ansible/Desktop/shell.sh",
"delta": "0:00:00.002609", "end": "2021-04-23 13:49:31.076033", "rc":
0, "start": "2021-04-23 13:49:31.073424", "stderr": "", "stderr_lines":
[], "stdout": "Running a Ansible Playbook", "stdout_lines": ["Running a Ansible Playbook"]}
PLAY
RECAP
************************************************************************************************************************************************************************************************
localhost : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
The other command output is as follows
ansible@ansible-VirtualBox:~/Desktop$ ansible-playbook -vvv shell.yml
ansible-playbook 2.10.8
config file = None
configured module search path = ['/home/ansible/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/lib/python3.8/dist-packages/ansible
executable location = /usr/local/bin/ansible-playbook
python version = 3.8.6 (default, Jan 27 2021, 15:42:20) [GCC 10.2.0]
No config file found; using defaults
host_list declined parsing /etc/ansible/hosts as it did not pass its verify_file() method
script declined parsing /etc/ansible/hosts as it did not pass its verify_file() method
auto declined parsing /etc/ansible/hosts as it did not pass its verify_file() method
Parsed /etc/ansible/hosts inventory source with ini plugin
Skipping callback 'default', as we already have a stdout callback.
Skipping callback 'minimal', as we already have a stdout callback.
Skipping callback 'oneline', as we already have a stdout callback.
PLAYBOOK:
shell.yml
***************************************************************************************************************************************************************************************
1 plays in shell.yml
PLAY
[localhost]
******************************************************************************************************************************************************************************************
TASK
[Gathering Facts]
************************************************************************************************************************************************************************************
task path: /home/ansible/Desktop/shell.yml:2
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: ansible
<localhost> EXEC /bin/sh -c 'echo ~ansible && sleep 0'
<localhost>
EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo
/home/ansible/.ansible/tmp `"&& mkdir "` echo
/home/ansible/.ansible/tmp/ansible-tmp-1619165993.756536-4014-251215842015888
`" && echo
ansible-tmp-1619165993.756536-4014-251215842015888="` echo
/home/ansible/.ansible/tmp/ansible-tmp-1619165993.756536-4014-251215842015888
`" ) && sleep 0'
<localhost> Attempting python interpreter discovery
<localhost>
EXEC /bin/sh -c 'echo PLATFORM; uname; echo FOUND; command -v
'"'"'/usr/bin/python'"'"'; command -v '"'"'python3.7'"'"'; command -v
'"'"'python3.6'"'"'; command -v '"'"'python3.5'"'"'; command -v
'"'"'python2.7'"'"'; command -v '"'"'python2.6'"'"'; command -v
'"'"'/usr/libexec/platform-python'"'"'; command -v
'"'"'/usr/bin/python3'"'"'; command -v '"'"'python'"'"'; echo ENDFOUND
&& sleep 0'
<localhost> EXEC /bin/sh -c '/usr/bin/python3 && sleep 0'
Using module file /usr/local/lib/python3.8/dist-packages/ansible/modules/setup.py
<localhost>
PUT /home/ansible/.ansible/tmp/ansible-local-4010hi_z_3cp/tmpoo7k3m55
TO
/home/ansible/.ansible/tmp/ansible-tmp-1619165993.756536-4014-251215842015888/AnsiballZ_setup.py
<localhost>
EXEC /bin/sh -c 'chmod u+x
/home/ansible/.ansible/tmp/ansible-tmp-1619165993.756536-4014-251215842015888/
/home/ansible/.ansible/tmp/ansible-tmp-1619165993.756536-4014-251215842015888/AnsiballZ_setup.py
&& sleep 0'
<localhost> EXEC /bin/sh -c
'/usr/bin/python3
/home/ansible/.ansible/tmp/ansible-tmp-1619165993.756536-4014-251215842015888/AnsiballZ_setup.py
&& sleep 0'
<localhost> EXEC /bin/sh -c 'rm -f -r
/home/ansible/.ansible/tmp/ansible-tmp-1619165993.756536-4014-251215842015888/
> /dev/null 2>&1 && sleep 0'
ok: [localhost]
META: ran handlers
TASK [Execute shell command] ******************************************************************************************************************************************************************************
task path: /home/ansible/Desktop/shell.yml:5
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: ansible
<localhost> EXEC /bin/sh -c 'echo ~ansible && sleep 0'
<localhost>
EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo
/home/ansible/.ansible/tmp `"&& mkdir "` echo
/home/ansible/.ansible/tmp/ansible-tmp-1619165994.531177-4083-230473977652675
`" && echo
ansible-tmp-1619165994.531177-4083-230473977652675="` echo
/home/ansible/.ansible/tmp/ansible-tmp-1619165994.531177-4083-230473977652675
`" ) && sleep 0'
Using module file /usr/local/lib/python3.8/dist-packages/ansible/modules/command.py
<localhost>
PUT /home/ansible/.ansible/tmp/ansible-local-4010hi_z_3cp/tmpy6c7des_
TO
/home/ansible/.ansible/tmp/ansible-tmp-1619165994.531177-4083-230473977652675/AnsiballZ_command.py
<localhost>
EXEC /bin/sh -c 'chmod u+x
/home/ansible/.ansible/tmp/ansible-tmp-1619165994.531177-4083-230473977652675/
/home/ansible/.ansible/tmp/ansible-tmp-1619165994.531177-4083-230473977652675/AnsiballZ_command.py
&& sleep 0'
<localhost> EXEC /bin/sh -c
'/usr/bin/python3
/home/ansible/.ansible/tmp/ansible-tmp-1619165994.531177-4083-230473977652675/AnsiballZ_command.py
&& sleep 0'
<localhost> EXEC /bin/sh -c 'rm -f -r
/home/ansible/.ansible/tmp/ansible-tmp-1619165994.531177-4083-230473977652675/
> /dev/null 2>&1 && sleep 0'
changed: [localhost] => {
"changed": true,
"cmd": "pwd\nls\ndf -h\n",
"delta": "0:00:00.003387",
"end": "2021-04-23 13:49:54.788373",
"invocation": {
"module_args": {
"_raw_params": "pwd\nls\ndf -h\n",
"_uses_shell": true,
"argv": null,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"stdin_add_newline": true,
"strip_empty_ends": true,
"warn": true
}
},
"rc": 0,
"start": "2021-04-23 13:49:54.784986",
"stderr": "",
"stderr_lines": [],
"stdout":
"/home/ansible/Desktop\ndocker.yml\nshell.sh\nshell.yml\nFilesystem
Size Used Avail Use% Mounted on\ntmpfs 199M 1.4M 198M 1%
/run\n/dev/sda3 31G 9.1G 21G 32% /\ntmpfs 992M
128K 992M 1% /dev/shm\ntmpfs 5.0M 4.0K 5.0M 1%
/run/lock\ntmpfs 4.0M 0 4.0M 0%
/sys/fs/cgroup\n/dev/sda2 512M 7.9M 505M 2%
/boot/efi\ntmpfs 199M 104K 199M 1%
/run/user/1000\n/dev/sr0 59M 59M 0 100%
/media/ansible/VBox_GAs_6.1.18",
"stdout_lines": [
"/home/ansible/Desktop",
"docker.yml",
"shell.sh",
"shell.yml",
"Filesystem Size Used Avail Use% Mounted on",
"tmpfs 199M 1.4M 198M 1% /run",
"/dev/sda3 31G 9.1G 21G 32% /",
"tmpfs 992M 128K 992M 1% /dev/shm",
"tmpfs 5.0M 4.0K 5.0M 1% /run/lock",
"tmpfs 4.0M 0 4.0M 0% /sys/fs/cgroup",
"/dev/sda2 512M 7.9M 505M 2% /boot/efi",
"tmpfs 199M 104K 199M 1% /run/user/1000",
"/dev/sr0 59M 59M 0 100% /media/ansible/VBox_GAs_6.1.18"
]
}
TASK [Execute shell script] *******************************************************************************************************************************************************************************
task path: /home/ansible/Desktop/shell.yml:11
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: ansible
<localhost> EXEC /bin/sh -c 'echo ~ansible && sleep 0'
<localhost>
EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo
/home/ansible/.ansible/tmp `"&& mkdir "` echo
/home/ansible/.ansible/tmp/ansible-tmp-1619165994.8185554-4113-157803549902887
`" && echo
ansible-tmp-1619165994.8185554-4113-157803549902887="` echo
/home/ansible/.ansible/tmp/ansible-tmp-1619165994.8185554-4113-157803549902887
`" ) && sleep 0'
Using module file /usr/local/lib/python3.8/dist-packages/ansible/modules/command.py
<localhost>
PUT /home/ansible/.ansible/tmp/ansible-local-4010hi_z_3cp/tmpx_72yvf1
TO
/home/ansible/.ansible/tmp/ansible-tmp-1619165994.8185554-4113-157803549902887/AnsiballZ_command.py
<localhost>
EXEC /bin/sh -c 'chmod u+x
/home/ansible/.ansible/tmp/ansible-tmp-1619165994.8185554-4113-157803549902887/
/home/ansible/.ansible/tmp/ansible-tmp-1619165994.8185554-4113-157803549902887/AnsiballZ_command.py
&& sleep 0'
<localhost> EXEC /bin/sh -c
'/usr/bin/python3
/home/ansible/.ansible/tmp/ansible-tmp-1619165994.8185554-4113-157803549902887/AnsiballZ_command.py
&& sleep 0'
<localhost> EXEC /bin/sh -c 'rm -f -r
/home/ansible/.ansible/tmp/ansible-tmp-1619165994.8185554-4113-157803549902887/
> /dev/null 2>&1 && sleep 0'
changed: [localhost] => {
"changed": true,
"cmd": "/home/ansible/Desktop/shell.sh",
"delta": "0:00:00.002630",
"end": "2021-04-23 13:49:54.970375",
"invocation": {
"module_args": {
"_raw_params": "/home/ansible/Desktop/shell.sh",
"_uses_shell": true,
"argv": null,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"stdin_add_newline": true,
"strip_empty_ends": true,
"warn": true
}
},
"rc": 0,
"start": "2021-04-23 13:49:54.967745",
"stderr": "",
"stderr_lines": [],
"stdout": "Running a Ansible Playbook",
"stdout_lines": [
"Running a Ansible Playbook"
]
}
META: ran handlers
META: ran handlers
PLAY
RECAP
************************************************************************************************************************************************************************************************
localhost : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible@ansible-VirtualBox:~/Desktop$
Check
the following link to find more information on shell executable in
Ansible ->
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/shell_module.html

