wiki:Csle2022/Agenda/Ansible

Version 17 (modified by dushmantha, 18 months ago) ( diff )

--

Ansible Lab

In this lab, you will install and use Ansible to install MySQL/MariaDB and create database remotely.

Requirements:

Participants are requested to have a computer with Windows 8/10/11 (8GB RAM, 25GB free disk space) with Virtualbox (version 6 or higher) hypervisor and PuTTY installed.
In Virtualbox, 'Extension pack' should be installed.

A wired internet connection is preferred.


Virtual Machine (VM) Setup

Download VM from the following Link.

https://docs.learn.ac.lk/index.php/s/YcojJ2544b40Zw4

Import the VM to Virtualbox.

Username and Password: docker

May have to create Virtualbox Host-Only Network Adapter

File > Host Network Manager > Create

Test internet connectivity.

This setup needs 3 VMs (one as control node and the others will be managed nodes). Import the same VM image and setup 3 VMs accordingly.

Login and check IP addresses of all VMs.

ip add

If they are same, then change them accordingly. You may disable dhcp and assign static IP addresses according to your setups.

vi /etc/netplan/00-installer-config.yaml
 enp0s8:
      addresses : [192.168.56.105/24]
:wq
netplan apply

Login using PuTTY.


Setup Network

Connect all VMs to 'Internal Network'.

Settings > Network > Adpater 3

Find the 'Internal Network' network interface.

ip add

Edit network configurations of VMs according to your setups.

vi /etc/netplan/00-installer-config.yaml
 enp0s9:
      addresses : [10.1.1.1/24]
:wq
netplan apply

Repeat the above steps in each machine accordingly and test the network connectivity.

Setup Nodes

In control node

Login as root to the node.

Change the hostname.

hostnamectl set-hostname controlnode
nano /etc/hosts
127.0.0.1 localhost
127.0.1.1 controlnode
reboot

Install Ansible

apt install ansible

In each managed node

Login as root to the node and change hostname. Each should have a unique hostname.

hostnamectl set-hostname m1
nano /etc/hosts
127.0.0.1 localhost
127.0.1.1 m1
reboot

Setup Passwordless SSH

Login to control node as normal user.

ssh-keygen -t rsa

Continue by entering the prompts.

cat /home/docker/.ssh/id_rsa.pub

Copy the ssh keys (the output of the above command) generated on the control node, paste and save it in the authorized_keys file on both managed nodes.

nano /home/docker/.ssh/authorized_keys

Verify passwordless ssh from control node to other nodes.

ssh docker@<managed node internal network IP address>

Add the managed nodes to control node Ansible hosts file

Login to the control node.

nano inventory

Insert the managed nodes' internal network IP addresses to the file according to your setting. And save it.

10.1.1.2
10.1.1.3

Verify connectivity

ansible all -i inventory -m ping

Output should be similar to the following.

10.1.1.2 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}
10.1.1.3 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}

Create the Playbook - In control node - As root

nano <playbook name>.yml
- name: setup mysql 
  become: yes
  hosts: Dbservers
  vars:
    - user: test
    - password: M@#567uers
    - db: testdb
  tasks:
    - name: installing mysql and dependencies
      package:
       name: "{{item}}"
       state: present
       update_cache: yes
     loop:
       - mysql-server
       - mysql-client 
       - python3-mysqldb
       - libmysqlclient-dev
     become: yes
    - name: start and enable mysql service
      service:
        name: mysql
        state: started
        enabled: yes
    - name: creating mysql user 
      mysql_user:
        name: "{{user}}"
        password: "{{password}}"
        priv: '*.*:ALL'
        host: '%'
        state: present
    - name: creating db
      mysql_db:
        name: "{{db}}"
        state: present
  handlers:
    - name: restart mysql
      service:
        name: mysql
        state: restarted

Run the Playbook - In control node - As root

ansible-playbook <playbook name>.yml -e

Verify results - In managed nodes

mysql -u root -p
show databases;

Attachments (1)

Download all attachments as: .zip

Note: See TracWiki for help on using the wiki.