实例介绍
【实例简介】
【实例截图】
【核心代码】
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 . . . . . . . . . . . . 7 Prototyping and testing with local virtual machines . . . . . . . . . . . . . . . . . . . . . 7 Your first local server: Setting up Vagrant . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Using Ansible with Vagrant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Your first Ansible playbook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Chapter 3 - Ad-Hoc Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Conducting an orchestra . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Build infrastructure with Vagrant for testing . . . . . . . . . . . . . . . . . . . . . . . . . 14 Inventory file for multiple servers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Your first ad-hoc commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 CONTENTS Discover Ansible’s parallel nature . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Learning about your environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Make changes using Ansible modules . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Configure groups of servers, or individual servers . . . . . . . . . . . . . . . . . . . . . . 22 Configure the Application servers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Configure the Database servers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Make changes to just one server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Manage users and groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Manage files and directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Get information about a file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Copy a file to the servers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Retrieve a file from the servers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Create directories and files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Delete directories and files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Run operations in the background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Update servers asynchronously, monitoring progress . . . . . . . . . . . . . . . . . . 28 Fire-and-forget tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Check log files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Manage cron jobs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Deploy a version-controlled application . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Ansible’s SSH connection history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Paramiko . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 OpenSSH (default) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Accelerated Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Faster OpenSSH in Ansible 1.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Chapter 4 - Ansible Playbooks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Power plays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Running Playbooks with ansible-playbook . . . . . . . . . . . . . . . . . . . . . . . . . 40 Limiting playbooks to particular hosts and groups . . . . . . . . . . . . . . . . . . . . 40 Setting user and sudo options with ansible-playbook . . . . . . . . . . . . . . . . . 41 Other options for ansible-playbook . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Real-world playbook: CentOS Node.js app server . . . . . . . . . . . . . . . . . . . . . . . 42 Add extra repositories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Deploy a Node.js app . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Launch a Node.js app . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Node.js app server summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Real-world playbook: Ubuntu LAMP server with Drupal . . . . . . . . . . . . . . . . . . . 49 Include a variables file, and discover pre_tasks and handlers . . . . . . . . . . . . . 49 Basic LAMP server setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Configure Apache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Configure PHP with lineinfile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 CONTENTS Configure MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Install Composer and Drush . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Install Drupal with Git and Drush . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Drupal LAMP server summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Real-world playbook: Ubuntu Apache Tomcat server with Solr . . . . . . . . . . . . . . . 59 Include a variables file, and discover pre_tasks and handlers . . . . . . . . . . . . . 60 Install Apache Tomcat 7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Install Apache Solr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Apache Solr server summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Chapter 5 - Ansible Playbooks - Beyond the Basics . . . . . . . . . . . . . . . . . . . . . . 66 Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 Environment variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Per-play environment variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Playbook Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Inventory variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Registered Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Accessing Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Host and Group variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 group_vars and host_vars . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 Magic variables with host and group variables and information . . . . . . . . . 76 Facts (Variables derived from system information) . . . . . . . . . . . . . . . . . . . 77 Local Facts (Facts.d) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Variable Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 If/then/when - Conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 Jinja2 Expressions, Python built-ins, and Logic . . . . . . . . . . . . . . . . . . . . . 81 register . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 when . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 changed_when and failed_when . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 ignore_errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Delegation, Local Actions, and Pauses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Pausing playbook execution with wait_for . . . . . . . . . . . . . . . . . . . . . . . 86 Running an entire playbook locally . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Prompts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 Chapter 6 - Playbook Organization - Roles and Includes . . . . . . . . . . . . . . . . . . . 91 Includes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Handler includes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 Playbook includes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 CONTENTS Complete includes example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 Roles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 Role scaffolding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 Building your first role . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 More flexibility with role vars and defaults . . . . . . . . . . . . . . . . . . . . . . . 99 Other role parts: handlers, files, and templates . . . . . . . . . . . . . . . . . . . . . . 101 Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 Files and Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 Organizing more complex and cross-platform roles . . . . . . . . . . . . . . . . . . . 102 Ansible Galaxy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 Getting roles from Galaxy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 Using role requirements files to manage dependencies . . . . . . . . . . . . . . 105 A LAMP server in six lines of YAML . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 A Solr server in six lines of YAML . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 Helpful Galaxy commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 Contributing to Ansible Galaxy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 Chapter 7 - Inventories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 A real-world web application server inventory . . . . . . . . . . . . . . . . . . . . . . . . 109 Non-prod environments, separate inventory files . . . . . . . . . . . . . . . . . . . . 113 Inventory variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 host_vars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 group_vars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 Ephemeral infrastructure: Dynamic inventory . . . . . . . . . . . . . . . . . . . . . . . . 116 Dynamic inventory with DigitalOcean . . . . . . . . . . . . . . . . . . . . . . . . . . 117 DigitalOcean account prerequisites . . . . . . . . . . . . . . . . . . . . . . . . 117 Connecting to your DigitalOcean account . . . . . . . . . . . . . . . . . . . . 117 Creating a droplet with Ansible . . . . . . . . . . . . . . . . . . . . . . . . . . 118 DigitalOcean dynamic inventory with digital_ocean.py . . . . . . . . . . . . 121 Dynamic inventory with AWS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 Inventory on-the-fly: add_host and group_by . . . . . . . . . . . . . . . . . . . . . . 123 Multiple inventory sources - mixing static and dynamic inventories . . . . . . . . . . 124 Creating custom dynamic inventories . . . . . . . . . . . . . . . . . . . . . . . . . . 124 Building a Custom Dynamic Inventory in Python . . . . . . . . . . . . . . . . 125 Building a Custom Dynamic Inventory in PHP . . . . . . . . . . . . . . . . . . 129 Managing a PaaS with a Custom Dynamic Inventory . . . . . . . . . . . . . . 132 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 Chapter 8 - Ansible Cookbooks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 Highly-Available Infrastructure with Ansible . . . . . . . . . . . . . . . . . . . . . . . . . 134 Directory Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 Individual Server Playbooks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 CONTENTS Main Playbook for Configuring All Servers . . . . . . . . . . . . . . . . . . . . . . . 145 Getting the required roles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 Vagrantfile for Local Infrastructure via VirtualBox . . . . . . . . . . . . . . . . . . . 146 Provisioner Configuration: DigitalOcean . . . . . . . . . . . . . . . . . . . . . . . . . 150 Provisioner Configuration: Amazon Web Services (EC2) . . . . . . . . . . . . . . . . 154 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 ELK Logging with Ansible . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 ELK Playbook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 Forwarding Logs from Other Servers . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 GlusterFS Distributed File System Configuration with Ansible . . . . . . . . . . . . . . . . 170 Configuring Gluster - Basic Overview . . . . . . . . . . . . . . . . . . . . . . . . . . 172 Configuring Gluster with Ansible . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 Mac Provisioning with Ansible and Homebrew . . . . . . . . . . . . . . . . . . . . . . . . 178 Running Ansible playbooks locally . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 Automating Homebrew package and app management . . . . . . . . . . . . . . . . . 179 Configuring Mac OS X through dotfiles . . . . . . . . . . . . . . . . . . . . . . . . . 181 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 Docker-based Infrastructure with Ansible . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 A brief introduction to Docker containers . . . . . . . . . . . . . . . . . . . . . . . . 183 Using Ansible to build and manage containers . . . . . . . . . . . . . . . . . . . . . . 184 Building a Flask app with Ansible and Docker . . . . . . . . . . . . . . . . . . . . . . 186 Data storage container . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190 Flask container . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 MySQL container . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 Ship it! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198 Chapter 9 - Deployments with Ansible . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 Deployment strategies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 Simple single-server deployments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 Provisioning a simple Ruby on Rails server . . . . . . . . . . . . . . . . . . . . . . . 200 Deploying a Rails app to the server . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202 Provisioning and Deploying the Rails App . . . . . . . . . . . . . . . . . . . . . . . . 206 Deploying application updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 Zero-downtime multi-server deployments . . . . . . . . . . . . . . . . . . . . . . . . . . . 210 Ensuring zero downtime with serial and integration tests . . . . . . . . . . . . . . . 217 Deploying to app servers behind a load balancer . . . . . . . . . . . . . . . . . . . . . 219 Capistrano-style and blue-green deployments . . . . . . . . . . . . . . . . . . . . . . . . . 225 Additional Deployment Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 CONTENTS Chapter 10 - Server Security and Ansible . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228 A brief history of SSH and remote access . . . . . . . . . . . . . . . . . . . . . . . . . . . 228 Telnet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 rlogin, rsh and rcp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230 SSH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230 The evolution of SSH and the future of remote access . . . . . . . . . . . . . . . . . . 232 Use secure and encrypted communication . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 Disable root login and use sudo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234 Remove unused software, open only required ports . . . . . . . . . . . . . . . . . . . . . . 235 Use the principle of least privilege . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236 User account configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236 File permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237 Update the OS and installed software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 Automating updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 Automating updates for RedHat-based systems . . . . . . . . . . . . . . . . . . . . . 239 Automating updates for Debian-based systems . . . . . . . . . . . . . . . . . . . . . 239 Use a properly-configured firewall . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240 Configuring a firewall with ufw on Debian or Ubuntu . . . . . . . . . . . . . . . . . . 240 Configuring a firewall with firewalld on RedHat, Fedora, or CentOS . . . . . . . . . 241 Make sure log files are populated and rotated . . . . . . . . . . . . . . . . . . . . . . . . . 243 Monitor logins and block suspect IP addresses . . . . . . . . . . . . . . . . . . . . . . . . 244 Use SELinux (Security-Enhanced Linux) or AppArmor . . . . . . . . . . . . . . . . . . . . 244 Summary and further reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 Chapter 11 - Automating Your Automation - Ansible Tower and CI/CD . . . . . . . . . . 247 Ansible Tower . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247 Getting and Installing Ansible Tower . . . . . . . . . . . . . . . . . . . . . . . . . . . 248 Using Ansible Tower . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 Other Tower Features of Note . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 Tower Alternatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252 Jenkins CI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252 Build a local Jenkins server with Ansible . . . . . . . . . . . . . . . . . . . . . . . . . 253 Create an Ansible playbook on the Jenkins server . . . . . . . . . . . . . . . . . . . . 254 Create a Jenkins job to run an Ansible Playbook . . . . . . . . . . . . . . . . . . . . . 255 Unit, Integration, and Functional Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . 256 Debugging and Asserting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257 The debug module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257 The fail and assert modules . . . . . . . . . . . . . . . . . . . . . . . . . . . 259 Checking syntax and performing dry runs . . . . . . . . . . . . . . . . . . . . . . . . 260 Automated testing on GitHub using Travis CI . . . . . . . . . . . . . . . . . . . . . . 261 Setting up a role for testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 Testing the role’s syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263 Role success - first run . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263 CONTENTS Role idempotence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263 Role success - final result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264 Some notes about Travis CI . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264 Real-world examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 Functional testing using serverspec . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266 Appendix A - Using Ansible on Windows workstations . . . . . . . . . . . . . . . . . . . 267 Prerequisites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 Set up an Ubuntu Linux Virtual Machine . . . . . . . . . . . . . . . . . . . . . . . . . . . 268 Log into the Virtual Machine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268 Install Ansible . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271 Appendix B - Ansible Best Practices and Conventions . . . . . . . . . . . . . . . . . . . . 272 Playbook Organization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272 Write comments and use name liberally . . . . . . . . . . . . . . . . . . . . . . . . . . 272 Include related variables and tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 Use Roles to bundle logical groupings of configuration . . . . . . . . . . . . . . . . . 274 Use role defaults and vars correctly . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274 YAML Conventions and Best Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274 YAML for Ansible tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275 Three ways to format Ansible tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276 Shorthand/one-line (key=value) . . . . . . . . . . . . . . . . . . . . . . . . . . 276 Structured map/multi-line (key:value) . . . . . . . . . . . . . . . . . . . . . . 276 Folded scalars/multi-line (>) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277 Using | to format multiline variables . . . . . . . . . . . . . . . . . . . . . . . . . . . 278 Using ansible-playbook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 Use Ansible Tower . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 Specify --forks for playbooks running on > 5 servers . . . . . . . . . . . . . . . . . . . . 279 Use Ansible’s Configuration file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论