One very hand trick up Ansible's sleeve is the ability to install applications on remote servers. Jack Wallen shows you how to make this work.
Ansible is one of those tools that, once you start using it, you'll wonder how you managed without--it's that flexible and useful. From within a single playbook, you can manage software provisioning, configure remote machines, and deploy applications to remote systems in your data center.
I've already walked you through the creation of playbooks, deploying a container, and how to run a command with Ansible. This time around, I'm going to show you how to install a piece of software on a remote server, with nothing but an Ansible playbook consisting of nine lines.
SEE: Hiring kit: Database administrator (TechRepublic Premium)
What you'll need
A machine running Ansible
A remote Linux machine
An SSH key
A user with sudo privileges
How to copy the SSH key to the remote server
The first thing we need to do is to copy our SSH key to the remote server. For those that don't already know how to work with SSH key authentication, see my post: How to set up ssh key authentication.
You should already have an SSH key generated. If not, take care of that now. Once you have your key, copy it to the server with the command:
Where USER is the username on the remote machine and SERVER is the remote machine's IP address. With the SSH key copied, you're ready to go.
How to configure the hosts
Before we create our playbook, let's configure the host. Say the new host will belong to a number of database servers. To do this, you'll want to create a new section in the Ansible hosts file. Open that file for editing with the command:
sudo nano /etc/ansible/hosts
Scroll to the bottom of that file and create a new section that looks like this:
[db_servers] SERVER ansible_user=USER
Where SERVER is the IP address of the remote server and USER is the remote username. You'll want to use the same username that's associated with the SSH key you just copied.
Save and close the file.
How to create the playbook
Since I'll be installing on a Ubuntu-based remote server, I'm going to name this playbook apt.yml. What I'm going to demonstrate is a very basic single app installation.
This playbook will do the following:
Declare the hosts
Gain sudo privileges
Install the latest version of zip
Update the apt cache
The playbook looks like this:
- hosts: db_servers tasks: - name: Ansible apt install packages example become: true become_user: root apt: name: zip state: present update_cache: true
Remember, since this is a YAML file, the indentation must be consistent, otherwise it will fail. Once you have the file set up properly, save and close it.
If you were wanting to install more than one package, you'd have to modify the apt section of the playbook to reflect this change:
apt: name: - zip - wget state: present update_cache: true
Every application would get its own line.
How to run the playbook
Back at the terminal window, issue the command:
ansible-playbook apt.yml --ask-become-pass
You will first be asked for the sudo password, followed by the SSH key authentication password. Once you've successfully authenticated against those two systems, the playbook will successfully run and the zip package will be installed on the remote machine. After Ansible completes, you should see ok=1 in the output, indicating the package was successfully installed. If you install multiple packages, you'll see ok=X (Where X is the number of packages installed).
And that's how you can easily deploy applications to your remote servers, from a single Ansible playbook.
- How to become a database administrator: A cheat sheet (TechRepublic)
- 10 things companies are keeping in their own data centers (TechRepublic download)
- How to use Restricted Shell to limit user access to a Linux system (TechRepublic)
- How to clone a drive from the Linux command line (TechRepublic)
- Snowflake is the Linux SSH GUI you didn't know you needed (TechRepublic)
- How to use the pipe in Linux commands (TechRepublic)
- Following NGINX acquisition, F5 unveils NGINX Controller 3.0 (ZDNet)
- Best cloud services for small businesses (CNET)
- Data Centers: News and How-To Tips (TechRepublic on Flipboard)