Learning SaltStack Learn how to manage your infrastructure by utilizing the power of SaltStack Colton Myers BIRMINGHAM - MUMBAI Learning SaltStack Copyright © 2015 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: January 2015 Production reference: 1210115 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78439-460-8 www.packtpub.com Credits Author Project Coordinator Colton Myers Neha Thakur Reviewers Proofreaders Darrel Clute Simran Bhogal Cameron R. Lane Stephen Copestake Hoàng Đình Quân Maria Gould Ameesha Green Commissioning Editor Paul Hindle Pramila Balan Indexer Acquisition Editor Tejal Soni Kevin Colaco Production Coordinator Content Development Editor Alwin Roy Parita Khedekar Cover Work Technical Editor Alwin Roy Vijin Boricha Copy Editors Sarang Chari Relin Hedly About the Author Colton Myers is a software engineer living in Salt Lake City, Utah. Since graduating with a BS in computer science from the University of Utah, he has worked professionally writing software in Python. Colton loves working on open source software, and he has represented the US PyCon conference as a speaker multiple times. He is a SaltStack Certified Trainer and has worked on the Salt open source software for years. At the time of writing this book, Colton was working as a platform engineer for SaltStack. You can find him on Twitter and GitHub at @basepi. I would like to thank my friends and family for the support they've given me while writing this book. I especially want to thank my parents, Mark and Annette, as I would not be who or where I am without their support. Finally, I'd like to thank Thomas Hatch, the creator of SaltStack, who gave me the opportunity to help develop such a great piece of software. About the Reviewers Darrel Clute is an IT Infrastructure Architect. Throughout his career, he has been predominately focused on network engineering, but has spent equal amounts of time on systems engineering, primarily with UNIX-based systems. Besides his employment, he is also an advocate for open source software used in the enterprise as well as for individuals, but will never advocate the use of open source purely for the sake of it. Outside his core competencies, Darrel also has extensive experience with proprietary and open source virtualization platforms. His knowledge and experience in IaaS solutions such as OpenStack are constantly increasing as well. Additionally, through his systems engineering experience, he has been exposed to and supported various web and enterprise applications. The most recent exposure has been to PaaS solutions, and this has led to advocation that either a public or private cloud offering is not complete without a coupling of both Iaas and PaaS solutions designed and deployed in lockstep. Besides core infrastructure, he has, at various times, and especially more so recently, been developing programming skills to augment his daily activities. Throughout his career, Darrel has utilized various languages and utilities to automate infrastructure. Some of his programming has been with the use of Bash, Net-SNMP, Sed, and Awk, and holistically with Python. Cameron R. Lane is a full-stack engineer with an interest in a broad range of languages and technologies. He is currently a senior software developer at Synapp.io in Atlanta, GA, where he designs and builds scalable APIs for the Web using MongoDB and Python. I would like to thank my wife, Stephanie, for her understanding and patience through all of life's changes, big and small. It is her support that gives me the motivation and confidence to take on big challenges. I would also like to thank my colleagues at Synapp.io, who teach me something new about technology every day. Hoàng Đình Quân is a Vietnamese System Administrator with experience in operating Network Infrastructure and OpenStack cloud platform automation tools (such as Ansible and SaltStack). www.PacktPub.com Support files, eBooks, discount offers, and more For support files and downloads related to your book, please visit www.PacktPub.com. Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub. com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.PacktPub.com, 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 Packt books and eBooks. TM https://www2.packtpub.com/books/subscription/packtlib Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books. Why subscribe? • Fully searchable across every book published by Packt • Copy and paste, print, and bookmark content • On demand and accessible via a web browser Free access for Packt account holders If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view 9 entirely free books. Simply use your login credentials for immediate access. Table of Contents Preface 1 Chapter 1: Diving In – Our First Salt Commands 7 Introducing Salt 7 Installing Salt 8 Installation with system packages (Ubuntu) 9 Installing with Salt-Bootstrap 10 Configuring Salt 11 Firewall configuration 11 Salt minion configuration 12 Starting the Salt master and Salt minion 13 Accepting the minion key on the master 14 A game of ping pong 15 Masterless Salt 18 Summary 20 Chapter 2: Controlling Your Minions with Remote Execution 21 The structure of a remote execution command 21 Command-line options 22 Targeting strings 25 Glob matching 25 Perl-compatible regular expression matching 26 List matching 28 Grain and pillar matching 28 Compound matching 33 Remote execution modules and functions 35 Adding users 37 Installing packages 38 Managing services 41 Monitoring minion states 42 Running arbitrary commands 43 Summary 45