Mastering DevOps: How to Install Jenkins on Ubuntu via Ansible

In this post we will cover the beginnings of an Ansible Implementation on Ubuntu in the AWS Cloud.

What we will cover:

  • AWS Ubuntu Instance provisioning
  • Ansible Installation
  • Inventory file creation
  • Pinging target nodes with ansible -m ping
  • Installing Apache and creating an index.html using an Ansible Playbook
  • Installing Java SDK using an Ansible Playbook
  • Installing Jenkins using an Ansible Playbook

So lets get started by spinning up 3 new Ubuntu Instances on AWS using this Amazon Machine Image Id (ami-07013dd48140efd73) in the N. California (us-west-1) region of AWS.

On the AWS Console in the N. California Region https://us-west-1.console.aws.amazon.com/ from the Services menu select Services > Compute > EC2
In EC2 Dashboard click the Instances link
Click the orange Launch Instance button (in the upper righthand corner of the page)
In the Name field we will call it Ansible Ubuntu (for now)
Under Application and OS Images (Amazon Machine Image) copy and past this AMI Id (ami-07013dd48140efd73) into the search field and press enter. You should see AWS Ubuntu rsa ssh 2022 ami-07013dd48140efd73 in the search results.
Click the orange Select button
For our purposes the default selection of t2.micro Instance Type is sufficient and Free Tier eligible, although not recommended for production.
Under Key pair (login) click create new key pair
For the Key pair name we will enter Ansible Key
Under Private key file format if you are using a Mac you can leave the default selection of .pem if you are on Windows and will be using PuTTY then select .ppk
In the Network settings section click Edit (to the right of the Network settings heading)
Click the Add security group rule button
In the Port range box enter 8080
In the Description box enter Jenkins
On the right hand side of the page in the Summary area enter 3 in the Number of instances box
Review the settings in the Summary section and compare to above the click Launch instance

While we wait for those 3 instances to spin up we can setup PuTTY to connect to Control Node (any one of the 3 instances can be the control node at this point)
Launch PuTTY (if you don’t have PuTTY installed yet download and install from here: https://www.putty.org/
In the Host Name (or IP address) box enter the IP address of one of 3 instances created earlier
In the Category list under the Connection section select Data
In the Auto-login username box enter ubuntu
In the Category list under the Connection section expand the SSH section (click the +)
Click Auth and click the Browse button next to the Private key file for authentication box
Browse to the Downloads folder and select the Ansible Key.ppk file created earlier
In the Category list select Session
In the Saved Sessions box enter Ansible Control Node and click Save
Click the Open button to open an SSH connection to the Ansible Control Node
Type yes to accept the key

Now that we are connected the control node we can update the cache and install Ansible with the code below. Copy and paste the code into the console and press enter

sudo apt update && sudo apt upgrade
sudo apt install ansible
sudo apt upgrade ansible

type yes to accept the install

Now that Ansible is installed we can test it with some basic playbooks. Clone the ProDataMan Ansible Intro repo using the command below in the console.

git clone https://github.com/ProDataMan/Ansible-Intro.git

Once the repo has been cloned and the files copied move into the Ansible-Intro folder and edit the inventory file replacing the IP Addresses with the address from your control node and 2 target nodes created earlier

vim inventory

press i to enter insert mode and make your changes
press <esc> to exit insert mode
press<shift> + zz to save

Test connectivity with Ansible ping

ansible -m ping <IP Address of Node1>
ansible -m ping <IP Address of Node2>

Type yes to accept the key on each node

Now that connectivity has been confirmed and keys accepted lets run a few playbooks. The first playbook (webservers.yml) will install Apache on the 2 managed nodes (node1 and node2) and create a new index.html page. The InstallJava.yml playbook does exactly what it sounds like it does on all 3 servers. The InstallJenkins.yml playbook installs Jenkins on the Ansible Control Node

ansible-playbook -i inventory webservers.yml
ansible-playbook -inventory InstallJava.yml
ansible-playbook -inventory InstallJenkins.yml

To verify the webservers playbook open a browser and enter the IP Address of either of the 2 managed servers (node1 or node2) and press enter. you should see a page that says “I’m a programmer now”
To verify the other 2 playbooks enter the IP Address of the control node and 8080 as the port number, separated by a colin (192.168.1.1:8080). You can use the code below on the control node to get the initial password for Jenkins

sudo cat /var/lib/jenkins/secrets/initialAdminPassword

Copy the password displayed and paste into the password field in Jenkins to complete the Jenkins setup.

Comment below if you would like to see a video demo of this process.

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart