Practical AnsibleConfiguration Managementfrom Start to FinishSecond EditionVincent SestoPractical Ansible: Configuration Management from Start to Finish Vincent Sesto Auckland, New Zealand ISBN-13 (pbk): 978-1-4842-8642-5 ISBN-13 (electronic): 978-1-4842-8643-2 https://doi.org/10.1007/978-1-4842-8643-2 Copyright 2022 by Vincent Sesto This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made.
The publisher makes no warranty, express or implied, with respect to the material contained herein. Managing Director, Apress Media LLC: Welmoed Spahr Acquisitions Editor: Aditee Mirashi Development Editor: James Markham Coordinating Editor: Aditee Mirashi Cover designed by eStudioCalamar Cover image designed by Freepik (www.freepik.com) Distributed to the book trade worldwide by Springer Science+Business Media New York, 1 New York Plaza, Suite 4600, New York, NY 10004-1562, USA. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. For information on translations, please e-mail booktranslations@springernature.com; for reprint, paperback, or audio rights, please e-mail bookpermissions@springernature.com.
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales web page at http://www.apress.com/bulk-sales. Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub. For more detailed information, please visit http://www.apress. com/source- code.
Printed on acid-free paper This book is dedicated to Kirsty and ourtwo cats Rusty and Ash. Thanks for alwayskeeping me company while Im writing and generallyhelping me get the most out of life.Table of Contents About the Author xi About the Technical Reviewer xiii Preface xv Introduction xxi Chapter 1: Configuration Management with Ansible 1 Getting Started with Configuration Management2 Why Ansible? 3 Getting Started with Ansible 6 Installing Ansible 8 Ansible Support for SSH 10 Finally, Our First Ansible Command 12 Basic Ansible Playbooks 14 Ansible Configuration and Inventory Files 16 Running Ansible Modules 20 Ping 21 Setup 22 Git 23 Shell24 Apt (or Yum) 24 Package 26 v Table of ConTenTs Service 27 Get_url 27 File 29 User 30 Find 31 Running Ansible on Different Servers 32 Different Versions of Ansible 34 Ansible Projects 34 Basic LAMP Stack 34 Splunk Server Environment 35 Summary36 Chapter 2: Ansible Playbooks 37 Ansible and YAML Syntax 38 Command-Line Modules to Ansible Playbooks 41 Ansible Playbook Verbose Output 47 Ansible Syntax in Finer Detail 48 Hosts and Remote Users 48 Tasks 50 Notify 51 Adding More Playbook Functionality with Variables and Loops 52 Plugging In Our LAMP Stack 59 Organizing Larger Playbooks with Include and Import 67 Summary70 vi Table of ConTenTs Chapter 3: Extending Playbooks with Roles and Templates 73 Ansible Roles 74 Creating the Web Server Role 75 Creating the Database Server Role 82 Ansible Command-Line Variables and Options 86 Expanding Our LAMP with Django 87 Conditional Tasks in Ansible 96 Using Tags to Run Specific Tasks 100 Summary104 Chapter 4: Custom Ansible Modules, Vaults, and Galaxies 105 Keeping Secrets with Ansible Vault 106 Ansible Galaxy 113 Searching and Working with Ansible Galaxy Roles 113 Downloading and Installing Multiple Roles from Galaxy 123 Moving Further with Ansible Galaxy 126 Contributing to Ansible Galaxy 129 Building Your Own Ansible Modules 137 Summary144 Chapter 5: Working with Ansible in the Amazon Cloud 145 So Why Amazon Web Services? 146 AWS Is Not Free 147 AWS Pricing 147 Gaining Access to AWS147 Using Environment Variables to Access AWS 154 Ansible AWS Modules to Implement Our New Project 155 Our New Splunk Implementation Project 155 vii Table of ConTenTs Creating Our AWS Instance 160 Deploying Splunk with User Data 171 Failures on Amazon Web Services 178 Summary179 Chapter 6: Ansible Templates and CloudFormation Scripts 181 One Final Word on AWS Costs 182 Ansible Templates in AWS Instances 183 Pulling Code with Ansible191 Ansible Pull GitHub Repository Deployment Explained 197 Build AWS Images for Quicker Deployments 199 Using CloudFormation with Ansible 206 Summary216 Chapter 7: Ansible Troubleshooting and Variables 219 AWS Modules Run Differently 220 Using the Debug Module with Registered Variables 222 Ansible Facts 228 Ansible Galaxy README Files 232 Testing Ansible As You Develop 234 Ansible Start At Option 234 Ansible Step 236 Ansible Lint 236 List Ansible Tasks 245 Ansible Check Mode 246 Ansible Playbook Syntax Check 249 viii Table of ConTenTs Ansible Connections Without SSH 250 Migrating to Ansible 253 Summary254 Chapter 8: Testing Ansible with Molecule 255 Ansible Testing with the Molecule Testing Framework 256 Using Molecule with Code Deployed to AWS 270 Update Tests to Match Our AWS Instance 276 CICD with TravisCI 280 Using AWS on TravisCI 289 Summary291 Chapter 9: Managing Large Server Environments 293 Managing Inventory and Hosts Files 294 Using Multiple Hosts Files 295 Using Ranges in Hosts Files 296 Adding Variables in Hosts Files 297 Working with Dynamic Inventory 297 Introducing Ansible Tower 301 Installing AWX Ansible Tower 303 Getting Started with AWX Ansible Tower 308 Main Dashboard 308 The AWX Ansible Tower Main Menu 309 Set Up Your Organization and Users 310 Introducing Projects 312 Summary319 Index 321 ix About the AuthorVincent Sesto is a DevOps engineer, endurance athlete, and author. As a DevOps engineer, he specializes working with Linux and open source applications. He is particularly interested in helping people get more from the tools they have and is currently developing his skills in DevOps, continuous integration, security, Splunk (UI and Reporting), and Python development. xi About the Technical ReviewerLambodar Ray is an IT professional in the United States. He is a database specialist with focused expertise in Amazon Cloud services and Oracle Cloud Infrastructure.
He has worked in delivering database and infrastructure solutions in diverse industry and business domains including workforce management, finance, procurement, manufacturing, sales, and compensation. He is also a Certified Oracle Cloud Architect Professional and Amazon AWS Architect Associate. xiii
Next page