DEV Community

Cover image for Quickstart Ansible With Virtual Machines
Tu Van Nguyen
Tu Van Nguyen

Posted on • Updated on

Quickstart Ansible With Virtual Machines

Compared to similar tools such as Puppet and Chef, Ansible is probably the best tool to get started in configuration management. It is agentless, so it doesn't require client software to be installed on the machines that will be managed by Ansible. Instead, Ansible controls the other machines through OpenSSH. This does mean that Ansible needs to be installed on a Linux machine. With this simple tutorial, you can get started with setting up remote machines to be controlled with Ansible.

System Requirements

Create the Virtual Machine

Download the ISO file for the guest OS. This tutorial uses the CentOS 7 Minimal ISO image because it's lightweight and installs without a GUI. So, it'll take up little memory and install quickly. To ensure the downloaded ISO image is secure, verify the file's SHA256 checksum:
echo "<known SHA 256 sum of the file> <ISO file name>" | sha256sum -c

Create the guest machine on virtualbox, and install the OS onto the guest. The guest machine can have the following specs:

  • OS: CentOS 7 Minimal
  • Base Memory: 1024 MB
  • Hard Disk: 15 GB, Dynamically Allocated

After login in as root, create a user such as "testuser" and specify the password. You need to create this user on the guest machine.

useradd testuser
passwd testuser
usermod -aG wheel testuser #makes testuser sudoer on CentOS

Next, add a host-only adapter to its network configurations. This allows the host to communicate with the guest on a static IP, so that Ansible can communicate with the guest through SSH.

The IPv4 address for the host adapter can be left as the default 192.168.56.1. But the address that Ansible will use to connect to the machine will be slightly different. This address can be found by running ip address on the guest. From the output below, the address is 192.168.56.3.

[testuser@localhost ~]$ ip address
...
3: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:c3:b8:38 brd ff:ff:ff:ff:ff:ff
    inet 192.168.56.3/24 brd 192.168.56.255 scope global noprefixroute dynamic eth0

Test the network connection to the virtual machine using ping and ssh. If the connection succeeds, the guest is ready to be used with Ansible.

ping 192.168.56.1 # make sure the host-only adapter is working
ssh testuser@192.168.56.3 # make sure host can ssh to virtual machine

Test Ansible with the Virtual Machine

Add the virtual machine to an Ansible inventory by the IPv4 address to /etc/ansible/hosts

# Virtual Machine Managed Nodes
192.168.56.3

Now, we can run some basic commands on Ansible to see if it can control the virtual machine.

ansible all -m ping -u testuser --ask-pass
ansible all -a "/bin/echo hello" -u testuser --ask-pass

Now Give Ansible Access with SSH-keys

To make using ansible a bit easier and more secure, we'll be replacing user-password authentication with SSH keys. We'll need a user on the virtual machine with the same name as the user on the local machine. This tutorial will just use root, but it's highly recommended to create a separate user.

#On the local machine

su #switch user
ssh-keygen #generate SSH key-pair
ssh-copy-id root@192.168.56.3 # copy ssh key to virtual machine

ssh root@192.168.56.3 #test connection
ansible all -m ping #test ansible

Congratulations! Now you can get started learning how to get Ansible to command remote machines to do more complex tasks.

Top comments (0)