Tuesday , 16 July 2019
Breaking News

Ansible Install/configure on RHEL

Install and configure Ansible on RHEL

  • Establish Prerequisites
  • Install Ansible
  • Discuss Ansible layout
  • Create a basic configuration

Installing Ansible

Ansible only needs to be installed on the control machine, or the machine from which you will be running commands. This will likely be your laptop or other computer from which you frequently access your server, or it may be a centralized server in more complicated setups.

 1. install Ansible by running yum -y install ansible on the command line. This will install a bunch of python dependencies during the process.

 2.Confirm that Ansible is installed and ready to go by running ansible –version

Screen Shot 2016-08-16 at 2.06.27 PM

Ansible Layout

Before we create a basic configuration, Lets take a look at Ansible file/folder structure. if you list the files/folders in /etc/ansible that you’re presented with the following. Alongside,please refer an explanation for each file or folder.

/etc/ansible The main configuration folder which encompasses all Ansible config
/etc/ansible/hosts This file holds information for the hosts/and host groups you will configure
/etc/ansible/ansible.cfg The main configuration file for Ansible
/etc/ansible/roles This folder allows you to create folders for each server role, web/app/db, etc.

It is advisable to use a proper role structure in Ansible, as this makes it easier to manage your playbooks.

What are playbooks? Playbooks are what Ansible uses to describe a policy you would like managed systems to enforce, whether it be installing a piece of software or setting DNS servers in resolv.conf.

A playbook will generally consist of the following elements:

  • Tasks — This can be used to include smaller files or provide further instructions.
  • Handlers — This can be used to do things like restart a service or carry out other tasks.
  • Templates — You can vary certain things in this to produce dynamic configuration files.
  • Files — This one is simple. It’s a static file which probably doesn’t need to be different across servers.

Create a basic configuration

Ansible is a really powerful orchestration tool, and as such, the list of things you can achieve by using it is endless. Before creating an example playbook, I’d like to demonstrate some things you can do with Ansible on one line. You can do cool things like run a command remotely using ansible <host> -m command -a “command_to_run”.

Screen Shot 2016-08-16 at 2.06.21 PM

You could easily scale this out to run this command against an entire group of hosts and return their kernel version in just a few seconds or minutes (depending on your infrastructure, of course!).

Now let’s create a basic playbook to deploy Apache to our local Ansible server. Obviously, you’d be looking at running Ansible against remote hosts such as web servers and database servers, etc., but this will show us how Ansible works. if you’re following this guide, but planning to run Ansible against another server, then it’s recommended that you configure a keypair on your Ansible server by running ssh-keygen -t rsa and then exporting the public key (id_rsa.pub) to the authorized_keys file for the user (usually root) on the server you’re wanting to run commands against. This will allow you to seamlessly execute playbooks against your environment without requiring a password to be entered each time.

A playbook is a YAML file, and typically follows this structure:

Sample Playbook YAML file
- hosts: [target hosts]
  remote_user: [yourname]
    - [task 1]
    - [task 2] 

For example, the following playbook would log in to all servers in the marketingservers group and ensure Apache was started.

Sample service check playbook

- hosts: [marketingservers]
  remote_user: webadmin
    - name: Ensure the Apache daemon has started
      service: name=httpd state=started
      become: yes
      become_method: sudo

Let’s create a new file with the .yml extension. In my case, I’m creating ‘test-playbook.yml’ to house our instructions. In it, I’ve put the following:

- hosts: localhost
    - name: install httpd
      yum: name=httpd update_cache=yes state=latest

You need to make sure that you follow standard YAML syntax guidelines when manipulating Ansible configuration files, otherwise you are likely to experience syntax errors. A link to YAML Syntax on Ansible’s website is included in the resources section of this article.

Now that we have a playbook to use, we simply use ansible-playbook test-playbook.yml to run the playbook against our server. If everything goes according to plan, you should see the following:

Screen Shot 2016-08-16 at 2.06.15 PM

Useful link  – https://deliciousbrains.com/automating-server-setup-ansible/

Check Also

Red Hat Enterprise Linux 7 Hardening Checklist

Red Hat Enterprise Linux 7 Hardening Checklist  Preparation and Physical Security 1 If machine is …

Leave a Reply