tomeon / vagrant-ansible_auto

Vagrant plugin for building ansible inventory files.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Vagrant::AnsibleAuto

Build Status Code Climate Coverage Status Gem Version

This Vagrant plugin provides the ansible_auto provisioner that automatically sets up the provisioned guest as an Ansible control machine for the nodes defined in your Vagrantfile. It also provides the vagrant ansible subcommand that generates an inventory file for use on your Vagrant host machine.

Installation

Install with:

$ vagrant plugin install vagrant-ansible_auto

Usage

Inventory Generation

Say you have a Vagrantfile with the following contents:

Vagrant.configure(2) do |config|
  config.vm.box = 'hashicorp/precise64'

  (1..2).each do |i|
    name = "ansible-test-worker-#{i}"
    config.vm.define name do |target|
    end
  end

  config.vm.define 'ansible-test-control' do |machine|
    machine.vm.provision :ansible_auto do |ansible|
      ansible.limit = '*'
      ansible.playbook = 'playbooks/test.yml'
    end
  end

  config.ansible.groups = {
    'control'           => %w(ansible-test-control),
    'worker'            => %w(ansible-test-worker-1 ansible-test-worker-2),
    'cluster:children'  => %w(control worker),
  }
end

Running vagrant ansible inventory will print this Ansible inventory:

ansible-test-worker-1 ansible_ssh_user=vagrant ansible_ssh_host=127.0.0.1 ansible_ssh_port=2222 ansible_ssh_private_key_file=/home/user/vagrant/cluster/.vagrant/machines/ansible-test-worker-1/virtualbox/private_key
ansible-test-worker-2 ansible_ssh_user=vagrant ansible_ssh_host=127.0.0.1 ansible_ssh_port=2200 ansible_ssh_private_key_file=/home/user/vagrant/cluster/.vagrant/machines/ansible-test-worker-2/virtualbox/private_key
ansible-test-control ansible_ssh_user=vagrant ansible_ssh_host=127.0.0.1 ansible_ssh_port=2201 ansible_ssh_private_key_file=/home/user/vagrant/cluster/.vagrant/machines/ansible-test-control/virtualbox/private_key
[control]
ansible-test-control
[worker]
ansible-test-worker-1
ansible-test-worker-2
[cluster:children]
control
worker

You can print the inventory as compact JSON by providing the --json flag or as pretty JSON by providing the --pretty flag.

Provisioning

The ansible_auto provisioner is an augmented version of the ansible_local provisioner included with Vagrant. It accepts all options valid for that provisioner, and adds the following options:

Vagrant.configure(2) do |config|
  config.define 'ansible-control' do |machine|
    machine.provision :ansible_auto do |ansible|
      # Will show up in inventory as
      #   [control]
      #   ansible-control
      ansible.groups = {
        'control'   => %w(ansible-control)
      }

      # Will show up in inventory as
      #  [dev:children]
      #  control
      ansible.children = {
        'dev'   => %w(control)
      }

      # Will show up in inventory as
      #   [dev:vars]
      #   git_branch = devel
      ansible.vars = {
        'dev' => {
          'git_branch' => 'devel'
        }
      }

      # Enable or disable the `StrictHostKeyChecking` SSH option.
      # Disabled by default.
      ansible.strict_host_key_checking = false

      # The number of times to attempt to connect to a managed host from the
      # Ansible control machine.
      ansible.host_connect_tries = 10

      # The number of seconds to delay between connection attempts.
      ansible.host_connect_sleep = 5

      # When true, insert the public key of the SSH user for the control
      # machine (the machine that will run `ansible-playbook`) into the
      # authorized_keys files of the SSH users on managed machines.  Enabled by
      # default.
      ansible.insert_control_machine_public_key = false

      # When true, upload the private keys for the SSH users of managed
      # machines to a temporary location on the control machine, using these
      # keys as the values of `ansible_ssh_private_key_file` in the generated
      # inventory.  Disabled by default, unless
      # `insert_control_machine_public_key` is disabled.
      ansible.upload_inventory_host_private_keys = true
    end
  end
end

Public Key Authentication

Each guest provisioned with ansible_auto will be set up as an Ansible control machine with the ability to connect to other guests defined in the Vagrantfile. This is facilitated by either:

  • Inserting the public key of the control machine's SSH user into the authorized_keys file of the SSH user on each of the managed machines (other than the control machine itself). This is what happens when the option insert_control_machine_public_key is in effect.
  • Uploading the private keys of each guest to a temporary path on the control machine and assigning this path as the hostvar ansible_ssh_private_key_file to the relevant host in the generated inventory. This is what happens when the option upload_inventory_host_private_keys is in effect.

insert_control_machine_public_key takes precedence over upload_inventory_host_private_keys, so public key insertion will be used if both options are set to true.

Targeted Machines

By default, the ansible_auto provisioner targets all machines defined in the inventory by setting the provisioner option ansible.limit to "*". This is different than the core ansible_local provisioner, which by default targets only the guest for which the provisioner was defined.

Contributing

  1. Fork it ( https://github.com/tomeon/vagrant-ansible_auto/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

License

The MIT licence

About

Vagrant plugin for building ansible inventory files.

License:MIT License


Languages

Language:Ruby 98.9%Language:Nix 1.1%