Skip to main content

Configuring Ansible Provisioner

1. Setting Playbook Path

Option Values Description
ansible.playbook site.yml Path to the main playbook file relative to the Vagrantfile
ansible.config_file ansible.cfg Path to the Ansible configuration file (optional)

Example: Setting Playbook Path

Vagrant.configure("2") do |config|
  config.vm.provision :ansible do |ansible|
    ansible.playbook = "playbooks/site.yml"
  end
end

2. Configuring Inventory Path

Option Values Description
ansible.inventory string Path to the Ansible inventory file. Defaults to ansible/inventory.
ansible.inventory_path string Alternative way to specify the inventory path. Overrides ansible.inventory.

Example: Setting Inventory Path

vagrant<span>.</span>provision<span>.</span>ansible<span>.</span>inventory_path <span>=</span> "./ansible/inventory"

3. Using Limit Option

Option Description Usage
limit Restricts the Ansible provisioner to run only on a subset of hosts limit = "web:<tag>"

Example: Using Limit Option

Vagrant.configure("2") do |config|
  config.vm.provision "ansible" do |ansible|
    ansible.playbook = "playbook.yml"
    ansible.limit = "web:<tag>"
  end
end
Note: The limit option is passed directly to the Ansible command as --limit.

4. Setting Extra Variables

Variable Description Example
ansible.extra_vars Extra variables to pass to the Ansible provisioner {"app_env": "production", "debug": false}

Example: Setting Extra Variables in Vagrantfile

Vagrant.configure("2") do |config|
  config.vm.provision "ansible" do |ansible|
    ansible.extra_vars = {
      app_env: "production",
      debug: false
    }
  end
end
Note: Extra variables can be passed as a hash or a path to a JSON/YAML file.

5. Configuring Verbose Mode

Option Values Description
ansible.verbose false Enable verbose output for Ansible provisioner
ansible.verbosity 0 Set verbosity level (0-4)

Example: Setting Verbose Mode in Vagrantfile

Vagrant.configure("2") do |config|
  config.vm.provision :ansible do |ansible|
    ansible.verbose = true
    ansible.verbosity = 3
  end
end
Note: Verbose mode is useful for debugging playbook execution and understanding what Ansible is doing under the hood.

6. Using Tags

Tag Description Usage
tags List of tags to apply to a provisioner "tags": ["web", "database"]
skip_tags List of tags to skip during provisioning "skip_tags": ["monitoring"]

Example: Using Tags in Vagrantfile

Vagrant.configure("2") do |config|
  config.vm.provision "ansible" do |ansible|
    ansible.playbook = "playbook.yml"
    ansible.tags = ["web", "api"]
  end
end

Example: Skipping Tags in Vagrantfile

Vagrant.configure("2") do |config|
  config.vm.provision "ansible" do |ansible|
    ansible.playbook = "playbook.yml"
    ansible.skip_tags = ["monitoring", "logging"]
  end
end
Note: Tags are useful for selective execution of Ansible tasks during Vagrant provisioning.

7. Setting Galaxy Role File

Property Values Description
ansible.galaxy_role_file roles/requirements.yml Path to the Ansible Galaxy roles file that defines roles to be installed.

Example: Setting Galaxy Role File

Vagrant.configure("2") do |config|
  config.vm.provision "ansible" do |ansible|
    ansible.galaxy_role_file = "roles/requirements.yml"
  end
end

8. Installing Galaxy Roles

Option Values Description
ansible.galaxy_role string Name of the role to install from Ansible Galaxy
ansible.galaxy_role_path string Path where roles are installed (default: ~/.ansible/roles)
ansible.galaxy_role_version string Specific version of the role to install (e.g., v1.2.3)

Example: Installing a role from Galaxy

vagrant init ubuntu/focal64
vagrant up

# In Vagrantfile:
config.vm.provision "ansible" do |ansible|
  ansible.playbook = "playbook.yml"
  ansible.galaxy_role = "geerlingguy.nginx"
  ansible.galaxy_role_version = "2.0.0"
end
Note: Ensure ansible-galaxy is installed on the host machine to manage roles.

9. Using Vault Password

Option Values Notes
ansible.vault_password_file path/to/vault-password-file Path to the file containing the vault password
ansible.ask_vault_pass true or false Ask for vault password at runtime

Example: Using Vault Password File

Vagrant.configure("2") do |config|
  config.vm.provision "ansible" do |ansible|
    ansible.playbook = "playbook.yml"
    ansible.vault_password_file = "/home/user/.vault_pass"
  end
end
Note: The vault password file must contain only the password with no extra whitespace or newlines.

10. Configuring Host Variables

Variable Type Description Example
ansible.host_vars Object Defines variables for specific hosts in the inventory
{
  "web01": {
    "ansible_host": "192.168.10.10",
    "http_port": 80
  }
}
ansible.group_vars Object Assigns variables to groups of hosts
{
  "webservers": {
    "http_port": 80,
    "max_connections": 1000
  }
}

Example: Setting Host Variables in Vagrantfile

Vagrant.configure("2") do |config|
  config.vm.provision "ansible" do |ansible|
    ansible.playbook = "playbook.yml"
    ansible.host_vars = {
      "web01" => {
        "ansible_host" => "192.168.10.10",
        "http_port" => 80
      }
    }
  end
end

11. Setting Compatibility Mode

Option Values Description
ansible.compatibility_mode "2.15", "2.16" Specifies the Ansible version compatibility mode to use for provisioner behavior.

Example: Setting Compatibility Mode

config.vm.provision "ansible" do |ansible|
  ansible.compatibility_mode = "2.16"
end
Note: Setting compatibility mode ensures that Vagrant's Ansible provisioner behaves according to the specified version's expectations, especially when dealing with newer or legacy Ansible features.

Was this article helpful?