Ansible Automation

From PheonixSolutions
Jump to navigation Jump to search

Install Ansible in Ubuntu

sudo apt install ansible

Setup Ansible in the Server

Server : 3.7.109.92 => PROD_CRON_01

Folder Path : /opt/airmeet/

Ansible Config path: /etc/ansible/

Ansible config File : /etc/ansible/ansible.cfg

vi /etc/ansible/ansible.cfg

Enable the following

inventory = /opt/airmeet/inventory

host_key_checking = False

remote_user = ubuntu

private_key_file = /opt/airmeet/airmeet.pem

Save and Close the file

Install NewRelic Agent

  1. 1 Open a new file with newrelic-agent-install.yml

Add the below contents

vi newrelic-agent-install.yml

---

- hosts: all

become: true

vars:

license_key: a9f860b86e4d28327c9d74934bf9fc6175b7NRAL

tasks:

- name: import the newrelic apt key

apt_key:

url: https://download.newrelic.com/infrastructure_agent/gpg/newrelic-infra.gpg

state: present

- name: install newrelic deb repository

apt_repository:

repo: "deb [arch=amd64] http://download.newrelic.com/infrastructure_agent/linux/apt bionic main"

state: present

update_cache: yes

- name: Update apt-get repo and cache

apt: update_cache=yes force_apt_get=yes cache_valid_time=3600

- name: ensure newrelic agent is installed

apt:

name: newrelic-infra

state: present

update_cache: yes

- name: Copy file with owner and permissions

copy:

src: /opt/airmeet/newrelic-infra.yml

dest: /etc/newrelic-infra.yml

owner: root

group: root

mode: '0644'

- name: Start newrelic-infra agent service

systemd:

name: newrelic-infra

state: started

- name: Enable newrelic-infra service on Boot

systemd:

name: newrelic-infra

enabled: yes

Save and Close it.

  1. 2 Now add a new file newrelic-infra.yml with its license key

vi newrelic-infra.yml

license_key: a9f860b86e4d28327c9d74934bf9fc6175b7NRAL

Save and Close it.

  1. 3 Add inventory file

vi inventory

    1. my vms/servers hosted by AWS (EC2) ##

[awshosts]

13.235.16.139

Save and Close it

  1. 4 Run Playbook now

$ sudo ansible-playbook newrelic-agent-install.yml

Output will be:-

ubuntu@ip-172-31-33-203:/opt/airmeet$ sudo ansible-playbook newrelic-agent-install.yml

PLAY [all] *************************************************************************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************************************************

ok: [13.235.16.139]

TASK [import the newrelic apt key] *************************************************************************************************************************************************************************

ok: [13.235.16.139]

TASK [install newrelic deb repository] *********************************************************************************************************************************************************************

changed: [13.235.16.139]

TASK [Update apt-get repo and cache] ***********************************************************************************************************************************************************************

ok: [13.235.16.139]

TASK [ensure newrelic agent is installed] ******************************************************************************************************************************************************************

changed: [13.235.16.139]

TASK [Copy file with owner and permissions] ****************************************************************************************************************************************************************

changed: [13.235.16.139]

TASK [Start newrelic-infra agent service] ******************************************************************************************************************************************************************

ok: [13.235.16.139]

TASK [Enable newrelic-infra service on Boot] ***************************************************************************************************************************************************************

ok: [13.235.16.139]

PLAY RECAP *************************************************************************************************************************************************************************************************

13.235.16.139 : ok=8 changed=3 unreachable=0 failed=0

Thats All.

Install FileBeat Agent

  1. 1 Add filebeat playbook

vi filebeat-install.yml

---

- hosts: all

become: true

tasks:

- name: Import the Elasticsearch apt key

apt_key:

url: https://artifacts.elastic.co/GPG-KEY-elasticsearch

state: present

- name: Install Elastic deb repository

apt_repository:

repo: "deb https://artifacts.elastic.co/packages/7.x/apt stable main"

state: present

update_cache: yes

- name: Update apt-get repo and cache

apt: update_cache=yes force_apt_get=yes cache_valid_time=3600

- name: Instal apt-transport-https

apt:

name: apt-transport-https

state: present

update_cache: yes

- name: Ensure FileBeat agent is installed

apt:

name: filebeat

state: present

update_cache: yes

- name: Copy filebeat.yml file

copy:

src: /opt/airmeet/filebeat.yml

dest: /etc/filebeat/filebeat.yml

owner: root

group: root

mode: '0600'

- name: Start FileBeat Agent

systemd:

name: filebeat

state: started

- name: Enable FileBeat Agent on Boot

systemd:

name: filebeat

enabled: yes

Save and Close it

  1. 2 Place a filebeat.yml configuration file here. This can be copied from any prod-app-be0x server to here.
  1. 3 Run Playbook

Output will be:

sudo ansible-playbook filebeat-install.yml

PLAY [all] *************************************************************************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************************************************

ok: [13.235.16.139]

TASK [Import the Elasticsearch apt key] ********************************************************************************************************************************************************************

ok: [13.235.16.139]

TASK [Install Elastic deb repository] **********************************************************************************************************************************************************************

changed: [13.235.16.139]

TASK [Update apt-get repo and cache] ***********************************************************************************************************************************************************************

ok: [13.235.16.139]

TASK [Instal apt-transport-https] **************************************************************************************************************************************************************************

changed: [13.235.16.139]

TASK [Ensure FileBeat agent is installed] ******************************************************************************************************************************************************************

changed: [13.235.16.139]

TASK [Copy filebeat.yml file] ******************************************************************************************************************************************************************************

changed: [13.235.16.139]

TASK [Start FileBeat Agent] ********************************************************************************************************************************************************************************

changed: [13.235.16.139]

TASK [Enable FileBeat Agent on Boot] ***********************************************************************************************************************************************************************

changed: [13.235.16.139]

PLAY RECAP *************************************************************************************************************************************************************************************************

13.235.16.139 : ok=9 changed=6 unreachable=0 failed=0

Thats all.