maanantai 2. kesäkuuta 2014

Trying out Ansible with Vagrant


There's two virtual machines in this setup, called "ansible" and "development". The first one ("ansible") is the host that is running ansible, and the latter one is the target. Both are running Fedora 20 images created as explained in previous blog post.

There's three, somewhat advanced, configurations in the Vagrantfile. The Vagrantfile defines two different hosts. They have to be named, and they can have different configurations. The.

To make things easier, there's a private network between these two hosts. This way the hosts can have predefined IPs, which can then be used for making connections between them.

Third thing is provisioning setup. Provisioning simply means configuring the environment by installing packages and modifying configurations. Here, simple bash script is used. This script installs Ansible from source, sets up profile -file to source env-setup on login and does some other minor things.

So here's the Vagrantfile:

VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.define "development" do |development|
    development.vm.box = "basic-fedora-20-x86_64"
    development.vm.network :private_network, ip: "192.168.111.100"
  end
  config.vm.define "ansible" do |ansible|
    ansible.vm.box = "basic-fedora-20-x86_64"
    ansible.vm.network :private_network, ip: "192.168.111.101"
    ansible.vm.provision "shell", path: "install_ansible.sh"
  end
end 
When defining multiple machines, vagrant commands are applied to all by default. So you can start both of machines with "vagrant up" in the same directory where the Vagrantfile is. After a while, both machine have booted.

Then you can ssh into "ansible" with vagrant ssh ansible. On login, you should see something like

Setting up Ansible to run out of checkout...
 The directory, where the Vagrant file is, can be found from /vagrant. In that directory, you can fined a inventory file (development-hosts) and simple playbook (base.yml). To make sure that everything is working, go to /vagrant -directory and execute

ansible -i development-hosts -u vagrant -k -m ping all
which will ask for a password (which is "vagrant") and should then print

ansible | success >> {
    "changed": false,
    "ping": "pong"
}
deployment | success >> {
    "changed": false,
    "ping": "pong"
}
The command  "ansible -i development-hosts -k -m ping all" is different than the one in tutorial. "-i development-hosts" tells ansible to use given file as inventory, "-u vagrant" means that the user who makes connection is vagrant and "-k" makes ansible to ask a ssh password. The "-u vagrant" is not necessary here, because without it, ansible would use the username of currently logged in user.

When ping is working, you can run the "base.yml" playbook with

ansible-playbook -i development-hosts -k base.yml
This playbook will output information about default ipv4 interface using debug -module.

Now you have a pretty good playground for trying out Ansible. First thing to do would be setting up public key authentication so you do not need to write password all the time (hint: authorized_key module in ansible)


Ei kommentteja :

Lähetä kommentti