Basic Ansible Playbooks needed by any System Administrator in their day to day tasks.

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.

Installing a package

One of the most required and basic tasks, a system administrator should be aware of how to install packages. Instead of manually connecting to remote machines and installing the package on them, you can do so by simply running this playbook.

Installing a package

In the above screenshot, you can see that we are using the dnf module since it has replaced yum in RHEL 8. It is also recommended that you use the dnf command instead of the yum command in your RHCSA & RHCE Training. Simply use the same playbook, just replace name: httpd with the name of your desired package. Here is also the code for this playbook so you can quickly copy paste it:

--- - name: install a particular package hosts: all tasks: - name: install apache dnf: name: httpd state: present

Restart a service

Restarting a service is important to make your desired changes visible. For example, you will have to restart httpd once you make some changes in the /var/www/html/ directory. You might also need to start or stop a service on multiple machines all together. This you can do use the service module in Ansible which will start, restart or stop a service on multiple machines simultaneously.

Restarting a service

In the above screenshot, you can see that we have simply given the service module two parameters, the name of our service and the state we want it in. If you want to start or stop the service, you can use the started or stopped keyword respectively to tell Ansible to do so. Yes, it uses past tense. Here is the code for this playbook:

--- - name: service module hosts: all tasks: - name: restart httpd service: name: httpd state: restarted

Replace a particular line in a file

Imagine you have multiple machines which have a particular file on which you need to replace a particular line, sounds like a nightmare doesn’t it? You can use the lineinfile module as given below.

Replacing a line in file

Just change the path to the file location, regexp will contain the new line and line will contain the line that needs to be replaced. Run this playbook and voila! Your work is done. Here is the code for this playbook:

--- - name: lineinfile module hosts: all tasks: - name: using lineinfile module lineinfile: path: /tmp/myfile2 regexp: 'rhce training' line: rh294 training state: absent

Creating a user with a password

Creating a user is probably the most boring task a system administrator has to do, nevertheless doing it on multiple machines would be such a pain. If you use the playbook, you can do the same with a single click.

ust replace name: enterusename with the required username and mypassword with the password that you want to set. RHEL 8 uses SHA512 as the algorithm to encrypt passwords for users so we will use the same. Here is the code for this playbook:

--- - name: user creation hosts: all tasks: - name: creating a user with a defined password user: name: enterusername password: "{{ mypassword | password_hash('sha512') }}" state: present

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 RHCE, AWS, Devops, Openstack or Openshift.