From charlesreid1

Revision as of 23:37, 13 December 2018 by Admin (talk | contribs) (→‎Regular Playbook)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Walkthrough of creating an Ansible playbook for yeti.

a) found pyenv playbook online. use this.

b) flailing with vagrant.


Setup

Before we start, we want to have the following:

Vagrantfile

Here we give a Vagrantfile that will start three separate nodes. This is important to test yeti's ability to handle multiple machines.

Vagrantfile:

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  # Use the same key for each machine
  config.ssh.insert_key = false

  config.vm.define "vagrant1" do |vagrant1|
    vagrant1.vm.box = "ubuntu/xenial64"
    vagrant1.vm.network "forwarded_port", guest: 80, host: 8877
    vagrant1.vm.network "forwarded_port", guest: 443, host: 7443
  end
  config.vm.define "vagrant2" do |vagrant2|
    vagrant2.vm.box = "ubuntu/xenial64"
    vagrant2.vm.network "forwarded_port", guest: 80, host: 8878
    vagrant2.vm.network "forwarded_port", guest: 443, host: 7444
  end
  config.vm.define "vagrant3" do |vagrant3|
    vagrant3.vm.box = "ubuntu/xenial64"
    vagrant3.vm.network "forwarded_port", guest: 80, host: 8879
    vagrant3.vm.network "forwarded_port", guest: 443, host: 7445
  end
end

Also see Ansible/Vagrant.

Inventory file: hosts

Info for the inventory file (port numbers) can be obtained with:

vagrant ssh-config

The corresponding inventory file looks like this:

$ cat hosts
[servers]
v1 ansible_host=127.0.0.1 ansible_port=2222
v2 ansible_host=127.0.0.1 ansible_port=2201
v3 ansible_host=127.0.0.1 ansible_port=2202

Roles

Galaxy playbook: Python and pyenv install

The Linux Foundation IT org (lfit) on Github maintains an Ansible script for installing different versions of Python using pyenv or using a system python. We use this role to prepare yeti worker nodes.

Link to the Ansible playbook: https://github.com/lfit/ansible-roles-python-install.git

Tasks

The tasks defined by the lfit org in their ansible playbook can be shown by running the ansible-playbook command with the --list-tasks flag:

$ ansible-playbook --list-tasks playbook.yml
 [WARNING]: Unable to parse /etc/ansible/hosts as an inventory source

 [WARNING]: No inventory was parsed, only implicit localhost is available

 [WARNING]: provided hosts list is empty, only localhost is available. Note that the
implicit localhost does not match 'all'

 [WARNING]: Could not match supplied host pattern, ignoring: servers

playbook: playbook.yml

  play #1 (servers): prepare yeti worker nodes to run tasks	TAGS: []
    tasks:
      lfit.python-install : Include distro specific variables	TAGS: []
      lfit.python-install : Install Python	TAGS: []
      lfit.python-install : Install pyenv {{pyenv_version}}	TAGS: []
      lfit.python-install : Install Python {{python34_version}}	TAGS: []
      lfit.python-install : Install Python {{python35_version}}	TAGS: []
      lfit.python-install : Install Python {{python36_version}}	TAGS: []
      lfit.python-install : Set pyenv global	TAGS: []

We can see the two major choices are, install pyenv, or install python.

Playbook

Provisioning Playbook: Pre-Install Python 2

We need to make sure that Python 2 is installed so that Ansible can do its thing and use the various modules.

Unfortunately, on Ubuntu Xenial, Python 2 is not available by default, so we need to shim it in before we do anything else.

We use two playbooks, a provision playbook provision.yml (to be run once on each machine before running the main playbook) and a main playbook main.yml.

The provision playbook needs to have a pre task, which we set up as follows:

$ cat provision.yml
---
# 
- name: provision the yeti worker nodes and prepare them for Ansible
  hosts: servers
  gather_facts: no
  pre_tasks:
    - name: install python2
      raw: sudo apt-get -y install python

Now we can apply this playbook to each of the nodes. This example shows applying it to three virtual boxes:

$ ansible-playbook -i hosts provision.yml
 _______________________________________________
< PLAY [provision the yeti worker nodes and prepare them for Ansible] >
 -----------------------------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

 ________________________
< TASK [install python2] >
 ------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

changed: [v2]
changed: [v3]
changed: [v1]
 ____________
< PLAY RECAP >
 ------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

v1                         : ok=1    changed=1    unreachable=0    failed=0
v2                         : ok=1    changed=1    unreachable=0    failed=0
v3                         : ok=1    changed=1    unreachable=0    failed=0

Regular Playbook

The playbook

$ cat main.yml
---
#
- name: prepare yeti worker nodes to run tasks
  hosts: servers
  roles:
     - { role: lfit.python-install }

We can list each of the tasks available (as shown above):

$ ansible-playbook --list-tasks main.yml

Now we can run this playbook:

$ ansible-playbook -i hosts main.yml

Flags