Ansible for Devops Server and Configuration Management for Humans
Total Page:16
File Type:pdf, Size:1020Kb
Ansible for DevOps Server and configuration management for humans Jeff Geerling This book is for sale at http://leanpub.com/ansible-for-devops This version was published on 2015-09-26 ISBN 978-0-9863934-0-2 This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and many iterations to get reader feedback, pivot until you have the right book and build traction once you do. ©2014 - 2015 Jeff Geerling Tweet This Book! Please help Jeff Geerling by spreading the word about this book on Twitter! The suggested tweet for this book is: I just purchased @Ansible4DevOps by @geerlingguy on @leanpub - https://leanpub.com/ansible-for-devops #ansible The suggested hashtag for this book is #ansible. Find out what other people are saying about the book by clicking on this link to search for this hashtag on Twitter: https://twitter.com/search?q=#ansible To my wife and children, and to the many readers who have helped make this book a reality. Cover photograph and illustration © 2011 Jeff Geerling Ansible is a software product distributed under the GNU GPLv3 open source license. Editing by Margie Newman and Katherine Geerling. Contents Preface ............................................... i Who is this book for? ..................................... i Typographic conventions ................................... ii Please help improve this book! ................................ iii About the Author ....................................... iii Introduction ............................................ iv In the beginning, there were sysadmins ........................... iv Modern infrastructure management ............................. iv Ansible and Ansible, Inc. ................................... v Ansible Examples ....................................... vi Other resources ........................................ vi Chapter 1 - Getting Started with Ansible ............................ 1 Ansible and Infrastructure Management ........................... 1 On snowflakes and shell scripts .............................. 1 Configuration management ................................ 1 Installing Ansible ....................................... 2 Creating a basic inventory file ................................ 4 Running your first Ad-Hoc Ansible command ........................ 5 Summary ............................................ 6 Chapter 2 - Local Infrastructure Development: Ansible and Vagrant ............ 8 Prototyping and testing with local virtual machines ..................... 8 Your first local server: Setting up Vagrant .......................... 9 Using Ansible with Vagrant .................................. 9 Your first Ansible playbook .................................. 10 Summary ............................................ 13 Chapter 3 - Ad-Hoc Commands ................................. 14 Conducting an orchestra ................................... 14 Build infrastructure with Vagrant for testing ......................... 15 Inventory file for multiple servers .............................. 17 Your first ad-hoc commands ................................. 18 CONTENTS Discover Ansible’s parallel nature ............................. 18 Learning about your environment ............................. 20 Make changes using Ansible modules ........................... 22 Configure groups of servers, or individual servers ...................... 23 Configure the Application servers ............................. 23 Configure the Database servers .............................. 24 Make changes to just one server .............................. 25 Manage users and groups ................................... 26 Manage files and directories .................................. 27 Get information about a file ................................ 27 Copy a file to the servers .................................. 27 Retrieve a file from the servers ............................... 28 Create directories and files ................................. 28 Delete directories and files ................................. 28 Run operations in the background .............................. 29 Update servers asynchronously, monitoring progress . 29 Fire-and-forget tasks .................................... 30 Check log files ......................................... 31 Manage cron jobs ....................................... 32 Deploy a version-controlled application ........................... 32 Ansible’s SSH connection history ............................... 33 Paramiko .......................................... 34 OpenSSH (default) ..................................... 34 Accelerated Mode ..................................... 34 Faster OpenSSH in Ansible 1.5+ .............................. 35 Summary ............................................ 36 Chapter 4 - Ansible Playbooks .................................. 37 Power plays .......................................... 37 Running Playbooks with ansible-playbook ......................... 41 Limiting playbooks to particular hosts and groups .................... 41 Setting user and sudo options with ansible-playbook . 42 Other options for ansible-playbook ........................... 43 Real-world playbook: CentOS Node.js app server ...................... 43 Add extra repositories ................................... 44 Deploy a Node.js app .................................... 47 Launch a Node.js app .................................... 48 Node.js app server summary ................................ 49 Real-world playbook: Ubuntu LAMP server with Drupal . 50 Include a variables file, and discover pre_tasks and handlers . 50 Basic LAMP server setup .................................. 51 Configure Apache ..................................... 53 Configure PHP with lineinfile ............................. 55 CONTENTS Configure MySQL ..................................... 56 Install Composer and Drush ................................ 56 Install Drupal with Git and Drush ............................. 58 Drupal LAMP server summary .............................. 60 Real-world playbook: Ubuntu Apache Tomcat server with Solr . 60 Include a variables file, and more pre_tasks and handlers . 61 Install Apache Tomcat 7 .................................. 62 Install Apache Solr ..................................... 62 Apache Solr server summary ............................... 66 Summary ............................................ 66 Chapter 5 - Ansible Playbooks - Beyond the Basics ...................... 68 Handlers ............................................ 68 Environment variables ..................................... 69 Per-play environment variables .............................. 70 Variables ............................................ 72 Playbook Variables ..................................... 72 Inventory variables ..................................... 74 Registered Variables .................................... 75 Accessing Variables ..................................... 75 Host and Group variables ................................. 77 Automatically-loaded group_vars and host_vars . 78 Magic variables with host and group variables and information . 78 Facts (Variables derived from system information) .................... 79 Local Facts (Facts.d) ................................ 80 Ansible Vault - Keeping secrets secret ........................... 81 Variable Precedence .................................... 84 If/then/when - Conditionals .................................. 85 Jinja2 Expressions, Python built-ins, and Logic ...................... 86 register .......................................... 87 when ............................................. 87 changed_when and failed_when .............................. 89 ignore_errors ....................................... 90 Delegation, Local Actions, and Pauses ............................ 90 Pausing playbook execution with wait_for ........................ 91 Running an entire playbook locally ............................ 92 Prompts ............................................ 92 Tags ............................................... 93 Blocks ............................................. 95 Summary ............................................ 96 Chapter 6 - Playbook Organization - Roles and Includes ................... 98 Includes ............................................ 98 CONTENTS Dynamic includes .....................................100 Handler includes ......................................100 Playbook includes .....................................101 Complete includes example ................................101 Roles ..............................................104 Role scaffolding .......................................104 Building your first role ...................................105 More flexibility with role vars and defaults . 106 Other role parts: handlers, files, and templates . 108 Handlers ......................................108 Files and Templates .................................109 Organizing more complex and cross-platform roles . 110 Ansible Galaxy .........................................111 Getting roles from Galaxy .................................112 Using role requirements files to manage dependencies . 112 A LAMP server in eight lines of YAML . 113 A Solr server in eight lines of YAML . 114 Helpful Galaxy commands .................................115 Contributing to Ansible Galaxy ..............................116 Summary ............................................116