📄 Page
1
(This page has no text content)
📄 Page
2
IT Infrastructure Automation Using Ansible Guidelines to Automate the Network, Windows, Linux, and Cloud Administration Waqas Irtaza www.bpbonline.com
📄 Page
3
FIRST EDITION 2022 Copyright © BPB Publications, India ISBN: 978-93-91030-032 All Rights Reserved. No part of this publication may be reproduced, distributed or transmitted in any form or by any means or stored in a database or retrieval system, without the prior written permission of the publisher with the exception to the program listings which may be entered, stored and executed in a computer system, but they can not be reproduced by the means of publication, photocopy, recording, or by any electronic and mechanical means. LIMITS OF LIABILITY AND DISCLAIMER OF WARRANTY The information contained in this book is true to correct and the best of author’s and publisher’s knowledge. The author has made every effort to ensure the accuracy of these publications, but publisher cannot be held responsible for any loss or damage arising from any information in this book. All trademarks referred to in the book are acknowledged as properties of their respective owners but BPB Publications cannot guarantee the accuracy of this information.
📄 Page
4
www.bpbonline.com
📄 Page
5
Dedicated to NAZMA AND ZIMAL My wife and daughter, for letting me sacrificing family time in peruse of my passion
📄 Page
6
About the Author Waqas Irtaza is an experienced IT professional, who for the past 11 years working cross infrastructure domains. He started his career with system administration, then move to network, wireless and cloud administration. Later in his career he start using his diverse experience in Infrastructure automation. He is certified from almost all technology leaders Cisco, Microsoft, AWS, PMI, EC-Council and Linux Foundation. He has started his automation journey with small python and shell scripts. He managed to write complex script later in his career to automate the day to day repetitive work for cross IT infrastructures. Ansible was a pleasant surprise for him, since complex IT task can be managed with simple Ansible playbooks. This simplicity and diversity of Ansible encourage him to write this book. Outside work, Waqas like cycling and horse riding in addition to help, coach, and mentor young people in taking up their careers in technology.
📄 Page
7
About the Reviewer Sumit Jaiswal has 10 years of industry experience in the development domain with core development technologies like Python, C++, and C#. He's currently working as a Senior software engineer under the Ansible automation team and working on developing content for the networking and security domain. He's an enthusiast of open-source projects.
📄 Page
8
Acknowledgement There are a few people I want to thank for the continued and ongoing support they have given me during the writing of this book. First and foremost, I would like to thank my wife and daughter, Nazma and Zimal for putting up with me while I was spending many weekends and evenings on writing—I could have never completed this book without their support. This book wouldn’t have happened if I hadn’t had the support from my line Manager Mr. John Daniel and Manager Technology Command Centre Mr. Manoj Panicker. My gratitude goes to my team Technology Command Centre (TCC) at The Emirates Group, especially Mr. Ahmed Humayun Zaheer, for providing valuable insights and expert opinions. Finally, I would like to thank BPB Publications for giving me this opportunity to write my first book for them.
📄 Page
9
Preface Ansible is a powerful open source automation language. Uniquely, it’s also a deployment and orchestration tool. While Ansible provides more productive drop-in replacements for many core capabilities in other automation solutions, it also seeks to solve other major unsolved IT challenges. Ansible is an open-source automation tool, or platform, used for IT tasks such as configuration management, application deployment, service orchestration, and infrastructure provisioning. Automation is crucial these days, with IT environments that are complex and often need to scale quickly for system, network and cloud administrators and developers to keep up if they had to do everything manually. Automation simplifies complex infrastructure tasks, not just making developers’ jobs more manageable but allowing them to focus attention on other tasks that add value to an organization. In other words, it frees up time and increases efficiency. And Ansible, as noted above, is rapidly rising to the top in the world of automation tools. When I start learning Ansible and gone through couple of books I have noticed that I have to toggle here and there to clear Ansible concepts. Therefore, I thought to come up with a book which covers all aspects of infrastructure administration. This book begin with Ansible basics so anyone with no automation experience can start from there. Once basics are cleared each chapter is specifically designed for respective technology. Over the course of 7 chapters in this book, you will learn the following:
📄 Page
10
Chapter 1 Lab setup and introduction to ansible and core concepts Chapter 2 Understand Ansible Ad-Hoc and playbook with examples. This chapter also covers the core concepts and practical implementations. Chapter 3 This chapter covers the Ansible advance concepts which will help reader writing professional playbooks. Chapter 4 is a key chapter for network administration. This chapter will discuss one liner Ad-Hoc commands which are handy for network administrators. Later this chapter discuss advance Ansible concepts specifically for network administration Chapter 5 is also a key chapter for system administration which covers both Linux and Windows Infrastructures for automation. Chapter 6 is a key chapter for DevOps and cloud Administrators. It covers Ansible automation for the public and private clouds in addition to Docker and Kubernates. Chapter 7 is the final chapter that discusses recommendation and best practices for Ansible.
📄 Page
11
Downloading the coloured images: Please follow the link to download the Coloured Images of the book: https://rebrand.ly/f936d4 Errata We take immense pride in our work at BPB Publications and follow best practices to ensure the accuracy of our content to provide with an indulging reading experience to our subscribers. Our readers are our mirrors, and we use their inputs to reflect and improve upon human errors, if any, that may have occurred during the publishing processes involved. To let us maintain the quality and help us reach out to any readers who might be having difficulties due to any unforeseen errors, please write to us at : errata@bpbonline.com Your support, suggestions and feedbacks are highly appreciated by the BPB Publications’ Family.
📄 Page
12
Did you know that BPB offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.bpbonline.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at business@bpbonline.com for more details. At you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on BPB books and eBooks.
📄 Page
13
BPB is searching for authors like you If you're interested in becoming an author for BPB, please visit www.bpbonline.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea. The code bundle for the book is also hosted on GitHub at In case there's an update to the code, it will be updated on the existing GitHub repository. We also have other code bundles from our rich catalog of books and videos available at Check them out! PIRACY If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at business@bpbonline.com with a link to the material. If you are interested in becoming an author
📄 Page
14
If there is a topic that you have expertise in, and you are interested in either writing or contributing to a book, please visit REVIEWS Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at BPB can understand what you think about our products, and our authors can see your feedback on their book. Thank you! For more information about BPB, please visit
📄 Page
15
Table of Contents 1. Up and Running with Ansible Structure Objective Introduction to Ansible Lab setup Infrastructure preparation for Ansible Ansible installation Control node setup Verify the Ansible installation Managed node setup Important concepts about Ansible Ansible Inventory Ansible configuration file Ansible modules Command Shell RAW PING Copy File YUM/APT Basic understanding of YAML Conclusion Multiple choice questions Multiple choice questions answers Points to remember Key terms
📄 Page
16
2. Ansible Basics Structure Objective Ansible ad-hoc mode Pattern Inventory Modules Examples for ad-hoc mode Working with host environmental variable Managing files Managing packages Managing users and groups Managing services Gathering facts Ansible playbooks Ansible variables User defined variables Built-in variables Ansible output Conditional statements in Ansible Loops in Ansible Install listed software on Ubuntu machines Display content of two files Print a sequence from 0 to 10 Ansible handler Ansible error handling Ignoring failed commands Resetting unreachable hosts Controlling what defines failure
📄 Page
17
Ansible Vault Modify the playbook Run the playbook Conclusion Multiple choice questions Multiple choice answers Points to remember Key terms 3. Ansible Advance Concepts Structure Objective Managing task control With_items Nested loops (with_nested) When statement Registering variables Handler Tags Dealing with errors Blocks Ansible file separation Ansible include statement Ansible roles Directory structure for role Role variables Defining role dependencies Order of execution in role Ansible Galaxy Galaxy CLI tool
📄 Page
18
Ansible optimization Host patterns Configuring delegation Delegation host outside of inventory Parallelism in Ansible Asynchronous tasks Ansible wait_for Ansible async_status Troubleshooting Ansible Ansible logging Ansible common errors Troubleshooting managed hosts Ansible modules for troubleshooting Ansible ad-hoc commands for troubleshooting Ansible file lookup Ansible template Ansible dynamic inventory Ansible filters Conclusion Multiple choice questions Multiple choice question answers Points to remember Key terms 4. Ansible for Network Administration Structure Objective Lab setup Network administration with RAW module Troubleshooting with RAW module
📄 Page
19
Ansible network modules New concepts for network modules Network communication protocol Network platform Privilege escalation Birds eye view for network modules Understand Cisco ios_command Parameters for ios_command Understand Juniper junos_command Parameters for junos_command Additional requirements for junos_command module Understand Cisco ios_config Parameters for ios_config Understand Juniper junos_config Parameters for junos_config Additional requirements for junos_config module Ansible custom filter Template playbook for network administration Conclusion Multiple choice questions Multiple choice questions answer Points to remember Key terms 5. Ansible for System Administration Structure Objective Lab setup System administration Execute commands on remote machines
📄 Page
20
Remote command execution modules for Linux servers Ansible command module Parameters for command module Ansible shell module Parameters for shell module Remote command execution modules for Windows Servers Ansible win_command module Parameters for win_command module Ansible win_shell module Parameters for win_shell module Bulk scripts execution on remote machines Ansible script module Parameters for script module User management Modules for user management in Linux servers Ansible user module Parameters for user module Modules for user management in Windows servers Ansible win_domain_user module Parameters for win_domain_user Ansible win_domain_computer module Parameters for win_domain_computer module Package management Ansible package management module for Linux servers Ansible package module Parameters package module Ansible package management module for Windows servers Ansible win_package module Parameter for win_package module Ansible win_feature module