CONFIGURATION
TASKS WITH ANSIBLE PLAYBOOK
Ensure you have Ansible installed on
your control machine. You can install Ansible using the following command:
sudo apt update
sudo apt install ansible
ansible –version
Save your key.pem on ubuntu server
vi key.pem
chmod 600
key.pem
Pwd
For MacUsers Only (Copy Key.pem to Ec2)
scp -i ~/Desktop/20.pem ~/Desktop/20.pem
ubuntu@52.207.218.240:/tmp/
ssh -i ~/Desktop/20.pem ubuntu@52.207.218.240
Create Your Inventory File
Create an inventory file vi host.ini that lists your EC2 instances. For
example:
[ec2_instances]
ec2_instance ansible_host=3.15.0.203 ansible_user=ubuntu ansible_ssh_private_key_file=/home/ubuntu/key.pem
Create Your Playbook
Create a playbook file (
install_git_java.yml) with the following content:
---
- name: Install Git and Java on EC2 Instances
hosts: ec2_instances
become: yes
tasks:
- name: Update and upgrade apt packages
apt:
update_cache: yes
upgrade: dist
- name: Install Git
apt:
name: git
state: present
- name: Install Java
apt:
name: openjdk-11-jdk
state: present
This
playbook does the following:
- Updates the apt package cache and upgrades all
packages.
- Installs Git.
- Installs Java (OpenJDK 11).
Run the Playbook
Run the playbook using the following command:
ansible-playbook -i host.ini install_git_java.ymlPlaybook to Install Jenkins
---
- name: Install Jenkins on EC2 Instances
hosts: ec2_instances
become: yes
tasks:
- name: Update apt package index
apt:
update_cache: yes
- name: Install Java (Jenkins dependency)
apt:
name: openjdk-11-jdk
state: present
- name: Install required packages
apt:
name:
- gnupg
- software-properties-common
state: present
- name: Add Jenkins repository key to the system
apt_key:
url: https://pkg.jenkins.io/debian/jenkins.io.key
state: present
- name: Add Jenkins repository to the sources list
apt_repository:
repo: deb http://pkg.jenkins.io/debian-stable binary/
state: present
- name: Update apt package index (again)
apt:
update_cache: yes
- name: Install Jenkins
apt:
name: jenkins
state: present
- name: Ensure Jenkins is started and enabled on boot
systemd:
name: jenkins
enabled: yes
state: startedPlaybook for Installing Docker
---
- name: Install Docker
hosts: servers
become: yes
tasks:
- name: Update the apt package index
apt:
update_cache: yes
- name: Install Docker
apt:
name:
- apt-transport-https
- ca-certificates
- curl
- software-properties-common
state: present
- name: Add Docker GPG key
apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
state: present
- name: Add Docker repository
apt_repository:
repo: deb https://download.docker.com/linux/ubuntu focal stable
state: present
- name: Update the apt package index again
apt:
update_cache: yes
- name: Install Docker CE
apt:
name: docker-ce
state: present