Ansible Shell Module Tutorial – Complete Beginner's Guide

17 Comments



The Ansible Shell Module allows you to run arbitrary commands on a remote host, just like you were logged into the shell. The Shell and Command modules are very similar, the major difference being that the shell module does not escape commands, allowing you to use shell operators like redirects (“greater than”, “less than”), pipe (“|”) and boolean operators (“&&”, “||”). This does mean that the Shell module is susceptible to command injection/shell injection, but this is easy enough to overcome by using the “quote” filter when using variables with the Shell module.

– 0:00 — Shell Module vs Command Module
– 3:39 — Basic Shell Module Usage (+ args, environment and loops)
– 5:15 — Preventing Shell Injection

ℹ️ READ THE IN-DEPTH TUTORIAL HERE == http://bit.ly/2CXMois

SETUP/EDITOR/IDE

✅ Visual Studio Code (https://code.visualstudio.com/)
✅ Ansible extension by Microsoft (http://bit.ly/2SdnqoK).
✅ SEE HOW I SET UP ANSIBLE == https://youtu.be/yiooWsMERaY

Thanks for watching and hope it’s entertaining/useful for you! Hit me up in the comments if you have any questions or feedback 💪.

Related & Channel
▶️ http://bit.ly/2VWid3D == Playlist of all Ansible videos
⭐️ http://bit.ly/2HavqCu == Subscribe to our YouTube Channel

Our Site & Socials
🌐 http://bit.ly/2shtKwZ == TopTechSkills.com
🐦 http://bit.ly/2RkgZkk == @TopTechSkills on Twitter
🐦 http://bit.ly/2AA19aN == @PercyGrunwald on Twitter
😃 http://bit.ly/2ACQgEW == @TopTechSkills on Facebook
🕹 http://bit.ly/2CWR3ST == @PercyGrunwald on Twitch

source

17 Comments
    • blank
      Rizwan Khan A
      May 12, 2022 19:38 pm Reply

      Hi Percy , thanks for all of your videos. Clear explanation of all the concepts. It’s very easy so that layman can even do calkwalk. I have a scenario to install python3.7 on windows using ansible I tried using win_package and win_shell but not able to install. Please can I ask you do a video on the same

    • blank
      CartesianColours
      May 12, 2022 19:38 pm Reply

      Tx. Saved some heartache and broken keyboards.

    • blank
      Isuru Rangana Rathnayake
      May 12, 2022 19:38 pm Reply

      thanks a lot

    • blank
      Thomas Smit
      May 12, 2022 19:38 pm Reply

      In which case would shell injection be a practical attack vector? I.e.: in which case does an attacker have access to variables, but not to the playbook, or ssh keys?

    • blank
      ace
      May 12, 2022 19:38 pm Reply

      Got yourself a new sub from PH. Thanks for a very concise video

    • blank
      Jon POK
      May 12, 2022 19:38 pm Reply

      Do have to do some more steps executing powershel script? I have add in playbook  
      tasks:

          – name: powercli script

            script: 1.ps1

      but is not working DO I have to do sth. ANd when You help me start this is it possible to save otput from script?

    • blank
      Juergen Schubert
      May 12, 2022 19:38 pm Reply

      Great video! Thumb up. I just wonder if I also can use the shell module to login to a ssh session and hit ENTER with the task as the first thing I need to do is "accept a EULA".

    • blank
      Amanjot Singh
      May 12, 2022 19:38 pm Reply

      Is there any way to run a shell script for both two hosts from local machine? What exactly we need to do for this? I'm pretty much stuck in this. The only thing I got is to copy the script on the host then running it using the command. Please let me know if there is another way to run the shell script file on the host which is placed on my local machine. Thanks in advance.

    • blank
      Ambhoj Shukla
      May 12, 2022 19:38 pm Reply

      Is it possible to perform ldap client add or remove via ansible playbook in linux?

    • blank
      D1Ck3n
      May 12, 2022 19:38 pm Reply

      Hi, thanks for the video!

      i have a question: is it possible to define a environment like this?:
      HOSTNAMEKT: "echo "$(dmidecode -s system-serial-number)" | tr '[:upper:]' '[:lower:]'"
      i want to change the hostname of a new machine to his serial number.

    • blank
      @Imran@Nikhat@ki@kahaani
      May 12, 2022 19:38 pm Reply

      nice percy sir. thank you sir

    • blank
      nagaraju Nagari
      May 12, 2022 19:38 pm Reply

      Thank you sir, very very effective and useful for me.

    • blank
      Kundan Singh
      May 12, 2022 19:38 pm Reply

      How did you setup your visual studio for ansible

    • blank
      Venkatesh Reddy
      May 12, 2022 19:38 pm Reply

      Hi Sir,
      May i know where u r mentioning the hosts names.Because i see u are not passing any parameters in the command.

    • blank
      manish shrivas
      May 12, 2022 19:38 pm Reply

      Hi Percy,
      Nice tutorial. I want to delete files inside directory. Directory contains authchild1.2.4, authchild1.3.7 and I have one another directory which contains file authchild1. 6.9 I want to copy this file into previous folder which contains 2 files (authchild1.2.4 and authchild1. 3.7) but after copy I want to delete same file which contains text authchild*.

    • blank
      George Snow
      May 12, 2022 19:38 pm Reply

      What platform and editor are you using for your demos?

    • blank
      Keyboard Interrupt
      May 12, 2022 19:38 pm Reply

      Hi I really like your video and Tutorial series, definitely going to recommend it to beginners!, in this one I especially liked you mentioning Shell injection as many people are not aware of security implications of not sanitizing your inputs ^^

      One thing, that might be out of the Beginner Scope, what about idempotency?, and how to achieve it by using Error Handling (https://docs.ansible.com/ansible/latest/user_guide/playbooks_error_handling.html)
      Don't just do `ignore_errors: True` though!!! 😉

      Also Maybe you should point out that most simple shell scripts can be replaced by a hand full of Tasks using the correct modules, and that using the Command Modules like Raw, Command, Shell and so on, should be avoided when a suitable Module is available.

      This is my personal preference though, and getting started by utilizing existing Shell scripts via `Command` and `Shell` and replacing them, as time goes by, with "real" Ansible is still a huge Step in the right direction 😉

      Maybe a great Video Idea would be a Break down of an existing Shell based deployment Script, and breaking it down into Ansible Tasks 🙂

Leave us a comment