Ansible Playbooks for Linux Administrator BAU Activities

Ansible is the Simple, Powerful and Agentless automation tool. It is push based configuration tool.

Thru SSH we can connect to other servers

Features:
–> Provisioning
–> Configuration Management
–> Continuous Delivery
–> Application Deployment
–> Security Compliance

Ansible is used to deploy application onto multiple servers at a time.

Below image refers the difference between Script and Ansible Playbook

How to install Ansible on control node ?

Control node requirements

For your control node (the machine that runs Ansible), you can use nearly any UNIX-like machine with Python installed. This includes Red Hat, Debian, Ubuntu, macOS, BSDs, and Windows under a Windows Subsystem for Linux (WSL) distribution. Windows without WSL is not natively supported as a control node; see Matt Davisโ€™ blog post for more information.

Managed node requirements

The managed node (the machine that Ansible is managing) does not require Ansible to be installed, but requires Python to run Ansible-generated Python code. The managed node also needs a user account that can connect through SSH to the node with an interactive POSIX shell.

Note

There can be exceptions in module requirements. For example, network modules do not require Python on the managed device. See documentation for the modules you use.

Node requirement summary

You can find details about control and managed node requirements, including Python versions, for each Ansible version in the ansible-core control node Python support and ansible-core support matrix sections.

Selecting an Ansible package and version to install

Ansibleโ€™s community packages are distributed in two ways:

  • ansible-core: a minimalist language and runtime package containing a set of built-in modules and plugins.
  • ansible: a much larger โ€œbatteries includedโ€ package, which adds a community-curated selection of Ansible Collections for automating a wide variety of devices.

Choose the package that fits your needs. The following instructions use ansible as a package name, but you can substitute ansible-core if you prefer to start with the minimal package and separately install only the Ansible Collections you require.

The ansible or ansible-core packages may be available in your operating systems package manager, and you are free to install these packages with your preferred method. For more information, see the Installing Ansible on specific operating systems guide. These installation instructions only cover the officially supported means of installing the python packages with pip.

Installing and upgrading Ansible with pipx

On some systems, it may not be possible to install Ansible with pip, due to decisions made by the operating system developers. In such cases, pipx is a widely available alternative.

These instructions will not go over the steps to install pipx; if those instructions are needed, please continue to the pipx installation instructions for more information.

Installing Ansible

Use pipx in your environment to install the full Ansible package:

pipx install --include-deps ansible

You can install the minimal ansible-core package:

pipx install ansible-core

Alternately, you can install a specific version of ansible-core:

pipx install ansible-core==2.12.3

Upgrading Ansible

To upgrade an existing Ansible installation to the latest released version:

pipx upgrade --include-injected ansible

Installing Extra Python Dependencies

To install additional python dependencies that may be needed, with the example of installing the argcomplete python package as described below:

pipx inject ansible argcomplete

Include the --include-apps option to make apps in the additional python dependency available on your PATH. This allows you to execute commands for those apps from the shell.

pipx inject --include-apps ansible argcomplete

If you need to install dependencies from a requirements file, for example when installing the Azure collection, you can use runpip.

pipx runpip ansible install -r ~/.ansible/collections/ansible_collections/azure/azcollection/requirements.txt

Installing and upgrading Ansible with pip

Locating Python

Locate and remember the path to the Python interpreter you wish to use to run Ansible. The following instructions refer to this Python as python3. For example, if you have determined that you want the Python at /usr/bin/python3.9 to be the one that you will install Ansible under, specify that instead of python3.

Ensuring pip is available

To verify whether pip is already installed for your preferred Python:

python3 -m pip -V

If all is well, you should see something like the following:

python3 -m pip -V
pip 21.0.1 from /usr/lib/python3.9/site-packages/pip (python 3.9)

If so, pip is available, and you can move on to the next step.

If you see an error like No module named pip, you will need to install pip under your chosen Python interpreter before proceeding. This may mean installing an additional OS package (for example, python3-pip), or installing the latest pip directly from the Python Packaging Authority by running the following:

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3 get-pip.py --user

You may need to perform some additional configuration before you are able to run Ansible.

Installing Ansible

Use pip in your selected Python environment to install the full Ansible package for the current user:

python3 -m pip install --user ansible

You can install the minimal ansible-core package for the current user:

python3 -m pip install --user ansible-core

Alternately, you can install a specific version of ansible-core:

python3 -m pip install --user ansible-core==2.12.3

Upgrading Ansible

To upgrade an existing Ansible installation in this Python environment to the latest released version, simply add --upgrade to the command above:

python3 -m pip install --upgrade --user ansible

Sample Playbook format :

How to execute / run Ansible playbook ?

# ansible-playbook <hosts file path > <name_playbook.yml>

The below Playbook is using Variables :

Ansible Conditionals :

Ansible Roles :

Below Sample Playbooks which we will use in realtime for day to day regular activities.

Server reboot :

The below Playbook will reboot the machine

OS Prechecks Playbook :

The Below Playbook will capture OS prechecks and output will be saved in the destination server

Below commands we have to save in the file called fileforscript :

uptime; cat /etc/redhat-release; uname -a;uname -r; route -n; ifconfig; ip a; free -g; lscpu; df -PTh; cat /etc/fstab; netstat -tulpn; ip route; date;lsblk; cat /etc/resolv.conf; cat /etc/hosts;date

Create User in all the hosts:

Delete user in all the hosts :

Remove / Delete the user in all the hosts at a time :

Update Password for the id SNOWDISC in all the servers :

Install and enable Crowd strike on all the servers :


Install and enable Nessus agent on all the servers :

Check installed package version and display output :

Password Max age to the ID in all servers :

Ansible Interview Questions

  1. What is Ansible ? How does it works ?
  2. What are the main components of Ansible ?
  3. Difference between Ansible and other configuration management tools ?
  4. How do you test playbooks locally before deploying ?
  5. How do you manage secrets securely in Ansible ?
  6. How do you deploy an application to multiple environments using Ansible ?
  7. How do you implement rolling updates in Ansible ?
  8. How do you execute tasks on the Ansible control nodes ?
  9. How do you handle loops in Ansible ?
  10. How do you troubleshoot ansible errors ?
  11. How do we manage dependices between roles ?
  12. How can we speedup playbook execution in Ansible ?
  13. How do you handle multiple SSH keys in Ansible ?
  14. How do you pass variables dynamically to a playbook ?
  15. How can you dynamically generate configuration files with Ansible ?
  16. How do you dynamically assign roles based on host variables ?
  17. How can we integrate Ansible with Jenkins for CI/CD pipelines ?
  18. How do we check if a file exists before executing a task ?
  19. How do we use tags to control task execution ?
  20. How can you include multiple variable files in a playbook ?
  21. How do you restart a service only when a configuration file changes ?
  22. How do you dynamically assign roles base on host variables ?
  23. How do you rollback changes in Ansible ?
  24. How do you debug and troubleshoot a failing Ansible task ?
  25. How do we handle task dependencies within Ansible Playbook ?
  26. How do you create an idempotent playbook in Ansible ?
  27. How can you use Ansible vault to encrypt sensitive information ?
  28. How do you implement a health check for a servcie in Ansible ?
  29. How do you conditionally include tasks or playbook ?
  30. How do you manage large inventories with Ansible ?

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top