Ansible: Up and Running, 2nd Edition

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. With this updated second edition, you’ll learn 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.

Publisher resources

View/Submit Errata

Table of contents

  1. Foreword
  2. Preface to the Second Edition
    1. A Note About Language
    2. Acknowledgments
      1. From Lorin
      2. From René
  3. Preface to the First Edition
    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
  4. 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
  5. 2. Playbooks: A Beginning
    1. Some Preliminaries
    2. A Very Simple Playbook
      1. Specifying an Nginx Config File
      2. Creating a Custom Home Page
      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 a TLS Certificate
      2. Variables
      3. Generating the Nginx Configuration Template
      4. Handlers
      5. Running the Playbook
  6. 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. Preexisting Inventory Scripts
    8. Breaking the Inventory into Multiple Files
    9. Adding Entries at Runtime with add_host and group_by
      1. add_host
      2. group_by
  7. 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
  8. 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
  9. 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 Become Clause to a Task
    6. Updating the Apt Cache
    7. Checking Out the Project by Using Git
    8. Installing Mezzanine and Other Packages into a virtualenv
    9. Complex Arguments in Tasks: A Brief Digression
    10. Configuring the Database
    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. Troubleshooting
      1. Cannot Check Out Git Repository
      2. Cannot Reach 192.168.33.10.xip.io
      3. Bad Request (400)
    20. Deploying Mezzanine on Multiple Machines
  10. 7. 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. 8. Complex Playbooks
    1. Dealing with Badly Behaved Commands: changed_when and failed_when
    2. Filters
      1. The Default Filter
      2. Filters for Registered Variables
      3. Filters That Apply to File Paths
      4. Writing Your Own Filter
    3. 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 Plugin
    4. More Complicated Loops
      1. with_lines
      2. with_fileglob
      3. with_dict
      4. Looping Constructs as Lookup Plugins
    5. Loop Controls
      1. Setting the Variable Name
      2. Labeling the Output
    6. Includes
      1. Dynamic Includes
      2. Role Includes
    7. Blocks
    8. Error Handling with Blocks
    9. Encrypting Sensitive Data with Vault
  12. 9. Customizing Hosts, Runs, and Handlers
    1. Patterns for Specifying Hosts
    2. Limiting Which Hosts Run
    3. Running a Task on the Control Machine
    4. Running a Task on a Machine Other Than the Host
    5. Running on One Host at a Time
    6. Running on a Batch of Hosts at a Time
    7. Running Only Once
    8. Running Strategies
      1. Linear
      2. Free
    9. Advanced Handlers
      1. Handlers in Pre and Post Tasks
      2. Flush Handlers
      3. Handlers Listen
    10. Manually Gathering Facts
    11. Retrieving the IP Address from the Host
  13. 10. Callback Plugins
    1. Stdout Plugins
      1. actionable
      2. debug
      3. dense
      4. json
      5. minimal
      6. oneline
      7. selective
      8. skippy
    2. Other Plugins
      1. foreman
      2. hipchat
      3. jabber
      4. junit
      5. log_plays
      6. logentries
      7. logstash
      8. mail
      9. osx_say
      10. profile_tasks
      11. slack
      12. timer
  14. 11. 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. Concurrent Tasks with Async
  15. 12. 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 that 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 Alternative Location for Bash
    12. Example Modules
  16. 13. 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
    7. Ansible Local Provisioner
  17. 14. 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. Autogenerated Groups
    5. Defining Dynamic Groups with Tags
      1. Applying Tags to Existing Resources
      2. Nicer Group Names
    6. EC2 Virtual Private Cloud 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. Uploading 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
  18. 15. Docker
    1. The Case for Pairing Docker with Ansible
    2. Docker Application Life Cycle
    3. Example Application: Ghost
    4. Connecting to the Docker Daemon
    5. Running a Container on Our Local Machine
    6. Building an Image from a Dockerfile
    7. Orchestrating Multiple Containers on Our Local Machine
    8. Pushing Our Image to the Docker Registry
    9. Querying Local Images
    10. Deploying the Dockerized Application
      1. Backend: Postgres
      2. Frontend
      3. Frontend: Ghost
      4. Frontend: Nginx
      5. Cleaning Out Containers
      6. Connecting Directly to Containers
    11. Ansible Container
      1. The Conductor
      2. Creating Docker Images
      3. Running Locally
      4. Publishing Images to Registries
      5. Deploying Containers to Production
  19. 16. Debugging Ansible Playbooks
    1. Humane Error Messages
    2. Debugging SSH Issues
    3. The Debug Module
    4. Playbook Debugger
    5. The Assert Module
    6. Checking Your Playbook Before Execution
      1. Syntax Check
      2. List Hosts
      3. List Tasks
      4. Check Mode
      5. Diff (Show File Changes)
    7. Limiting Which Tasks Run
      1. Step
      2. Start-at-Task
      3. Tags
  20. 17. Managing Windows Hosts
    1. Connection to Windows
    2. PowerShell
    3. Windows Modules
    4. Our First Playbook
    5. Updating Windows
    6. Adding Local Users
    7. Conclusion
  21. 18. Ansible for Network Devices
    1. Status of Network Modules
    2. List of Network Vendors Supported
    3. Preparing Our Network Device
      1. Enable SSH Authentication
    4. How the Modules Work
    5. Our First Playbook
    6. Inventory and Variables for Network Modules
      1. Local Connection
      2. Host Connection
      3. Authentication Variables
      4. Save the Config
    7. Use Configs from a File
    8. Templates, Templates, Templates
    9. Gathering Facts
    10. Conclusion
  22. 19. Ansible Tower: Ansible for the Enterprise
    1. Subscription Models
      1. Try Ansible Tower
    2. What Ansible Tower Solves
      1. Access Control
      2. Projects
      3. Inventory Management
      4. Run Jobs by Job Templates
    3. RESTful API
    4. Ansible Tower CLI
      1. Installation
      2. Create a User
      3. Launch a Job
    5. Onward
  23. A. SSH
    1. Native SSH
    2. SSH Agent
    3. Starting Up ssh-agent
      1. macOS
      2. Linux
    4. Agent Forwarding
      1. Sudo and Agent Forwarding
    5. Host Keys
  24. B. Using IAM Roles for EC2 Credentials
    1. AWS Management Console
    2. Command-Line
  25. Glossary
  26. Bibliography
  27. Index

Product information

  • Title: Ansible: Up and Running, 2nd Edition
  • Author(s): Lorin Hochstein, Rene Moser
  • Release date: August 2017
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781491979808