Project goal
Using Ansible on a managed Ubuntu node:
- Install Docker
- Start the Docker service
- Run the Jenkins LTS container
- Open the Jenkins UI in a browser
Control Node → Ansible playbook → Managed Node → Docker → Jenkins container
Prerequisites
| Requirement | Notes |
|---|---|
| Ubuntu managed node | SSH from control node works (ansible all -m ping) |
| Security group | 22 (SSH), 8080 (Jenkins UI) |
Playbook
mkdir -p playbooks
vim playbooks/jenkins.yml---
- name: Install Docker and run Jenkins
hosts: all
become: true
tasks:
- name: Install Docker
apt:
name: docker.io
state: present
update_cache: true
- name: Start Docker service
service:
name: docker
state: started
enabled: true
- name: Add ubuntu user to docker group
user:
name: ubuntu
groups: docker
append: true
- name: Reset SSH connection
meta: reset_connection
- name: Remove existing Jenkins container
shell: docker rm -f jenkins
ignore_errors: true
changed_when: false
- name: Run Jenkins container
shell: |
docker run -d \
--name jenkins \
--restart unless-stopped \
-p 8080:8080 \
-p 50000:50000 \
-v jenkins_home:/var/jenkins_home \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /usr/bin/docker:/usr/bin/docker \
--user root \
jenkins/jenkins:lts
args:
creates: /var/lib/docker/containers # soft guard; prefer community.docker in prod
- name: Verify Jenkins container
shell: docker ps
register: docker_output
changed_when: false
- name: Display running containers
debug:
var: docker_output.stdout_linesansible-playbook playbooks/jenkins.yml
ansible all -m shell -a "docker ps"Access Jenkins
- Open
http://<managed-node-public-ip>:8080 - Unlock Jenkins with the initial admin password:
ansible all -m shell -a \
"docker exec jenkins cat /var/jenkins_home/secrets/initialAdminPassword"- Install suggested plugins and create an admin user
Useful checks
ansible all -m shell -a "docker logs jenkins --tail 50"
ansible all -m shell -a "docker exec jenkins java -version"| Command | Purpose |
|---|---|
docker ps | Running containers |
docker logs jenkins | Troubleshooting |
docker rm -f jenkins | Remove container |
Mounting the Docker socket gives Jenkins powerful host access — fine for labs, lock down for production. Prefer community.docker.docker_container over raw shell when you harden this playbook.
Mini assignment: add OpenJDK 17 install and a health-check task that fails the play if port 8080 is not listening.