From charlesreid1

Line 2: Line 2:


Ansible can wrap around Vagrant and use it to automate the management of remote machines. This is useful for testing Ansible locally if you need to test it with multiple machines or particular architectures.
Ansible can wrap around Vagrant and use it to automate the management of remote machines. This is useful for testing Ansible locally if you need to test it with multiple machines or particular architectures.
==Using Ansible Locally with Vagrant==


[[Vagrant]] allows you to set up virtual machine(s) using VirtualBox, which can give you a way of testing Ansible scripts locally (without using the AWS or Google Cloud platforms).  
[[Vagrant]] allows you to set up virtual machine(s) using VirtualBox, which can give you a way of testing Ansible scripts locally (without using the AWS or Google Cloud platforms).  
Line 9: Line 7:
Details of how to connect to vagrant boxes can be added to the Ansible hosts file, or machines can be managed using a dynamic inventory script (see [[Ansible/Hosts/Dynamic Inventory]] for an example Vagrant dynamic inventory script). This will allow Ansible to connect to the remote machines and execute commands.
Details of how to connect to vagrant boxes can be added to the Ansible hosts file, or machines can be managed using a dynamic inventory script (see [[Ansible/Hosts/Dynamic Inventory]] for an example Vagrant dynamic inventory script). This will allow Ansible to connect to the remote machines and execute commands.


===Before you begin: set up vagrant box===
==Before you begin: set up vagrant box==


See the [https://charlesreid1.com/wiki/Vagrant#Basic_Startup_Shutdown_Procedure Basic Startup and Shutdown Procedure] section of the [[Vagrant]] page for steps to set up a Vagrant box, and connect to it via SSH.
See the [https://charlesreid1.com/wiki/Vagrant#Basic_Startup_Shutdown_Procedure Basic Startup and Shutdown Procedure] section of the [[Vagrant]] page for steps to set up a Vagrant box, and connect to it via SSH.
Line 47: Line 45:
Note: for multiple vagrant boxes, see the guide on the [https://charlesreid1.com/wiki/Ansible/Full_Stack_Playbook#Vagrant_multi-machine_setup Ansible/Full Stack Playbook] page.
Note: for multiple vagrant boxes, see the guide on the [https://charlesreid1.com/wiki/Ansible/Full_Stack_Playbook#Vagrant_multi-machine_setup Ansible/Full Stack Playbook] page.


===Connect Ansible to Vagrant===
==Connect Ansible to Vagrant==


Use the hosts file to tell Ansible how to connect to the (Vagrant) hosts.
Use the hosts file to tell Ansible how to connect to the (Vagrant) hosts.


====Managing hosts file by hand====
===Managing hosts file by hand===


Edit <code>playbooks/hosts</code> and include details about each vagrant host.
Edit <code>playbooks/hosts</code> and include details about each vagrant host.
Line 75: Line 73:
Your vagrant boxes should be up and running with the <code>vagrant up</code> command.
Your vagrant boxes should be up and running with the <code>vagrant up</code> command.


====Managing hosts dynamically====
===Managing hosts dynamically===


{{Main|Ansible/Hosts/Dynamic Inventory}}
{{Main|Ansible/Hosts/Dynamic Inventory}}
Line 87: Line 85:
</pre>
</pre>


===Ping Vagrant box===
==Ping Vagrant box==


To ping the Vagrant machine (that Ansible is managing), use Ansible's ping module.
To ping the Vagrant machine (that Ansible is managing), use Ansible's ping module.
Line 121: Line 119:




===Use a config file===
==Use a config file==


The inventory file (hosts) required lots of details, and if we have many nodes to deal with that is not going to scale well.
The inventory file (hosts) required lots of details, and if we have many nodes to deal with that is not going to scale well.
Line 138: Line 136:
This dir structure allows you to place the config file, plus the playbooks, under version control in a single repo.
This dir structure allows you to place the config file, plus the playbooks, under version control in a single repo.


===Example config file===
==Example config file==


To specify default values for SSH key, username, and inventory filename, use this config file:
To specify default values for SSH key, username, and inventory filename, use this config file:
Line 166: Line 164:
</pre>
</pre>


===Executing arbitrary commands===
==Executing arbitrary commands==


We have already seen the <code>-m</code> flag used with ansible to specify an ansible module.  
We have already seen the <code>-m</code> flag used with ansible to specify an ansible module.  
Line 191: Line 189:
</pre>
</pre>


====Important Note====
===Important Note===


The command module '''is actually the default module'''!!! So we don't need to specify <code>-m command</code>, that is the default value:
The command module '''is actually the default module'''!!! So we don't need to specify <code>-m command</code>, that is the default value:
Line 211: Line 209:
</pre>
</pre>


===Executing root commands===
==Executing root commands==


To run commands that require root access, pass the <code>-b</code> flag to BECOME the root user:
To run commands that require root access, pass the <code>-b</code> flag to BECOME the root user:
Line 219: Line 217:
</pre>
</pre>


===Installing packages===
==Installing packages==


The apt module is useful for installing packages. Here's how you would install nginx:
The apt module is useful for installing packages. Here's how you would install nginx:
Line 233: Line 231:
</pre>
</pre>


===Starting services===
==Starting services==


Once nginx is installed, the service can be started using the service module:
Once nginx is installed, the service can be started using the service module:

Revision as of 01:34, 11 November 2018

Vagrant is a command line interface to VirtualBox that allows you to create virtual machines from the command line and manage them.

Ansible can wrap around Vagrant and use it to automate the management of remote machines. This is useful for testing Ansible locally if you need to test it with multiple machines or particular architectures.

Vagrant allows you to set up virtual machine(s) using VirtualBox, which can give you a way of testing Ansible scripts locally (without using the AWS or Google Cloud platforms).

Details of how to connect to vagrant boxes can be added to the Ansible hosts file, or machines can be managed using a dynamic inventory script (see Ansible/Hosts/Dynamic Inventory for an example Vagrant dynamic inventory script). This will allow Ansible to connect to the remote machines and execute commands.

Before you begin: set up vagrant box

See the Basic Startup and Shutdown Procedure section of the Vagrant page for steps to set up a Vagrant box, and connect to it via SSH.

Briefly:

To initialize a vagrant box:

vagrant init ubuntu/xenial64

To bring it up:

vagrant up

For info about the ssh configuration of the box:

vagrant ssh-config

To ssh into the box:

vagrant ssh

To halt the machine:

vagrant halt

Note: for multiple vagrant boxes, see the guide on the Ansible/Full Stack Playbook page.

Connect Ansible to Vagrant

Use the hosts file to tell Ansible how to connect to the (Vagrant) hosts.

Managing hosts file by hand

Edit playbooks/hosts and include details about each vagrant host.

Use the command vagrant ssh-config to get these details.

Example:

$ cat playbooks/hosts

myvagrantbox ansible_host=127.0.0.1 ansible_port=2222 ansible_user=vagrant ansible_private_key_file=.vagrant/machines/default/virtualbox/private_key

Side note: once you are ready to run with an AWS EC2 node, you would specify the hostname of the EC2 node and the AWS .pem private key, like so:

$ cat playbooks/hosts

myamazonbox ansible_host=ec2-203-0-113-120.compute-1.amazonaws.com ansible_user=ubuntu ansible_private_key_file=/path/to/keyfile.pem

Your vagrant boxes should be up and running with the vagrant up command.

Managing hosts dynamically

There is a dynamic inventory management script that interacts with vagrant to provide Ansible with machine details: https://charlesreid1.com/wiki/Ansible/Hosts/Dynamic_Inventory#Putting_it_all_together

This can be used by specifying the path to the executable dynamic inventory script using the -i flag when running ansible:

ansible -i dynamic_vagrant.py ...

Ping Vagrant box

To ping the Vagrant machine (that Ansible is managing), use Ansible's ping module.

Call Ansible and the name of the machine (must be listed in the hosts file).

Pass it the inventory file name with the -i flag, and the ping module with the -m flag:

ansible myvagrantbox -i hosts -m ping

or if you're using a dynamic inventory file,

ansible myvagrantbox -i hosts -m ping

You should see some output like this:

myvagrantbox | success >> {
    "changed": false,
    "ping": "pong"
}

If it did not succeed, re-run with the -vvvv flag for max verbosity to help debug the issue.

This is a basic module that runs a ping test; the server should respond with a pong on the other end.

The "'changed': false" indicates that Ansible is not changing the state of the machine.


Use a config file

The inventory file (hosts) required lots of details, and if we have many nodes to deal with that is not going to scale well.

To make it easier to deal with, we can use the ansible.cfg file to set some defaults and variables.

Where to put ansible.cfg? Here is where Ansible looks:

  • ANSIBLE_CONFIG env var (specifies the config file)
  • /ansible.cfg (current directory)
  • ~/.ansible.cfg (.ansible.cfg in your home directory
  • /etc/ansible/ansible.cfg

A good place for it is alongside the playbooks, in the current directory.

This dir structure allows you to place the config file, plus the playbooks, under version control in a single repo.

Example config file

To specify default values for SSH key, username, and inventory filename, use this config file:

ansible.cfg:

[defaults]
inventory = hosts
remote_user = vagrant
private_key_file = .vagrant/machines/default/virtualbox/private_key
host_key_checking = False

Now we can also simplify the hosts file:

playbooks/hosts:

myvagrantbox ansible_host=127.0.0.1 ansible_port=2222

Because the hosts file is already specified as the inventory file, we don't have to use the -i flag:

$ ansible myvagrantbox -m ping

Executing arbitrary commands

We have already seen the -m flag used with ansible to specify an ansible module.

We can use the command module to run arbitrary commands on the vagrant machines that ansible is managing.

To run a command on the remote machine via ansible:

ansible myvagrantbox -m command -a <the command to run>

For example, to run the uptime command:

ansible myvagrantbox -m command -a uptime

Results in:

testserver | success | rc=0 >>
 17:14:07 up  1:16,  1 user,  load average: 0.16, 0.05, 0.04

Important Note

The command module is actually the default module!!! So we don't need to specify -m command, that is the default value:

ansible myvagrantbox -a <the command to run>

For example:

ansible myvagrantbox -a uptime

If the command has spaces in it, use quotes:

ansible myvagrantbox -a "tail /var/log/dmesg"

Executing root commands

To run commands that require root access, pass the -b flag to BECOME the root user:

$ ansible myvagrantbox -b -a "tail /var/log/syslog"

Installing packages

The apt module is useful for installing packages. Here's how you would install nginx:

ansible myvagrantbox -b -m apt -a name=nginx

IMPORTANT: The first apt module command you give should also run the equivalent of apt-get update before the package is installed. To do that, change the name to name="nginx update_cache=yes":

ansible myvagrantbox -b -m apt -a name="nginx update_cache=yes"

Starting services

Once nginx is installed, the service can be started using the service module:

ansible myvagrantbox -b -m service -a "name=nginx state=restarted"

equivalent of running sudo service nginx restart