You are on page 1of 27

Ansible Hands-On Training

Documentation
Release 0.0.1

Glen Jarvis

May 20, 2018


Contents:

1 Ansible Hands-On Training 1


1.1 Project and Build Status . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Welcome . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.3 Make this better by Contributing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

2 Installation 7
2.1 Stable release . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.2 From sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

3 Usage 9

4 Contributing 11
4.1 Getting Started! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
4.2 Linting and Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
4.3 Testing Guidelines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.4 Commit Guidelines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.5 Pull Request Guidelines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.6 Code of Conduct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
4.7 Credits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

5 Authors 17
5.1 Maintainer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
5.2 Contributors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

6 History 19
6.1 release-1.3.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
6.2 release-1.2.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
6.3 release-1.1.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
6.4 release-1.0.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

7 Indices and tables 23

i
ii
CHAPTER 1

Ansible Hands-On Training

A tutorial with video and code taking the user from AWS machine creation to their first deployment using Ansible

1.1 Project and Build Status

• GitHub repo: https://github.com/glenjarvis/ansible_tutorial/


• Documentation: Read The Docs
• Free software: LICENSE

1.2 Welcome

A tutorial with video and code taking the user from AWS machine creation to their first deployment using Ansible

1.2.1 Successfully automating your machines in the cloud using Ansible

Videos of Talk

Prerequisite Talks

• Building an AWS Instance

Slow Walkthrough

Same material / Digestible pace / Longer Video:


• 9-Aug, 2014 Hands-On Training

1
Ansible Hands-On Training Documentation, Release 0.0.1

Fast Overview

Same material / Quickly covered / Shorter Video:


• 26-June, 2014 Bay Area Python Interest Group (BayPIGgies)/Silicon Valley Python MeetUp Video

Practice Sessions

This Repository is now out of date. This was written against an older version of Ansible and hasn’t yet been updated.
I am writing this note on 18-May, 2018 and hope to get to upgrading this repository to work with current versions of
Ansible. This issue tracks:
https://github.com/glenjarvis/ansible_tutorial/issues/78

Hands-On Training

• No sessions currently scheduled

Talk description

As we have seen with the previous talk, “Red Pill, Blue Pill Virtual Machines and Virtual Environments” (GitHub /
YouTube <https://www.youtube.com/watch?v=xZb3cr1JrMg>), we can create virtual machines in the cloud.
But, how do you “stamp” those machines differently? If you need to build a web server, mail server, DNS server, and
load balancer, each machine may have the same base image but needs to be configured differently.
If you manually configure those machines, what happens when you suddenly have a surge in traffic and need four more
web servers? Or, what if one finds a vulnerability in a library like Heartbleed in OpenSSL? A very safe option would
be to rebuild these machines from scratch. If they were built manually, rebuilding these machines within minutes from
scratch would be daunting, tedious and error prone.
There are several tools that have been built to fix this problem. Two of the most popular tools, Chef and Puppet, are
written in the Ruby programming language. And, especially for the most popular, Chef, one needs somewhat of a
familiarity with that language to use the tool.
There are two more tools that are written in Python and are growing in popularity: Salt and Ansible. Ansible requires
the least amount of set-up (if any) and has the simplest infrastructure (it simply uses commands over ssh like Fabric
does). Ansible is the easiest tool to get started with if you are new in the machine build automation frameworks.
We will start with a newly built machine and obtain it’s public IP address. We will configure the ansible_hosts file
with the IP address, and add/build plays (like recipes) to gradually configure that machine so that it is a Django web
server running in the cloud. When we are finished, we should have a running machine and a recipe to easily build a
seconded machine with a few keystrokes.
P.S. If you haven’t previously built an Amazon Web Instance, I highly recommend watching this video in advance of
the talk.

Getting Started (Installing and following-along)

This Repository is now out of date. This was written against an older version of Ansible and hasn’t yet been updated.
I am writing this note on 18-May, 2018 and hope to get to upgrading this repository to work with current versions of
Ansible. This issue tracks:
https://github.com/glenjarvis/ansible_tutorial/issues/78

2 Chapter 1. Ansible Hands-On Training


Ansible Hands-On Training Documentation, Release 0.0.1

1. Clone this repo.


::code: bash
$ git clone https://github.com/glenjarvis/ansible_tutorial.git Cloning into ‘ansible_tutorial’. . . re-
mote: Reusing existing pack: 112, done. remote: Total 112 (delta 0), reused 0 (delta 0) Receiving
objects: 100% (112/112), 37.58 KiB | 0 bytes/s, done. Resolving deltas: 100% (48/48), done. Check-
ing connectivity. . . done.
2. Make a virtualenv named venv for your Python environment of choice.
• For Python2:
$ virtualenv venv

• For Python3:
$ python3 -m venv venv

3. Activate the Virtual Enviroment. Every time you come back to work on this project, you will need to activate
your virtual environment:
$ cd full_path_of_this_repo
$ source venv/bin/activate

When the Virtual Environment is activated, you should see venv in the prompt. It may look something to this:
(venv) $

I often like to be able to jump to this folder quickly from anywhere and have it automatically setup my virtual
environment. So, I put something like this in my $HOME/.bashrc (or equivalent) file:
function cd-ansible_tutorial {
deactivate 2> /dev/null
cd /FULL_PATH_TO_THIS_DIRECTORY
source venv/bin/activate
}

4. Upgrade Pip. The Pip that comes with a new Virtual Environment is often too old. Upgrade it to be sure it is
current:
(venv)$ pip install --upgrade pip
Collecting pip
Using cached https://files.pythonhosted.org/packages/0f/74/
˓→ecd13431bcc456ed390b44c8a6e917c1820365cbebcb6a8974d1cd045ab4/pip-10.0.1-py2.py3-

˓→none-any.whl

Installing collected packages: pip


Found existing installation: pip 9.0.3
Uninstalling pip-9.0.3:
Successfully uninstalled pip-9.0.3
Successfully installed pip-10.0.1

5. Configure the repo for your account and settings


(venv)$ ( cd src; python configure.py )

This script creates configuration files for using Ansible to


configure a newly-created virtual machine.
It has been tested by the author on an AWS free tier VM.
(continues on next page)

1.2. Welcome 3
Ansible Hands-On Training Documentation, Release 0.0.1

(continued from previous page)


This has the best chance of working on an AWS free tier VM, or
failing that, on a VM with a recent version of CentOS.

No configuration file found. Let me ask questions so that we can configure.

What is the path to your .pem key file for the virtual machine?
--> ~/example_key.pem

What user to use to ssh to the remote system [ec2-user]?


-->
Configuring `ansible_hosts` file ./ansible_hosts...

What is the IP address of the virtual machine?


--> demos.glenjarvis.com

Configuration is complete.

6. Follow the examples (starting with the example_01 subdirectory).

Examples

Here is a list of the examples just in case there’s any confusion in which order the examples should be executed:
1. ( cd src; python configure.py ) (see instructions above)
2. ( cd src/example_01; python access_machine.py )
3. ( cd src/example_02; python auto_update_machine.py )
4. ( cd src/example_03; less README.txt ) (Read the README.txt file; it’s not really meant to
be executed)
5. ( cd src/example_04; ansible webservers -m ping; ansible webservers -vvv
-m ping )
6. ( cd src/example_05; ansible webservers -vvv -a 'sudo yum update -y' )
7. ( cd src/playbook_examples; ansible-playbook demo_playbook_iter_01.yml )
8. ( cd src/playbook_examples; ansible-playbook demo_playbook_iter_02.yml )
9. ( cd src/playbook_examples; ansible-playbook demo_playbook_iter_03.yml )
10. ( cd src/playbook_examples; ansible-playbook demo_playbook_iter_04.yml )
11. ( cd src/playbook_examples; ansible-playbook demo_playbook_iter_05.yml )
12. ( cd src/playbook_examples; ansible-playbook demo_playbook_iter_06.yml )
13. ( cd src/playbook_examples; ansible-playbook demo_playbook_iter_07.yml )
14. ( cd src/playbook_examples; ansible-playbook demo_playbook_iter_08.yml )
15. ( cd src/playbook_examples; ansible-playbook demo_playbook_iter_09.yml )
16. ( cd src/playbook_examples; less pedantically_commented_playbook ) (Read but
don’t execute: pedantically_commented_playbook.yml)
17. ( cd src/role_examples; ansible-playbook demo_play_role_01.yml )
18. ( cd src/role_examples; ansible-playbook demo_play_role_02.yml )
19. ( cd src/role_examples; ansible-playbook demo_play_role_03.yml )

4 Chapter 1. Ansible Hands-On Training


Ansible Hands-On Training Documentation, Release 0.0.1

20. Exploration: Log into machine; sudo su - webuser; cd /home/webuser/sample_project;


python manage.py runserver

Bio

Glen has been a full-time Python programmer since 2007 and has worked for companies such as IBM, UC Berkeley,
Sprint, Informix, and many small start-ups. He has also worked both in the US and in the UK and has had Bioin-
formatics research published in Nucleic Acids Research (Oxford Journals) He is a certified DBA and has also been
certified in Linux/Unix Administration.
He is a DevOps engineer and uses Ansible heavily. He just finished working for a startup after 5 years. He is creating
a Coursera course in collaboration with a UC campus on Source Code Mangement Systems for the DevOps developer.
Additionally, he runs a small start up, Glen Jarvis Training & Consulting, LLC, that does online technical training and
assists employees obtaining telepresence in their current work place.
Glen is the organizer for the Silicon Valley Python MeetUp Group and a co-organizer of the Bay Area Python Interest
Group.
GitHub
Google+
LinkedIn

1.3 Make this better by Contributing

This is an Open Source project and contributions are always welcome, and they are greatly appreciated! Every little
bit helps, and credit will always be given.
You can contribute in many ways:
• Report bugs
• Write Documentation
• Fix bugs
To maximize the chance that your hard work gets merged, we have these guidelines to guide you along the way to a
successfully merged Pull Request:
• Contributing
• https://github.com/glenjarvis/ansible_tutorial/blob/master/CONTRIBUTING.rst

1.3. Make this better by Contributing 5


Ansible Hands-On Training Documentation, Release 0.0.1

6 Chapter 1. Ansible Hands-On Training


CHAPTER 2

Installation

2.1 Stable release

To install ansible_tutorial, run this command in your terminal:

$ pip install ansible_tutorial

This is the preferred method to install ansible_tutorial, as it will always install the most recent stable release.
If you don’t have pip installed, this Python installation guide can guide you through the process.

2.2 From sources

The sources for ansible_tutorial can be downloaded from the Github repo.
You can either clone the public repository:

$ git clone git://github.com/glenjarvis/ansible_tutorial

Or download the tarball:

$ curl -OL https://github.com/glenjarvis/ansible_tutorial/tarball/master

Once you have a copy of the source, you can install it with:

$ python setup.py install

7
Ansible Hands-On Training Documentation, Release 0.0.1

8 Chapter 2. Installation
CHAPTER 3

Usage

To use ansible_tutorial in a project:

import ansible_tutorial

9
Ansible Hands-On Training Documentation, Release 0.0.1

10 Chapter 3. Usage
CHAPTER 4

Contributing

Ready to contribute? Your contributions are very much welcomed and credit will be given. These Guidelines will help
you effectively contribute to this project and guide you to successfully merged Pull Requests.
Here’s how to set up the ansible_tutorial for local development.

4.1 Getting Started!

This repository is in standard wheel format so it can be used like a normal Python Package. However, if you follow
these instructions, you can avoid having your python environment look like this:

11
Ansible Hands-On Training Documentation, Release 0.0.1

If you need help with background knowledge, see online training video: https://GlenJarvis.com/v/
virtual-environments.
1. Find a place to work:

$ cd path_where_you_want_this_repo

2. Clone the project:

$ git clone https://github.com/glenjarvis/ansible_tutorial.git


$ cd ansible_tutorial

3. Make a virtualenv named venv for your Python environment of choice.


• For Python2:

12 Chapter 4. Contributing
Ansible Hands-On Training Documentation, Release 0.0.1

$ virtualenv venv

• For Python3:

$ python3 -m venv venv

4. Activate the Virtual Enviroment. Every time you come back to work on this project, you will need to activate
your virtual environment:

$ cd path_of_this_repo
$ source venv/bin/activate

When the Virtual Environment is activated, you should see venv in the prompt. It may look something to this:

(venv) $

I often like to be able to jump to this folder quickly from anywhere and have it automatically setup my virtual
environment. So, I put something like this in my $HOME/.bashrc (or equivalent) file:

function cd_ansible_tutorial {
deactivate 2> /dev/null
cd /FULL_PATH_TO_THIS_DIRECTORY
source venv/bin/activate
}

5. Upgrade Pip. The Pip that comes with a new Virtual Environment is often too old. Upgrade it to be sure it is
current:

(venv)$ pip install --upgrade pip

6. Pip install as editable. This project is in wheel format. So, simply install a reference in your virtual environment
so that you can edit files in this folder and see an immediate affect in the virtual environment:

(venv)$ pip install --editable .

7. Install extra packages for development:

(venv)$ pip install -r requirements_dev.txt

8. Install the Git Hooks. Copy the contents of githooks into your checked out project’s .git/hooks folder:

(venv)$ cp githooks/* .git/hooks

Note: git commit --no-verify bypasses Git Hooks. Please don’t do that unless the Git Hooks are
actually broken.
9. Check out a topic branch and begin working.

4.2 Linting and Style

Before any pull request is submitted, please ensure that you follow this project’s style and linting guidelines. We
supply some Git Hooks to help you with this.
Every commit’s applicable files should:
• Pass pycodestyle and be PEP8 compliant.

4.2. Linting and Style 13


Ansible Hands-On Training Documentation, Release 0.0.1

• Pass default PyLint with score of 9.0 or above (preferrably 10.0).


If there is something that PyLint has gotten wrong (not that uncommon), make it clear to both the readers of
your your code and to PyLint that your choice was intentional.
For example, in a Python based Git Hook referenced below, PyLint gave a warning that the pre-commit filename
is not a valid Python module name.
PyLint is correct - we wouldn’t normally want to name our Python program files pre-commit. In a normal
circumstance, we would want to name the program file precommit.py or something even more intuitive.
However, having an executable script with the name of pre-commit is a dependency of the Git Hook and
can’t be avoided. Thus, to communicate this to both the reader of the program and PyLint, I made these two
comments toward the top where it was signficant:

# "pre-commit" filename name is a hard Git dependency


# pylint: disable=invalid-name

The top line explains to a human reader why we are placing this comment. And, the second line explains to
PyLint that this warning can be surpressed (and not lower your score).
Thus, a PyLint score of 10.0 (the highest) should be possible. Both lines should be as clear and readable to a
human as possible.

4.2.1 Git Hooks

To enforce style and linting consistency in the project, a Git Hook has been provided to catch style and lint issues at
each commit. Installation is described above.
The pre-commit hook gives errors and stops the commit if:
• There are any pycodestyle violations.
• PyLint score drops below 9.0.
As with any automation, we should have a choice. The automation should help us enforce a good coding style and
not get in our way. If this Git Hook ever get in your way, you can bypass it by using the --no-verify option (e.g.,
git commit --no-verify).
If this does happen, please email me at glen@glenjarvis.com with as much relevant informaton that you can. I will
want to get that fixed as quickly as I can.

4.2.2 Extra Code Style

• Functions and methods should be as short as possible, breaking concepts into smaller functions/methods when-
ever possible.
• The pull request should work for Python 2.7, 3.4, 3.5 and 3.6, and for PyPy. Check https://travis-ci.org/
glenjarvis/ansible_tutorial/pull_requests and make sure that the tests pass for all supported Python versions.
• Follow the Zen:

The Zen of Python, by Tim Peters

Beautiful is better than ugly.


Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
(continues on next page)

14 Chapter 4. Contributing
Ansible Hands-On Training Documentation, Release 0.0.1

(continued from previous page)


Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

4.3 Testing Guidelines

Whenever possible, you should use Test Drive Development (TDD). If you are unfamiliar with this code design and
testing concept, here is an introductory video.
At the very least, all code submitted should have test coverage.

4.3.1 Tips and Tricks

• TravisCI will run tests against your pull requests and catch test errors: https://travis-ci.org/glenjarvis/ansible_
tutorial/pull_requests
• The pull request should work for Python 2.7, 3.4, 3.5 and 3.6, and for PyPy. Running tox locally will help
catch errors across versions of Python and make sure that the tests pass for all supported Python versions:

$ tox

4.4 Commit Guidelines

All commits should follow The seven rules of a great Git commit

4.5 Pull Request Guidelines

Please keep a good Git hygiene in your contribution. Not everyone knows how to use a Source Control Management
system like Git properly. We’re here to help.

4.5.1 Git Training

I teach classes in this subject and I want to help you. I am currently making two courses:

4.3. Testing Guidelines 15


Ansible Hands-On Training Documentation, Release 0.0.1

• Coursera course in collaboration with a UC College campus. If the current date is after 31-Aug, 2018 and you
still see this sentence, would you please send me an email at glen@glenjarvis.com to remind me to place the
Coursera link here in these Guidelines.
• An OnLine course “How to Contribute to Open Source Projects” at https://GlenJarvis.com/v/
contribute-open-source. This course isn’t yet finished. Email glen@glenjarvis.com for an early adoptor
invitation.
If you don’t understand all of the following, you should take one of these courses:
• The HEAD pointer
• The refs branch pointers
• The objects database (where everything is stored)
• How to fork
• How to push
• How to commit
• How to rebase

4.5.2 Guidelines

• Use a different topic branch for each topic


• Keep commits small
• Rebase topic branches (i.e., Don’t merge master back into topic)
• Use proper commit message

4.6 Code of Conduct

We value the participation of each member of the Open Source community and want all contributors and consumers of
this project to have an enjoyable and fulfilling experience. Accordingly, all contributors are expected to show respect
and courtesy to other contributors and community members working within this project.
To make clear what is expected, all communication around this project by all contributing members (including Glen
Jarvis) are required to conform to the Python Packaging Authority Code of Conduct.

4.7 Credits

This package was created with Cookiecutter

16 Chapter 4. Contributing
CHAPTER 5

Authors

5.1 Maintainer

• Glen Jarvis <glen@glenjarvis.com>

5.2 Contributors

• Marc Abramowitz (@msabramo)


• Abe Young (@plazith)
• Alex Kleider (@alexKleider)

17
Ansible Hands-On Training Documentation, Release 0.0.1

18 Chapter 5. Authors
CHAPTER 6

History

6.1 release-1.3.0

6.1.1 New Features

• Apply Cookiecutter template

6.2 release-1.2.0

6.2.1 New Features

Abe Young
• Update Hands on Training Schedule Thanks to @alexKleider for the merge
Alex Kleider
• Spol suggestions
• Improve pem file error checking
• Material changes in examples

6.3 release-1.1.0

6.3.1 New Features

Marc Abramowitz
• configure.py: Use os.path.expanduser
• example_01/access_machine.py: Improve messaging a bit.

19
Ansible Hands-On Training Documentation, Release 0.0.1

• configure.py: Remove extra blank lines in output


• README.md tweaks
• src/example_03/README.txt: Minor tweaks
• src/example_04/README.txt: Mention -v for verbose
• Remove ec2-user from playbooks
• Refactor/generalize so not quite so specific to ec2
Glen Jarvis
• Add JSON and YAML data type examples
• Add Slides from BayPIGgies Presentation
• Add Video from BayPIGgies/SV Meeetup 27-June 2014
• Add Practice Session section
• Add more Level 0 and Level 1 practice sessions
• Improve messaging
• Add -vvv explanation to README.txt

6.3.2 Bugs Fixed

Abe Young
• Format won’t work in all versions
Glen Jarvis
• Fix playbook action (nginx action didn’t match)
• Fix the error ‘the this’ in README

6.4 release-1.0.0

6.4.1 New Features

• Add README talk description


• Update BSD license
• Add Example_01 and Output
• Update Example 01
• Add Example 02 script with output
• Add Example 03
• Add Example 04 and its Output
• Move Ansible cofiguration group to webservers
• Add Example 05 and Output
• Move ansible_hosts to example_06
• Add example 06 in iterative_demo directory

20 Chapter 6. History
Ansible Hands-On Training Documentation, Release 0.0.1

• Update configuration for entire repo


• Refactor and Update example 04
• Add requirements.txt
• Update Example 5 instructions and output
• Refactor example_01 to dynamically read from ansible.cfg
• Strip carriage return from hostname during parsing
• Refactor Example_02 to dynamically read from ansible.cfg
• Fix pem_path_name for output
• Change numbering to be from 1 (not 0)
• Add second iteration of playbook
• Fix Erroneous Output
• Add a demo playbook 03
• Add demo playbook 04
• Add example 05
• Add playbook 07 demonstrating services
• Add playbook 08
• Add playbook 09
• Add example of “insanely complete” playbook
• Rename pedantically_commented_playbook.yml to it’s original name
• Rename iterative_examples directory to playbook_examples
• Add first role_example
• Add second role_example
• Add third role_example
• Add more README documentation
• Fix gunicorn dependency
• Fix spelling mistakes in README.md
• Fix symlinks for playbook_examples

6.4.2 Bugs Fixed

• Library code not checked in

6.4. release-1.0.0 21
Ansible Hands-On Training Documentation, Release 0.0.1

22 Chapter 6. History
CHAPTER 7

Indices and tables

• genindex
• modindex
• search

23

You might also like