Ansible includes a shell module that can be used to execute commands on remote machines.
Ansible is an incredibly flexible tool. With it you can create highly complicated and powerful playbooks that are able to control and manage a large number of servers within your data center. One way you can make ansible even more flexible is using the shell module.
The Ansible shell module enables you to execute commands on remote nodes. I am going to show you how to make use of the shell module inside of your Ansible playbooks. Believe it or not, this tool is quite easy.
SEE: Hiring kit: Database administrator (TechRepublic Premium)
What you'll need
The only things you'll need to make this work are:
An instance of Ansible running (see: How to install the Ansible server management system on Ubuntu)
SSH key authentication setup between the Ansible server and remote machines
A remote server to send the commands to
A user with sudo privileges
Your Ansible hosts file
The first thing to do is create a hosts entry for your remote server(s). To do this, log in to your Ansible server and issue the command:
sudo nano /etc/ansible/hosts
Scroll to the bottom of that file and add a new entry like so (modifying this entry to fit your needs):
Save and close the file.
The Ansible playbook
We're going to create a very simple Ansible playbook that will install an app on the remote machine. Naturally, you can run whatever command you need, but since installing an app requires sudo access, we can demonstrate how that is done.
The playbook we'll create looks like this:
- hosts: nextcloud tasks: - name: Execute a command using the shell module become: true become_user: root shell: apt-get install clamav -y
We must include become: true to instruct Ansible we'll be requiring escalated privileges, and become_user: root to indicate the user that will execute the command. Note: Even though the root user is disabled on Ubuntu, using root in this instance actually works. Finally the shell entry is the command that will be run on the remote machine.
Save and close the file (naming it something like myfile.yml).
How to run the playbook
With our hosts in place and our playbook written, we can now run the playbook. To do that, issue the command:
ansible-playbook myfile.yml --ask-become-pass
The --ask-become-pass option instructs Ansible to ask for the password to be used for the become_user option in the playbook. You will be prompted to type the password. Once you type the become password, you will then be asked for the SSH key authentication password and the playbook will execute. Once the playbook completes, you should have clamav installed on the remote machine (or whatever it is you've chosen to do).
And that's all there is to using the Ansible shell module. Make use of this handy tool to extend the capabilities of your Ansible playbooks.
- How to become a database administrator: A cheat sheet (TechRepublic)
- 10 things companies are keeping in their own data centers (TechRepublic download)
- How to deploy a container with Ansible (TechRepublic)
- How to create a job on Rundeck (TechRepublic)
- How to run remote commands on multiple Linux servers with Parallel-SSH (TechRepublic)
- Red Hat Enterprise Linux 8.1 adds live Linux kernel patching (ZDNet)
- Best cloud services for small businesses (CNET)
- DevOps: More must-read coverage (TechRepublic on Flipboard)