Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
The Linux DevOps Handbook

You're reading from   The Linux DevOps Handbook Customize and scale your Linux distributions to accelerate your DevOps workflow

Arrow left icon
Product type Paperback
Published in Nov 2023
Publisher Packt
ISBN-13 9781803245669
Length 428 pages
Edition 1st Edition
Languages
Tools
Concepts
Arrow right icon
Authors (2):
Arrow left icon
Damian Wojsław Damian Wojsław
Author Profile Icon Damian Wojsław
Damian Wojsław
Grzegorz Adamowicz Grzegorz Adamowicz
Author Profile Icon Grzegorz Adamowicz
Grzegorz Adamowicz
Arrow right icon
View More author details
Toc

Table of Contents (20) Chapters Close

Preface 1. Part 1: Linux Basics
2. Chapter 1: Choosing the Right Linux Distribution FREE CHAPTER 3. Chapter 2: Command-Line Basics 4. Chapter 3: Intermediate Linux 5. Chapter 4: Automating with Shell Scripts 6. Part 2: Your Day-to-Day DevOps Tools
7. Chapter 5: Managing Services in Linux 8. Chapter 6: Networking in Linux 9. Chapter 7: Git, Your Doorway to DevOps 10. Chapter 8: Docker Basics 11. Chapter 9: A Deep Dive into Docker 12. Part 3: DevOps Cloud Toolkit
13. Chapter 10: Monitoring, Tracing, and Distributed Logging 14. Chapter 11: Using Ansible for Configuration as Code 15. Chapter 12: Leveraging Infrastructure as Code 16. Chapter 13: CI/CD with Terraform, GitHub, and Atlantis 17. Chapter 14: Avoiding Pitfalls in DevOps 18. Index 19. Other Books You May Enjoy

Ansible Galaxy

Ansible is a powerful automation tool that enables users to configure, deploy, and manage complex IT infrastructures with ease. However, creating and maintaining Ansible playbooks can be time-consuming, especially when working with large-scale environments. Fortunately, Ansible Galaxy exists to help streamline this process by providing a centralized repository of pre-built roles and playbooks that can be easily integrated into an existing Ansible project.

Ansible Galaxy is a community-driven platform that hosts an extensive collection of Ansible roles and playbooks. These roles and playbooks are submitted by users from around the world and are reviewed and curated by Ansible’s maintainers. Ansible Galaxy provides a simple, efficient way to find and use pre-built automation content that can save users time and effort while ensuring quality and consistency.

Using Ansible Galaxy, users can quickly find, download, and use pre-built roles and playbooks for popular applications, services, and infrastructure components. These pre-built components can help speed up deployment times, ensure best practices are followed, and reduce the likelihood of errors or inconsistencies. Ansible Galaxy can also help users learn from others’ experiences and gain insights into the best practices of their peers.

Let’s use one of the Galaxy roles to install the nginx web server on our webserver role. In order to do that, we will need to install the role from Ansible Galaxy. First, ensure that Ansible is installed on your system by running the following command:

admin@myhome:~$ ansible-galaxy install nginxinc.nginx

This command will download and install the nginx role from Ansible Galaxy. By default, all roles installed are placed in the ~/.ansible/roles directory. It’s possible to change that by creating a global Ansible configuration file in your home directory: ~/.ansible.cfg.

An example of a configuration file changing the roles_path directory looks like this:

[defaults]
roles_path = /home/admin/myansibleroles

A good practice is to pin role version numbers and put this version in a YAML file saved in the same Git repository where you will keep your Ansible playbooks. To achieve this, let’s create an ansible_requirements.yml file:

---
- src: nginxinc.nginx
  version: 0.24.0

To install the role from Ansible Galaxy using that file, you would run the following command:

admin@myhome:~$ ansible-galaxy install -r ansible_requirements.yml

Once the role is installed, it can be used in an Ansible playbook by adding the following line to the playbook:

roles:
    - nginxinc.nginx

Here’s an example playbook that uses the nginx role from Ansible Galaxy to install and configure nginx on a remote server:

---
- name: Install and configure Nginx
  hosts: webservers
  become: true
  roles:
    - nginxinc.nginx
  vars:
    nginx_sites:
      myapp:
        template: "{{ playbook_dir }}/templates/myapp.conf.j2"

In this playbook, we specify the webservers group as the target hosts and use the nginxinc.nginx role to install and configure nginx. We also define a variable called nginx_sites that specifies the configuration for a nginx server block that will be created using a Jinja2 template located in the playbook’s templates directory.

By using Ansible Galaxy and pre-built roles such as nginxinc.nginx, users can automate complex tasks quickly and reliably, ensuring consistency and reducing the risk of errors.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime