Ansible: Up and Running

Book description

Among the many configuration management tools available, Ansible has some distinct advantages—it’s minimal in nature, you don’t need to install anything on your nodes, and it has an easy learning curve. This practical guide shows you how to be productive with this tool quickly, whether you’re a developer deploying code to production or a system administrator looking for a better automation solution.

Author Lorin Hochstein shows you how to write playbooks (Ansible’s configuration management scripts), manage remote servers, and explore the tool’s real power: built-in declarative modules. You’ll discover that Ansible has the functionality you need and the simplicity you desire.

  • Understand how Ansible differs from other configuration management systems
  • Use the YAML file format to write your own playbooks
  • Learn Ansible’s support for variables and facts
  • Work with a complete example to deploy a non-trivial application
  • Use roles to simplify and reuse playbooks
  • Make playbooks run faster with ssh multiplexing, pipelining, and parallelism
  • Deploy applications to Amazon EC2 and other cloud platforms
  • Use Ansible to create Docker images and deploy Docker containers

Publisher resources

View/Submit Errata

Table of contents

  1. Foreword
  2. Preface
    1. Why I Wrote This Book
    2. Who Should Read This Book
    3. Navigating This Book
    4. Conventions Used in This Book
    5. Online Resources
    6. Safari® Books Online
    7. How to Contact Us
    8. Acknowledgments
  3. 1. Introduction
    1. A Note About Versions
    2. Ansible: What Is It Good For?
    3. How Ansible Works
    4. What’s So Great About Ansible?
      1. Easy-to-Read Syntax
      2. Nothing to Install on the Remote Hosts
      3. Push-Based
      4. Ansible Scales Down
      5. Built-in Modules
      6. Very Thin Layer of Abstraction
    5. Is Ansible Too Simple?
    6. What Do I Need to Know?
    7. What Isn’t Covered
    8. Installing Ansible
    9. Setting Up a Server for Testing
      1. Using Vagrant to Set Up a Test Server
      2. Telling Ansible About Your Test Server
      3. Simplifying with the ansible.cfg File
    10. Moving Forward
  4. 2. Playbooks: A Beginning
    1. Some Preliminaries
    2. A Very Simple Playbook
      1. Specifying an nginx Config File
      2. Creating a Custom Homepage
      3. Creating a Webservers Group
    3. Running the Playbook
    4. Playbooks Are YAML
      1. Start of File
      2. Comments
      3. Strings
      4. Booleans
      5. Lists
      6. Dictionaries
      7. Line Folding
    5. Anatomy of a Playbook
      1. Plays
      2. Tasks
      3. Modules
      4. Putting It All Together
    6. Did Anything Change? Tracking Host State
    7. Getting Fancier: TLS Support
      1. Generating TLS certificate
      2. Variables
      3. Generating the Nginx Configuration Template
      4. Handlers
      5. Running the Playbook
  5. 3. Inventory: Describing Your Servers
    1. The Inventory File
    2. Preliminaries: Multiple Vagrant Machines
    3. Behavioral Inventory Parameters
      1. ansible_connection
      2. ansible_shell_type
      3. ansible_python_interpreter
      4. ansible_*_interpreter
      5. Changing Behavioral Parameter Defaults
    4. Groups and Groups and Groups
      1. Example: Deploying a Django App
      2. Aliases and Ports
      3. Groups of Groups
      4. Numbered Hosts (Pets versus Cattle)
    5. Hosts and Group Variables: Inside the Inventory
    6. Host and Group Variables: In Their Own Files
    7. Dynamic Inventory
      1. The Interface for a Dynamic Inventory Script
      2. Writing a Dynamic Inventory Script
      3. Pre-Existing Inventory Scripts
    8. Breaking Out the Inventory into Multiple Files
    9. Adding Entries at Runtime with add_host and group_by
      1. add_host
      2. group_by
  6. 4. Variables and Facts
    1. Defining Variables in Playbooks
    2. Viewing the Values of Variables
    3. Registering Variables
    4. Facts
      1. Viewing All Facts Associated with a Server
      2. Viewing a Subset of Facts
      3. Any Module Can Return Facts
      4. Local Facts
    5. Using set_fact to Define a New Variable
    6. Built-in Variables
      1. hostvars
      2. inventory_hostname
      3. Groups
    7. Setting Variables on the Command Line
    8. Precedence
  7. 5. Introducing Mezzanine: Our Test Application
    1. Why Deploying to Production Is Complicated
      1. PostgreSQL: The Database
      2. Gunicorn: The Application Server
      3. Nginx: The Web Server
      4. Supervisor: The Process Manager
  8. 6. Deploying Mezzanine with Ansible
    1. Listing Tasks in a Playbook
    2. Organization of Deployed Files
    3. Variables and Secret Variables
    4. Using Iteration (with_items) to Install Multiple Packages
    5. Adding the Sudo Clause to a Task
    6. Updating the Apt Cache
    7. Checking Out the Project Using Git
    8. Installing Mezzanine and Other Packages into a virtualenv
    9. Complex Arguments in Tasks: A Brief Digression
    10. Creating the Database and Database User
    11. Generating the local_settings.py File from a Template
    12. Running django-manage Commands
    13. Running Custom Python Scripts in the Context of the Application
      1. Setting Service Configuration Files
    14. Enabling the Nginx Configuration
    15. Installing TLS Certificates
    16. Installing Twitter Cron Job
    17. The Full Playbook
    18. Running the Playbook Against a Vagrant Machine
    19. Deploying Mezzanine on Multiple Machines
  9. 7. Complex Playbooks
    1. Running a Task on the Control Machine
    2. Running a Task on a Machine Other Than the Host
    3. Manually Gathering Facts
    4. Running on One Host at a Time
    5. Running Only Once
    6. Dealing with Badly Behaved Commands: changed_when and failed_when
    7. Retrieving the IP Address from the Host
    8. Encrypting Sensitive Data with Vault
    9. Patterns for Specifying Hosts
    10. Limiting Which Hosts Run
    11. Filters
      1. The Default Filter
      2. Filters for Registered Variables
      3. Filters That Apply to File Paths
      4. Writing Your Own Filter
    12. Lookups
      1. file
      2. pipe
      3. env
      4. password
      5. template
      6. csvfile
      7. dnstxt
      8. redis_kv
      9. etcd
      10. Writing Your Own Lookup Plug-in
    13. More Complicated Loops
      1. with_lines
      2. with_fileglob
      3. with_dict
      4. Looping Constructs as Lookup Plug-ins
  10. 8. Roles: Scaling Up Your Playbooks
    1. Basic Structure of a Role
    2. Example: Database and Mezzanine Roles
    3. Using Roles in Your Playbooks
    4. Pre-Tasks and Post-Tasks
    5. A “Database” Role for Deploying the Database
    6. A “Mezzanine” Role for Deploying Mezzanine
    7. Creating Role Files and Directories with ansible-galaxy
    8. Dependent Roles
    9. Ansible Galaxy
      1. Web Interface
      2. Command-Line Interface
      3. Contributing Your Own Role
  11. 9. Making Ansible Go Even Faster
    1. SSH Multiplexing and ControlPersist
      1. Manually Enabling SSH Multiplexing
      2. SSH Multiplexing Options in Ansible
    2. Pipelining
      1. Enabling Pipelining
      2. Configuring Hosts for Pipelining
    3. Fact Caching
      1. JSON File Fact-Caching Backend
      2. Redis Fact Caching Backend
      3. Memcached Fact Caching Backend
    4. Parallelism
    5. Accelerated Mode
    6. Fireball Mode
  12. 10. Custom Modules
    1. Example: Checking That We Can Reach a Remote Server
    2. Using the Script Module Instead of Writing Your Own
    3. can_reach as a Module
    4. Where to Put Custom Modules
    5. How Ansible Invokes Modules
      1. Generate a Standalone Python Script with the Arguments (Python Only)
      2. Copy the Module to the Host
      3. Create an Arguments File on the Host (Non-Python Only)
      4. Invoke the Module
    6. Expected Outputs
      1. Output Variables Ansible Expects
    7. Implementing Modules in Python
      1. Parsing Arguments
      2. Accessing Parameters
      3. Importing the AnsibleModule Helper Class
      4. Argument Options
      5. AnsibleModule Initializer Parameters
      6. Returning Success or Failure
      7. Invoking External Commands
      8. Check Mode (Dry Run)
    8. Documenting Your Module
    9. Debugging Your Module
    10. Implementing the Module in Bash
    11. Specifying an Alternaive Location for Bash
    12. Example Modules
  13. 11. Vagrant
    1. Convenient Vagrant Configuration Options
      1. Port Forwarding and Private IP Addresses
      2. Enabling Agent Forwarding
    2. The Ansible Provisioner
    3. When the Provisioner Runs
    4. Inventory Generated by Vagrant
    5. Provisioning in Parallel
    6. Specifying Groups
  14. 12. Amazon EC2
    1. Terminology
      1. Instance
      2. Amazon Machine Image
      3. Tags
    2. Specifying Credentials
      1. Environment Variables
      2. Configuration Files
    3. Prerequisite: Boto Python Library
    4. Dynamic Inventory
      1. Inventory Caching
      2. Other Configuration Options
      3. Auto-Generated Groups
    5. Defining Dynamic Groups with Tags
      1. Applying Tags to Existing Resources
      2. Nicer Group Names
    6. EC2 Virtual Private Cloud (VPC) and EC2 Classic
    7. Configuring ansible.cfg for Use with ec2
    8. Launching New Instances
    9. EC2 Key Pairs
      1. Creating a New Key
      2. Upload an Existing Key
    10. Security Groups
      1. Permitted IP Addresses
      2. Security Group Ports
    11. Getting the Latest AMI
    12. Adding a New Instance to a Group
    13. Waiting for the Server to Come Up
    14. Creating Instances the Idempotent Way
    15. Putting It All Together
    16. Specifying a Virtual Private Cloud
      1. Dynamic Inventory and VPC
    17. Building AMIs
      1. With the ec2_ami Module
      2. With Packer
    18. Other Modules
  15. 13. Docker
    1. The Case for Pairing Docker with Ansible
    2. Docker Application Life Cycle
    3. Dockerizing Our Mezzanine Application
    4. Creating Docker Images with Ansible
      1. Mezzanine
    5. The Other Container Images
      1. Postgres
      2. Memcached
      3. Nginx
      4. Certs
      5. Building the Images
    6. Deploying the Dockerized Application
    7. Starting the Database Container
    8. Retrieving the Database Container IP Address and Mapped Port
    9. Waiting for the Database to Start Up
      1. Initializing the Database
      2. Starting the Memcached Container
      3. Starting the Mezzanine Container
      4. Starting the Certificate Container
      5. Starting the Nginx Container
    10. The Entire Playbook
  16. 14. Debugging Ansible Playbooks
    1. Debugging SSH Issues
    2. The Debug Module
    3. The Assert Module
    4. Checking Your Playbook Before Execution
      1. Syntax Check
      2. List Hosts
      3. List Tasks
      4. Check Mode
      5. Diff (Show File Changes)
    5. Limiting Which Tasks Run
      1. Step
      2. Start-at-Task
      3. Tags
    6. Onward
  17. A. SSH
    1. Native SSH
    2. SSH Agent
    3. Starting Up ssh-agent
      1. Mac OS X
      2. Linux
    4. Agent Forwarding
      1. Sudo and Agent Forwarding
    5. Host Keys
  18. B. Default Settings
  19. C. Using IAM Roles for EC2 Credentials
    1. AWS Management Console
    2. Command-Line
  20. Glossary
  21. Bibliography
  22. Index

Product information

  • Title: Ansible: Up and Running
  • Author(s):
  • Release date: May 2015
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781491915325