Skip to content

Latest commit

 

History

History
150 lines (81 loc) · 6.68 KB

File metadata and controls

150 lines (81 loc) · 6.68 KB

Ansible Refactoring And Static Assignment (Imports And Roles) Project

In this project, I automated the deployment of a websolution on webservers by configuring UAT Web Servers using Ansible imports and roles.

First Step – Jenkins job enhancement.

  1. Go to your Jenkins-Ansible server and create a new directory called ansible-config-artifact – we will store there all artifacts after each build.
  • Run sudo mkdir /home/ubuntu/ansible-config-artifact

create directory

  1. Change permissions to this directory, so Jenkins could save files there.
  • Run sudo chmod -R 0777 /home/ubuntu/ansible-config-artifact

change file permission

  1. Go to Jenkins web console and install "Copy Artifacts" pligin-> Manage Jenkins -> Manage Plugins -> on Available tab search for "Copy Artifact plugin and install this plugin without restarting Jenkins.

install Copy Artifact plugin

  1. Create a new Freestyle project and name it - save_artifacts.

create Freestyle project

  1. Configure the project.

configure project configure project

  1. Create a Build step and choose Copy artifacts from other project, specify ansible as a source project and /home/ubuntu/ansible-config-artifact as a target directory.

configure project

  1. Test the set up by making some changes in README.MD file inside ansible-config-mgt repository. If both Jenkins jobs have completed one after another – you shall see your files inside /home/ubuntu/ansible-config-artifact directory and it will be updated with every commit to your master branch.

Test the setup Test the setup Test the setup

Second Step – Refactor Ansible code by importing other playbooks into site.yml

  1. Before starting to refactor the codes, ensure that you have pulled down the latest code from master (main) branch, and created a new branch, name it refactor.

pull down latest code

  1. Within playbooks folder, create a new file and name it site.yml. Create a new folder in root of the repository and name it static-assignments.

create site.yml

  1. Move common.yml file into the newly created static-assignments folder. Inside site.yml file, import common.yml playbook.

edit site.yml

folder structure

  1. Create another playbook under static-assignments and name it common-del.yml. In this playbook, configure deletion of wireshark utility.

created static-assignment/common-del

  • update site.yml with import_playbook: ../static-assignments/common-del.yml instead of common.yml. By default path to inventory is /etc/ansible/hosts. Change the path in ansible.cfg file and then run it against dev servers:

  • Run sudo vi /etc/ansible/ansible.cfg, uncomment and add path to inventory line then save. Confirm host availability with ansible all -m ping.

  • Run cd /home/ubuntu/ansible-config-artifact/

  • Run ansible-playbook -i inventory/dev.yml playbooks/site.yml

edit ansible.cfg run playbooks run playbooks run playbooks

  • check that wireshark is deleted on all the servers.
    • Run which wireshark or wireshark --version

ansible-playbook ansible-playbook ansible-playbook

Third Step – Configure UAT Webservers with a role ‘Webserver’

  1. Launch 2 fresh EC2 instances using RHEL 8 image, we will use them as our uat servers, so give them names accordingly – Web1-UAT and Web2-UAT (Only Web Servers and the existing Jenkins-Ansible server up and running).

create files on Github

  1. To create a roles directory and a folder called webserver, relative to the playbook file in Github rather than locally on Jenkins-Ansible server with necessary folders and files.

create files on Github

  1. Update your inventory ansible-config-mgt/inventory/uat.yml file with IP addresses of your 2 UAT Web servers (use SSh agent).

Add IP of webservers to uat.yml

  1. In /etc/ansible/ansible.cfg file uncomment roles_path string and provide a full path to your roles directory roles_path = /home/ubuntu/ansible-config-mgt/roles, so Ansible could know where to find configured roles. update roles path
  • Also, make sure to add path to private key file added to ssh-agent.

update roles path

  1. Configuration tasks to be performed in the tasks/main.yml file to do the following:
  • Install and configure Apache (httpd service)
  • Clone Tooling website from GitHub https://github.com//tooling.git.
  • Ensure the tooling website code is deployed to /var/www/html on each of 2 UAT Web servers.
  • Make sure httpd service is started.

main.yml

Forth Step – Reference ‘Webserver’ role

  1. Within the static-assignments folder, create a new assignment for uat-webservers as uat-webservers.yml.

create new folder

  • Refer to uat-webservers.yml role inside site.yml being the entry point to the anible configuration.

refer to site.yml

Fifth Step – Commit & Test

  1. Commit your changes, create a Pull Request and merge them to master branch, make sure webhook triggered two consequent Jenkins jobs, ran successfully and copied all the files to your Jenkins-Ansible server into /home/ubuntu/ansible-config-artifact/ directory.

commit changes

merge changes

webhook trigger

  • Now run the playbook against your uat inventory and see what happens:

    • Run sudo ansible-playbook -i /home/ubuntu/ansible-config-artifact/inventory/uat.yml /home/ubuntu/ansible-config-artifact/playbooks/site.yml

    run playbooks

    run playbooks run playbooks

  • UAT Web servers configured and and solutions accessible via web browsers accordingly.

  • Make sure to enable http in the inbound rule on tcp port 80 in AWS inbound tule

tool web solution portal access