Leanpub Header

Skip to main content

Ansible by Examples

200+ Automation Examples For Linux and Windows System Administrators and DevOps

Looking to learn Ansible Playbook code from scratch or update an existing project? Automate your System Administrator's day-to-day tasks in Linux and Windows with the Ansible Open Source IT automation tool. Learn how to use 200+ Examples to provision and manage your servers easily.

The author is letting you choose the price you pay for this book!

Pick Your Price...
PDF
EPUB
WEB
About

About

About the Book

Ansible is an open-source IT automation tool. This book includes both the obvious and the less obvious best practices for Ansible automation. Nowadays, every successful IT department relies on automation for bare metal servers, virtual machines, containers, and edge computing. Automate your IT journey with Ansible technology.

You will begin by installing Ansible on Enterprise Linux, Community Linux, Windows, and MacOS using the most common package managers and archives.

Each of the 200+ lessons summarizes a module, covering everything from the most important parameters to Ansible code and real-life applications. Each code has been battle-tested in real-world scenarios. Mundane tasks such as creating a text file, extracting and archiving data, and fetching repositories using HTTPS or SSH connections can be automated with just a few lines of code. These examples are just a small portion of the extensive list included in the course.

Some Ansible scripts are applicable across all Linux systems, while others are specific to RedHat, Debian, and Windows systems.

The 20+ troubleshooting lessons will teach you how to read error messages, reproduce errors, troubleshoot issues, and resolve them.

Are you ready to automate your day with Ansible?

The examples in the book have been tested with the latest versions of Ansible 2.9+ and Ansible Core 2.11+.

Price

Pick Your Price...

Minimum price

$9.99

$15.99

You pay

$15.99

Author earns

$12.79
$

All prices are in US $. You can pay in US $ or in your local currency when you check out.

EU customers: prices exclude VAT, which is added during checkout.

...Or Buy With Credits!

Number of credits (Minimum 1)

1
The author will earn $12.00 from your purchase!
You can get credits monthly with a Reader Membership

Author

About the Author

Luca Berton

Luca Berton is an Ansible Automation Expert who has been working with JPMorgan Chase & Co. and previously worked with the Red Hat Hat Ansible Engineer Team for three years. Published author of the Ansible for VMware by Examples and Ansible for Kubernetes by Examples best-seller of the Ansible By Example(s) practical book series and creator of the Ansible Pilot project. With more than 15 years of experience as a System Administrator, he has strong expertise in Infrastructure Hardening and Automation. Enthusiast of the Open Source supports the community, sharing his knowledge in different events of public access. Geek by nature, Linux by choice, Fedora, of course.

Leanpub Podcast

Episode 280

An Interview with Luca Berton

Contents

Table of Contents

Introduction

  1. Modern IT Infrastructure
  2. Whois Luca Berton

Ansible For Beginners With Examples

  1. What is Ansible
  2. Getting Started
  3. Inventory
  4. Playbook
  5. Variables
  6. Facts and Magic Variables
  7. Vault
  8. Conditional
  9. Loop
  10. Handler
  11. Role
  12. Ansible Best Practices

Install Ansible

  1. Ansible terminology - ansible vs ansible-core packages
  2. Installing Ansible in Red Hat Enterprise Linux (RHEL) 8 with Ansible Engine
  3. Installing Ansible in Ubuntu LTS: Universe and PPA Repositories Guide
  4. Install Ansible in Fedora
  5. Installing Ansible on CentOS 9 Stream
  6. Install Ansible in Windows WSL Windows Subsystem for Linux
  7. Installing Ansible on macOS Using Homebrew
  8. Installing Ansible on SUSE Linux Enterprise Server (SLES)
  9. Installing Ansible with PIP
  10. Installing Ansible Core on Red Hat Enterprise Linux (RHEL) 9
  11. Installing Ansible in Amazon Linux
  12. Install Ansible in AlmaLinux 9

Ansible For Linux

  1. Ansible modules - command vs shell
  2. Test host availability - Ansible module ping
  3. How to print a text or a variable during the execution with Ansible
  4. Edit single-line text - Ansible module lineinfile
  5. Edit multi-line text - Ansible module blockinfile
  6. Read a file into a variable on host - Ansible lookup plugin file
  7. Reboot remote hosts - Ansible module reboot
  8. Checkout git repository via HTTPS - Ansible module git
  9. Checkout git repository via SSH - Ansible module git
  10. Copy files to remote hosts - Local to Remote - Ansible module copy
  11. Copy files from remote hosts - Remote to Local - Ansible module fetch
  12. Start and enable services on boot on Linux remote hosts - Ansible module service_facts, service
  13. Restart services on remote hosts - Ansible module service
  14. Stop and disable services on boot on remote hosts - Ansible module service_facts, service
  15. Apply a file template - Ansible module template - HTML placeholder
  16. Loop in file template - Ansible module template - Generate hosts file
  17. Schedule a Cron Job task in Linux - Ansible module cron
  18. Read a file from remote hosts - Ansible module slurp
  19. Read an environment variable - Ansible lookup plugin env”
  20. Permanently Set Remote System Wide Environment Variables on Linux - /etc/environment - Ansible module lineinfile
  21. Write a Variable to a File - Ansible module copy vs template

Ansible Code reuse: Roles and Collections with Ansible Galaxy

  1. Download and Use Ansible Galaxy Role - ansible-galaxy and requirements.yml
  2. Download and Use Ansible Galaxy Collection - ansible-galaxy and requirements.yml

Ansible Tip & Tricks

  1. Pause execution - Ansible module pause
  2. How to Pass Variables to Ansible Playbook in command line? - Ansible extra variables
  3. Break a string over multiple lines - Ansible Literal and Folded Block Scalar operators
  4. Ansible terminology - ansible_hostname vs inventory_hostname vs ansible_fqdn
  5. Set remote environment per task or play - Ansible environment statement
  6. Execute command on the Ansible host - Ansible localhost
  7. Three options to Safely Limit Ansible Playbooks Execution to a Single Machine
  8. Ansible modules - command vs shell
  9. Write a Variable to a File - Ansible module copy vs template
  10. Filter A List By Its Attributes - Ansible selectattr filter
  11. Using Date, Time and Timestamp in Ansible Playbook - Ansible Tip and Tricks
  12. Using Date, Time and Timestamp without Facts in Ansible Playbook - Ansible date and lookup plugin
  13. Ansible Magic Variables - Ansible Tip and Tricks
  14. Ansible Playbook Dry Run - check and diff mode
  15. Run a Python Script on Remote Machines - Ansible module script
  16. The run_once statement in Ansible

Ansible for Linux Filesystem

  1. Create an empty file - Ansible module file
  2. Create a text file - Ansible module copy
  3. Check if a file exists - Ansible module stat
  4. How to create a directory with Ansible?
  5. How to check if a directory exists in Ansible?
  6. How to rename a file or directory using an Ansible task on a remote system?
  7. Change file permission - Ansible module file
  8. Add Execute Permission 755 Linux file - Ansible module file
  9. Delete file or directory - Ansible module file
  10. Download a file - Ansible module get_url
  11. Extract an archive - Ansible module unarchive
  12. Create a symbolic link (also symlink or soft link) in Linux - Ansible module file
  13. Create a hard link in Linux - Ansible module file
  14. Mount a Windows share in Linux SMB/CIFS - Ansible module mount
  15. Mount an NFS share in Linux - Ansible module mount
  16. Concatenate multiple files in a specific order - Ansible module template and YAML
  17. Backup With Rsync - Local to Remote - Ansible module synchronize
  18. How to Delete Only Files Inside a Directory - Ansible module find
  19. Find All Files with Extension - Ansible module find
  20. Search for a String in a File -Ansible module lineinfile
  21. Links
  22. Create ISO image from Files and Folders - Ansible module iso_create

Ansible For Linux User Management

  1. Ansible create a user account
  2. Ansible remove user account
  3. Ansible change user password
  4. Ansible disable user account
  5. Ansible enable user account
  6. Ansible user password expiration
  7. Ansible creates a group
  8. Ansible deletes a group account
  9. Ansible changes the User Primary Group on Linux
  10. Ansible adds a user to a secondary group(s)

Ansible Playbook Code interact with Web Services API

  1. Submit a GET request to a REST API endpoint - Interact with web services - Ansible module uri
  2. Token-Based Authentication in REST API - Interact with web-service - Ansible module uri - Authentication request using the REST API token

Ansible For Linux Security

  1. Set sysctl kernel parameters - Ansible module sysctl
  2. Load and Unload Kernel Modules in Linux - Ansible module modprobe
  3. Set the SELinux Policy States and Modes on Linux - Ansible module selinux
  4. Configure Kernel Parameters in RedHat-like Linux systems - Ansible system role
  5. Enable or Disable SELinux Boolean on Linux - Ansible module seboolean
  6. Enable or Disable Permissive Domain in SELinux policy on Linux - Ansible module selinux_permissive
  7. Vulnerability Scanner/Detector Log4Shell Remote Code Execution Log4j (CVE-2021–44228) — Ansible log4j-cve-2021–44228

Ansible Playbook Code for RedHat-like systems

  1. Register a system with Red Hat Subscription-Manager - Ansible module redhat_subscription
  2. Install a package in RedHat like systems - Ansible module yum
  3. Rolling Update RedHat like systems - Ansible module yum
  4. Open firewall ports in RedHat like systems - Ansible module firewalld
  5. Install Google Chrome in RedHat-like systems - Ansible module rpm_key, yum_repos
  6. Install Microsoft Edge in RedHat-like systems - Ansible module rpm_key, yum_repository and yum
  7. NFS Server - Export an NFS Share in RedHat-like systems: RHEL, CentOS, CentOS Stream, Fedora - Ansible modules yum, file, lineinfile, command, firewalld, service
  8. Deploy a web server apache httpd on RedHat-like systems - Ansible modules yum, copy, service firewalld
  9. Deploy a proxy server squid on RedHat-like systems - Ansible modules yum, template, service and firewalld
  10. Deploy a web server apache httpd virtualhost on RedHat-like systems - Ansible modules yum, file, copy, template, service and firewalld

Ansible Playbook Code for Debian-like systems

  1. Install a package in Debian like systems - Ansible module apt
  2. Rolling Update Debian-like systems - Ansible module apt
  3. Open firewall ports in Debian like systems - Ansible module ufw
  4. Install Google Chrome in Debian-like systems - Ansible module apt_key, apt_repos
  5. Install Microsoft Edge in Debian-like systems - Ansible module apt_key, apt_repository and apt
  6. Deploy a web server apache httpd on Debian-like systems - Ansible modules apt, copy, service and ufw
  7. Deploy a web server apache httpd virtual host on Debian-like systems - Ansible modules apt, file, copy, template, command, ufw and service

Ansible Playbook Code for Suse-like systems

  1. Install a package in Suse-like systems - Ansible module zypper
  2. Install Google Chrome in Suse-like systems - Ansible module rpm_key, zypper_repo

Ansible Playbook Code for Windows

  1. Configure a Windows Host for Ansible - ansible winrm
  2. Test Windows host availability - Ansible module win_ping
  3. Create an empty file in Windows-like systems - Ansible module win_file
  4. Check if a file exists on Windows-like systems - Ansible module win_stat
  5. Install Windows software - Ansible module win_chocolatey
  6. Copy files to Windows remote hosts - Local to Remote - Ansible module win_copy
  7. Copy files from Windows remote hosts - Ansible module fetch
  8. Reboot Windows hosts - Ansible module win_reboot
  9. Install Google Chrome in Windows-like systems - Ansible module win_chocolatey
  10. Extract an archive in Windows-like systems - Ansible module win_unzip
  11. Download a file in Windows-like systems - Ansible module win_get_url
  12. Create a directory on Windows-like systems - Ansible module win_file
  13. Check if a directory exists on Windows-like systems - Ansible module win_stat
  14. Check Registry .NET Framework version on Windows-like systems - Ansible module win_reg_stat
  15. Rolling Update Windows-like systems - Ansible module win_updates
  16. Add Windows Registry on Windows-like systems - Ansible module win_regedit
  17. Remove Windows Registry path or key on Windows-like systems - Ansible module win_regedit
  18. Create a local group on Windows-like systems - Ansible module win_group
  19. Remove a local group on Windows-like systems - Ansible module win_group
  20. Create a local user on Windows-like systems - Ansible module win_user
  21. Change local user password on Windows-like systems - Ansible module win_user
  22. Backup With Robocopy on Windows - Ansible module win_robocopy
  23. Ansible modules - win_command vs win_shell

Ansible For Containers

  1. Ansible install Docker in Debian-like systems
  2. Ansible install Docker in RedHat-like systems
  3. Install Docker in Windows-like systems - Ansible module win_chocolatey
  4. Install Zoom flatpak in Debian-like systems - Ansible module flatpak
  5. Install Zoom flatpak in RedHat-like systems - Ansible module flatpak
  6. Update Zoom flatpak(s) in Linux systems - Ansible module command
  7. Install Spotify snap in Debian-like systems - Ansible module snap
  8. Install Spotify snap in RedHat-like systems - Ansible module snap
  9. Deploy Apache Web Server in a Docker Container for Debian-like systems - Ansible modules docker_image and docker_container
  10. Deploy Apache Web Server in a Podman Container for RedHat-like systems - Ansible modules podman_image and podman_container

Ansible For Kubernetes

  1. Install Red Hat CodeReady Containers to run OpenShift 4 in macOS
  2. Create Kubernetes K8s or OpenShift OCP namespace project - Ansible module k8s
  3. Create Kubernetes K8s or OpenShift OCP Pod - nginx - Ansible module k8s
  4. Create Kubernetes K8s or OpenShift OCP Secret - Ansible module k8s
  5. Create Kubernetes K8s or OpenShift OCP service - Ansible module k8s
  6. Assign CPU Resources to Kubernetes K8s or OpenShift OCP Containers and Pods — Ansible module k8s
  7. Assign Memory Resources to Kubernetes K8s or OpenShift OCP Containers and Pods - Ansible module k8s
  8. Configure a Pod to Use a Volume for Storage - Ansible module k8s
  9. Apply Multiple Yaml Files at Once on Kubernetes K8s or OpenShift OCP - Ansible module k8s and lookup plugin fileglob

Ansible for Network Automation

  1. Backup Config on Dell EMC Networking Operating System DNOS 6 - Ansible Network dellemc.os6
  2. Backup Config on Dell EMC Networking Operating System DNOS 10 - Ansible Network dellemc.os10
  3. Backup Config on Mikrotik RouterOS - Ansible Network community.routeros

Ansible For VMware

  1. Configure Ansible for VMware - ansible collection community.vmware
  2. Ansible troubleshooting - VMware Failed to Import PyVmomi
  3. Ansible troubleshooting - VMware Unknown error while connecting to vCenter or ESXi
  4. Ansible troubleshooting - VMware certificate verify failed connecting to vCenter or ESXi
  5. Create a VMware vSphere Virtual Machine - Ansible module vmware_guest
  6. Deploy a VMware vSphere Virtual Machine from a Template - Ansible module vmware_guest
  7. Start a VMware vSphere Virtual Machine - Ansible module vmware_guest_powerstate
  8. Stop a VMware vSphere Virtual Machine - Ansible module vmware_guest_powerstate
  9. Take a VMware Virtual Machine Snapshot - Ansible module vmware_guest_snapshot
  10. Delete a VMware Virtual Machine Snapshot - Ansible module vmware_guest_snapshot
  11. Add a New Hard Disk to VMware vSphere Virtual Machine - Ansible module vmware_guest_disk
  12. Expand a Virtual Disk in VMware vSphere Virtual Machine - Ansible module vmware_guest_disk
  13. Gather Info about all VMware ESX/ESXi Hosts in a given Cluster - Ansible module vmware_host_config_info
  14. Get VMware vSphere Virtual Machine UUID - Ansible module vmware_guest_info

Ansible For PostgreSQL

  1. Install PostgreSQL in RedHat-like systems - Ansible modules yum, stat, shell, service
  2. Install PostgreSQL in Debian-like systems - Ansible modules apt, stat, shell, service
  3. Create a PostgreSQL Database - Ansible module postgresql_db
  4. Drop a PostgreSQL Database - Ansible module postgresql_db
  5. Rename a PostgreSQL Database - Ansible module postgresql_db
  6. Create a PostgreSQL User or Role - Ansible module postgresql_user
  7. Allow md5 Connection for a PostgreSQL User / Role - Ansible module postgresql_pg_hba
  8. Grant Privileges to User/Role on PostgreSQL Database - Ansible module postgresql_privs
  9. Backup a PostgreSQL Database - Ansible module postgresql_db
  10. Restore a PostgreSQL Database — Ansible module postgresql_db
  11. Run a SQL Command/Query on PostgreSQL - Ansible module postgresql_query

Ansible Troubleshooting The Most Common Errors

  1. Ansible troubleshooting - connection failed
  2. Ansible troubleshooting - macOS fork error
  3. Ansible troubleshooting - indentation error
  4. Ansible troubleshooting - syntax error
  5. Ansible troubleshooting - undefined variable
  6. Ansible troubleshooting - invalid argument
  7. Ansible troubleshooting - privilege escalation error
  8. Ansible troubleshooting - missing sudo password and incorrect sudo password
  9. Ansible troubleshooting - missing module parameter
  10. Ansible troubleshooting - failure downloading
  11. Ansible troubleshooting - chgrp failed
  12. Ansible troubleshooting - not a valid attribute for a Play error
  13. Ansible troubleshooting - fatal template error while templating string
  14. Ansible troubleshooting - PowerShell incompatible with the sudo become plugin
  15. Ansible troubleshooting - passwordless account
  16. Ansible troubleshooting - user module password_expiry_min bug
  17. Ansible troubleshooting - Windows 10 Error 0x80370102 WSL: Windows Subsystem for Linux
  18. Ansible troubleshooting - Windows 11 Error 0x80370102 WSL: Windows Subsystem for Linux
  19. Ansible troubleshooting - urlopen error
  20. Ansible troubleshooting - destination does not exist
  21. Ansible troubleshooting - Destination does not exist rc 257
  22. Ansible troubleshooting - “role not found” error
  23. Ansible troubleshooting - module failure on Windows-target
  24. Ansible troubleshooting - permission denied Errno 13
  25. Ansible troubleshooting - VARIABLE IS NOT DEFINED! ansible_hostname
  26. Ansible troubleshooting - Unhandled exception while executing module win_user
  27. Ansible troubleshooting - This command has to be run under the root user
  28. Ansible troubleshooting - Kubernetes K8s or OpenShift OCP 401 Unauthorized
  29. Common Interview Questions, Answers and Troubleshooting Tips
  30. Ansible Linter
  31. Rule Analysis and Best Practices
  32. Ansible-Lint Profiles
  33. Ansible Best Practices: ignore_errors in Ansible Playbooks

Ansible Troubleshooting Cookbook

  1. Problem: Error 102 - No Jinja2 in When Conditions
  2. Problem: Error 104 - Deprecated Bare Vars
  3. Problem: Error 105 - Deprecated Module Usage
  4. Problem: Error 106 - Role Name Rules
  5. Problem: Error 202 - Risky Octal Permissions
  6. Problem: Error 203 - No Tabs
  7. Problem: Error 205 - Playbook Extension
  8. Problem: Error 206 - Jinja Spacing
  9. Problem: Error 207 - Jinja Invalid
  10. Problem: Error 208 - Risky File Permissions
  11. Problem: Error 301 - No Changed When
  12. Problem: Error 302 - Deprecated Command Syntax
  13. Problem: Error 303 - Command Instead of Module
  14. Problem: Error 304 - Inline Environment Variable
  15. Problem: Error 305 - Command Instead of Shell
  16. Problem: Error 306 - Risky Shell Pipe
  17. Problem: Error 401 - Latest Git
  18. internal-error
  19. avoid-implicit
  20. args
  21. loop-var-prefix
  22. FQCN
  23. Error key-order
  24. Error load-failure
  25. Error 102: No Jinja2 in ‘when’ Conditions
  26. Error 104: Deprecated Bare Vars
  27. Error 106: Role Name Rules
  28. Error 202: Risky Octal Permissions
  29. Error 203: No Tabs
  30. Error 205: playbook-extension
  31. Error: name[play]
  32. Error: name[template]
  33. Error 502: name[missing]
  34. Error: name[casing]
  35. Error 206 Jinja Spacing
  36. Error 207: Jinja Invalid
  37. Error: no-jinja-when
  38. Error 208: risky-file-permissions
  39. Error 301: no-changed-when
  40. Error 302: deprecated-command-syntax
  41. Error 303: command-instead-of-module
  42. Error 304: inline-env-var
  43. Error 305: command-instead-of-shell
  44. Error 306: risky-shell-pipe
  45. Error 401: latest[git]
  46. Error 402: latest[hg]
  47. Error 403: package-latest
  48. Error 404: no-relative-paths
  49. Error 501: partial-become
  50. Error 503: no-handler
  51. Error 504: deprecated-local-action
  52. Error 505: missing-import
  53. Error 601: literal-compare
  54. Error 602: empty-string-compare
  55. Error 911: syntax-check
  56. Error no-free-form
  57. The Importance of Full Syntax
  58. Error: name[prefix]
  59. Error no-prompting
  60. Error no-same-owner
  61. Error only-builtins
  62. Error parser-error
  63. Error run-once
  64. Error sanity
  65. Error use-loop
  66. Ansible troubleshooting - Error galaxy
  67. Error 702: meta-no-tags
  68. Error 703: meta-incorrect
  69. Error 704: meta-video-links
  70. Error meta-runtime

Thank you

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $14 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub