Book Image

Learning Ansible 2.7 - Third Edition

By : Fabio Alessandro Locati
Book Image

Learning Ansible 2.7 - Third Edition

By: Fabio Alessandro Locati

Overview of this book

Ansible is an open source automation platform that assists organizations with tasks such as application deployment, orchestration, and task automation. With the release of Ansible 2.7, even complex tasks can be handled much more easily than before. Learning Ansible 2.7 will help you take your first steps toward understanding the fundamentals and practical aspects of Ansible by introducing you to topics such as playbooks, modules, and the installation of Linux, Berkeley Software Distribution (BSD), and Windows support. In addition to this, you will focus on various testing strategies, deployment, and orchestration to build on your knowledge. The book will then help you get accustomed to features including cleaner architecture, task blocks, and playbook parsing, which can help you to streamline automation processes. Next, you will learn how to integrate Ansible with cloud platforms such as Amazon Web Services (AWS) before gaining insights into the enterprise versions of Ansible, Ansible Tower and Ansible Galaxy. This will help you to use Ansible to interact with different operating systems and improve your working efficiency. By the end of this book, you will be equipped with the Ansible skills you need to automate complex tasks for your organization.
Table of Contents (18 chapters)
Free Chapter
1
Section 1: Creating a Web Server Using Ansible
4
Section 2: Deploying Playbooks in a Production Environment
9
Section 3: Deploying an Application with Ansible
13
Section 4: Deploying an Application with Ansible

Using Ruby modules

Writing modules in Ruby is as easy as writing a module in Python or bash. You just need to take care of the arguments, errors, return statements, and, of course, know basic Ruby! Let's create the library/rsync.rb file with the following code. The full code is available on GitHub:

#!/usr/bin/env ruby 
 
require 'rsync' 
require 'json' 
 
src = '' 
dest = '' 
ret_msg = '' 
SUCCESS = '' 
 
def print_message(state, msg, key='failed') 
    message = { 
        key => state, 
        "msg" => msg 
    } 
    print message.to_json 
    exit 1 if state == false 
    exit 0 
...

In the preceding module, we first process the user arguments, then copy the file using the rsync library, and finally, return the output.

To be able to use this, you need to ensure that the rsync library...