Some Basic Ansible Playbooks

As a system administrator, you will need to perform a lot of tasks on a regular basis. Instead of doing these tasks manually through the command line, you can use Ansible & Ansible Playbooks to automate your tasks which would make your work easier as well as efficient. Of course, you will first need to install and connect Ansible to the machines you need to manage but once you do that, these playbooks will make your work very easy. These are some basic ansible playbooks to help you get started!

Configuring yum repo on client side

One of the most basic tasks, you will need to is to know how to configure a basic yum repository on a remote machine to get the required packages. Below is a screenshot for creating a yum repository.

network nuts rhce

You do not need to write the ‘.repo’ extension with the file name since the module yum_repository will do it automatically for you. The rest of it is quite simple if you have done RHCSA Training. Here is the code for configuring yum repo on client side:

- name: configure a sample yum repo
    - name: install example repo
        file: example
        name: example-internal
        description: networknuts yum repo
        enabled: yes
        gpgcheck: no

Update all the packages on a remote machine

For updating all the packages on a remote machine, you’d generally give the command yum update but when you wish to do it through Ansible, you can simple use an asterisk (works as a wildcard) which will automatically update all the packages to the latest version.

network nuts rhce

Here is the code for the updating all packages on a remote machine:

- name: update all packages
  hosts: all
          - name: update all pkgs
                    name: '*'
                    state: latest

Create a group and then put the user in that group

This is a very simply playbook which will really help you as a system administrator since user management is one of the most recurring tasks in your job profile. This playbook will create a group and then a user which will also be made a part of said group.

network nuts rhce

As you can see, we have also given the user a custom UID and comment. This of course is optional. Here is the code for creating a group, a user and then putting the user into that group:

- hosts: all
    - name: create a group
        name: mygroup
        state: present
    - name: create a user in the group
        name: aryan123
        comment: "Aryan Srivastava"
        uid: 1998
        group: mygroup

Creating a sample Apache Server

A simple httpd server is probably the most crucial part of RHCE Training as well as of a system administrator’s job. It is very easy and can really help you a lot in the long run. This playbook is a little long but if you go through it, it is actually quite simple to understand.

network nuts rhce

The first step of course is to install httpd which is the package name of Apache, after this we create the index.html file in the location /home/ Once we do this, we need to allow certain ports into our firewall service. The last step is to restart httpd or Apache service for the changes to take affect. Here is the code for the sample Apache server:

- name: configuring an apache server
  hosts: all
          - name: installing httpd package
                    name: httpd
                    state: present

          - name: configuring index.html file
                    content: " Input content here "
                    dest: /home/

          - name: adding http in firewalld
                    service: http
                    permanent: true
                    enabled: true

          - name: adding https in firewalld
                    service: https
                    permanent: true
                    enabled: true

          - name: restart httpd
                    name: httpd
                    state: restart

These are the some of the most essential playbooks to use with Ansible in a Linux environment. You can read my previous entry for essential playbooks here.

More Knowledge

If you are completely new to Linux, I would suggest that you go through the basic Linux program also known as RHCSA: Linux Training
If you are an expert with Linux, grow your career with RHCEAWSDevopsOpenstack or Openshift.