Ansible Tutorial
Ansible Tutorial
Documentation
Release 0.0.1
Glen Jarvis
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
i
ii
CHAPTER 1
A tutorial with video and code taking the user from AWS machine creation to their first deployment using Ansible
1.2 Welcome
A tutorial with video and code taking the user from AWS machine creation to their first deployment using Ansible
Videos of Talk
Prerequisite Talks
Slow Walkthrough
1
Ansible Hands-On Training Documentation, Release 0.0.1
Fast Overview
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
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.
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
• 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
1.2. Welcome 3
Ansible Hands-On Training Documentation, Release 0.0.1
What is the path to your .pem key file for the virtual machine?
--> ~/example_key.pem
Configuration is complete.
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 )
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
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
Installation
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.
The sources for ansible_tutorial can be downloaded from the Github repo.
You can either clone the public repository:
Once you have a copy of the source, you can install it with:
7
Ansible Hands-On Training Documentation, Release 0.0.1
8 Chapter 2. Installation
CHAPTER 3
Usage
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.
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
12 Chapter 4. Contributing
Ansible Hands-On Training Documentation, Release 0.0.1
$ virtualenv venv
• For Python3:
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:
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:
8. Install the Git Hooks. Copy the contents of githooks into your checked out project’s .git/hooks folder:
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.
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.
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.
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 [email protected] with as much relevant informaton that you can. I will
want to get that fixed as quickly as I can.
• 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:
14 Chapter 4. Contributing
Ansible Hands-On Training Documentation, Release 0.0.1
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.
• 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
All commits should follow The seven rules of a great Git commit
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.
I teach classes in this subject and I want to help you. I am currently making two courses:
• 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 [email protected] 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 [email protected] 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
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
16 Chapter 4. Contributing
CHAPTER 5
Authors
5.1 Maintainer
5.2 Contributors
17
Ansible Hands-On Training Documentation, Release 0.0.1
18 Chapter 5. Authors
CHAPTER 6
History
6.1 release-1.3.0
6.2 release-1.2.0
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
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
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
20 Chapter 6. History
Ansible Hands-On Training Documentation, Release 0.0.1
6.4. release-1.0.0 21
Ansible Hands-On Training Documentation, Release 0.0.1
22 Chapter 6. History
CHAPTER 7
• genindex
• modindex
• search
23