From 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sun, 7 Apr 2024 13:41:34 -0500 Subject: new repository --- devdocs/vagrant/provisioning%2Fansible_intro.html | 132 ++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 devdocs/vagrant/provisioning%2Fansible_intro.html (limited to 'devdocs/vagrant/provisioning%2Fansible_intro.html') diff --git a/devdocs/vagrant/provisioning%2Fansible_intro.html b/devdocs/vagrant/provisioning%2Fansible_intro.html new file mode 100644 index 00000000..98427e42 --- /dev/null +++ b/devdocs/vagrant/provisioning%2Fansible_intro.html @@ -0,0 +1,132 @@ +

Ansible and Vagrant

The information below is applicable to both Vagrant Ansible provisioners:

The list of common options for these two provisioners is documented in a separate documentation page.

This documentation page will not go into how to use Ansible or how to write Ansible playbooks, since Ansible is a complete deployment and configuration management system that is beyond the scope of Vagrant documentation.

To learn more about Ansible, please consult the Ansible Documentation Site.

The Playbook File

The first component of a successful Ansible provisioner setup is the Ansible playbook which contains the steps that should be run on the guest. Ansible's playbook documentation goes into great detail on how to author playbooks, and there are a number of best practices that can be applied to use Ansible's powerful features effectively.

A playbook that installs and starts (or restarts) the NTP daemon via YUM looks like:

---
+- hosts: all
+  tasks:
+    - name: ensure ntpd is at the latest version
+      yum: pkg=ntp state=latest
+      notify:
+      - restart ntpd
+  handlers:
+    - name: restart ntpd
+      service: name=ntpd state=restarted
+
+

You can of course target other operating systems that do not have YUM by changing the playbook tasks. Ansible ships with a number of modules that make running otherwise tedious tasks dead simple.

Running Ansible

The playbook option is strictly required by both Ansible provisioners (ansible and ansible_local), as illustrated in this basic Vagrantfile` configuration:

Vagrant.configure("2") do |config|
+
+  # Use :ansible or :ansible_local to
+  # select the provisioner of your choice
+  config.vm.provision :ansible do |ansible|
+    ansible.playbook = "playbook.yml"
+  end
+end
+
+

Since an Ansible playbook can include many files, you may also collect the related files in a directory structure like this:

.
+|-- Vagrantfile
+|-- provisioning
+|   |-- group_vars
+|           |-- all
+|   |-- roles
+|           |-- bar
+|           |-- foo
+|   |-- playbook.yml
+
+

In such an arrangement, the ansible.playbook path should be adjusted accordingly:

Vagrant.configure("2") do |config|
+  config.vm.provision "ansible" do |ansible|
+    ansible.playbook = "provisioning/playbook.yml"
+  end
+end
+
+

The Inventory File

When using Ansible, it needs to know on which machines a given playbook should run. It does this by way of an inventory file which lists those machines. In the context of Vagrant, there are two ways to approach working with inventory files.

Auto-Generated Inventory

The first and simplest option is to not provide one to Vagrant at all. Vagrant will generate an inventory file encompassing all of the virtual machines it manages, and use it for provisioning machines.

Example with the ansible provisioner

# Generated by Vagrant
+
+default ansible_ssh_host=127.0.0.1 ansible_ssh_port=2200 ansible_ssh_user='vagrant' ansible_ssh_private_key_file='/home/.../.vagrant/machines/default/virtualbox/private_key'
+
+

Note that the generated inventory file is stored as part of your local Vagrant environment in .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory.

Example with the ansible_local provisioner

# Generated by Vagrant
+
+default ansible_connection=local
+
+

Note that the generated inventory file is uploaded to the guest VM in a subdirectory of tmp_path, e.g. /tmp/vagrant-ansible/inventory/vagrant_ansible_local_inventory.

Host Variables

As of Vagrant 1.8.0, the host_vars option can be used to set variables for individual hosts in the generated inventory file (see also the notes on group variables below).

With this configuration example:

Vagrant.configure("2") do |config|
+  config.vm.define "host1"
+  config.vm.define "host2"
+  config.vm.provision "ansible" do |ansible|
+    ansible.playbook = "playbook.yml"
+    ansible.host_vars = {
+      "host1" => {"http_port" => 80,
+                  "maxRequestsPerChild" => 808},
+      "host2" => {"http_port" => 303,
+                  "maxRequestsPerChild" => 909}
+    }
+  end
+end
+
+

Vagrant would generate the following inventory file:

# Generated by Vagrant
+
+host1 ansible_ssh_host=... http_port=80 maxRequestsPerChild=808
+host2 ansible_ssh_host=... http_port=303 maxRequestsPerChild=909
+
+

Groups and Group Variables

The groups option can be used to pass a hash of group names and group members to be included in the generated inventory file.

As of Vagrant 1.8.0, it is also possible to specify group variables, and group members as host ranges (with numeric or alphabetic patterns).

With this configuration example:

Vagrant.configure("2") do |config|
+
+  config.vm.box = "ubuntu/trusty64"
+
+  config.vm.define "machine1"
+  config.vm.define "machine2"
+
+  config.vm.provision "ansible" do |ansible|
+    ansible.playbook = "playbook.yml"
+    ansible.groups = {
+      "group1" => ["machine1"],
+      "group2" => ["machine2"],
+      "group3" => ["machine[1:2]"],
+      "group4" => ["other_node-[a:d]"], # silly group definition
+      "all_groups:children" => ["group1", "group2"],
+      "group1:vars" => {"variable1" => 9,
+                        "variable2" => "example"}
+    }
+  end
+end
+
+

Vagrant would generate the following inventory file:

# Generated by Vagrant
+
+machine1 ansible_ssh_host=127.0.0.1 ansible_ssh_port=2200 ansible_ssh_user='vagrant' ansible_ssh_private_key_file='/home/.../.vagrant/machines/machine1/virtualbox/private_key'
+machine2 ansible_ssh_host=127.0.0.1 ansible_ssh_port=2222 ansible_ssh_user='vagrant' ansible_ssh_private_key_file='/home/.../.vagrant/machines/machine2/virtualbox/private_key'
+
+[group1]
+machine1
+
+[group2]
+machine2
+
+[group3]
+machine[1:2]
+
+[group4]
+other_node-[a:d]
+
+[all_groups:children]
+group1
+group2
+
+[group1:vars]
+variable1=9
+variable2=example
+
+

Notes:

Static Inventory

The second option is for situations where you would like to have more control over the inventory management.

With the inventory_path option, you can reference a specific inventory resource (e.g. a static inventory file, a dynamic inventory script or even multiple inventories stored in the same directory). Vagrant will then use this inventory information instead of generating it.

A very simple inventory file for use with Vagrant might look like:

default ansible_ssh_host=192.168.111.222
+
+

Where the above IP address is one set in your Vagrantfile:

config.vm.network :private_network, ip: "192.168.111.222"
+
+

Notes:

The Ansible Configuration File

Certain settings in Ansible are (only) adjustable via a configuration file, and you might want to ship such a file in your Vagrant project.

When shipping an Ansible configuration file it is good to know that:

+

+ © 2010–2018 Mitchell Hashimoto
Licensed under the MPL 2.0 License.
+ https://www.vagrantup.com/docs/provisioning/ansible_intro.html +

+
-- cgit v1.2.3