Skip to content

Commit cc3febf

Browse files
first part
1 parent 81fb658 commit cc3febf

17 files changed

Lines changed: 1177 additions & 172 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
name: Ansible Deployment
2+
3+
on:
4+
push:
5+
branches: [ main, master ]
6+
paths:
7+
- 'ansible/**'
8+
- '!ansible/docs/**'
9+
- '.github/workflows/ansible-deploy.yml'
10+
pull_request:
11+
branches: [ main, master ]
12+
paths:
13+
- 'ansible/**'
14+
15+
jobs:
16+
lint:
17+
name: Ansible Lint
18+
runs-on: ubuntu-latest
19+
steps:
20+
- name: Checkout code
21+
uses: actions/checkout@v4
22+
23+
- name: Set up Python
24+
uses: actions/setup-python@v5
25+
with:
26+
python-version: '3.12'
27+
28+
- name: Install dependencies
29+
run: |
30+
pip install ansible ansible-lint
31+
32+
- name: Run ansible-lint
33+
run: |
34+
cd ansible
35+
ansible-lint playbooks/*.yml
36+
37+
deploy:
38+
name: Deploy Application
39+
needs: lint
40+
runs-on: self-hosted
41+
steps:
42+
- uses: actions/checkout@v4
43+
- name: Deploy with Ansible
44+
run: |
45+
cd ansible
46+
echo "${{ secrets.ANSIBLE_VAULT_PASSWORD }}" > /tmp/vault_pass
47+
ansible-playbook playbooks/deploy.yml \
48+
--vault-password-file /tmp/vault_pass \
49+
--tags "app_deploy"
50+
rm /tmp/vault_pass
51+
- name: Verify Deployment
52+
run: |
53+
sleep 10 # Wait for app to start
54+
curl -f http://${{ secrets.VM_HOST }}:8000 || exit 1
55+
curl -f http://${{ secrets.VM_HOST }}:8000/health || exit 1

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
![Workflow](https://github.com/BulatGazizov-dev/DevOps-Core-Course/actions/workflows/python-ci.yml/badge.svg)
44

5+
[![Ansible Deployment](https://github.com/yBulatGazizov-dev/DevOps-Core-Course/actions/workflows/ansible-deploy.yml/badge.svg)](https://github.com/yBulatGazizov-dev/DevOps-Core-Course/actions/workflows/ansible-deploy.yml)
6+
57
[![Labs](https://img.shields.io/badge/Labs-18-blue)](#labs)
68
[![Exam](https://img.shields.io/badge/Exam-Optional-green)](#exam-alternative)
79
[![Duration](https://img.shields.io/badge/Duration-18%20Weeks-lightgrey)](#course-roadmap)

ansible/docs/LAB06.md

Lines changed: 849 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
1-
$ANSIBLE_VAULT;1.1;AES256
2-
65303433666634303436373461313837653238613463363931643361653664316238623264393266
3-
3834336566373731306136323631346136643466666436650a356664396463666436646163383565
4-
64303335303366376537316266346234383761323864623639343035343565356430336461303462
5-
3161613031356666360a393161313162396363613033323639313264363637373230326532313565
6-
65643334663232396230616434643730343732646638646131333865643664343064363366643432
7-
62366635613738393661323062353134356436313135656638656133613866383836396633356235
8-
65616263316431636133343866643865343031333633306631663366653530303362376533353132
9-
65303434646666383937396432356261323461613761636539393966303335313034666662383339
10-
61303062343637613734326531336533346663323931393366323033313534616230653763616566
11-
31333730626563656530656531313563303433346663356461313364333837316536343864626631
12-
37303336613536643939376432616332633361653564633037623231316233613562353938383732
13-
36626264386437376466303763666366366238333831393133323534643532356664366233313132
14-
62666337393731326133333632313661656533363034663536363535633333303838663839633465
15-
66633761373963336638623938396139313266646538343534623230376632383331323063626235
16-
31316132333137633636623532373239313230346133393535616139393438393361323030343739
17-
36326366393037396266333635373162636338333562313639363332646538623738663233346661
18-
62366437653532333439313362396636366332343062306463383830343334613235353534333737
19-
34316461346334666533313662626331363237636335306636646137373861306537393138616639
20-
65346431383739636238623237643130336630313532333236386163356666666330326562613730
21-
32303032643639623761
1+
docker_image: bulatgazizov/python_app
2+
docker_tag: latest
3+
app_port: 8000
4+
app_internal_port: 5000
5+
app_name: "app"
6+
7+
compose_project_dir: "/opt/{{ app_name }}"
8+
docker_compose_version: "3.8"
9+
10+
app_env:
11+
DEBUG: "false"
12+
13+
app_secret_key: !vault |

ansible/inventory/hosts.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[webservers]
2-
your-vm-name ansible_host=45.150.238.55 ansible_user=root ansible_ssh_private_key_file=~/.ssh/id_rsa
2+
your-vm-name ansible_host=89.169.172.199 ansible_user=ubuntu ansible_ssh_private_key_file=~/.ssh/id_rsa
33

44
[webservers:vars]
55
ansible_python_interpreter=/usr/bin/python3

ansible/playbooks/deploy.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
become: yes
55

66
roles:
7-
- app_deploy
7+
- web_app

ansible/roles/app_deploy/defaults/main.yml

Lines changed: 0 additions & 9 deletions
This file was deleted.

ansible/roles/app_deploy/tasks/main.yml

Lines changed: 0 additions & 75 deletions
This file was deleted.
Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,42 @@
11
---
2-
- name: Update apt cache
3-
apt:
4-
update_cache: yes
5-
cache_valid_time: 3600
2+
- name: Install packages
3+
block:
4+
- name: Update apt cache
5+
apt:
6+
update_cache: yes
7+
cache_valid_time: 3600
68

7-
- name: Install common packages
8-
apt:
9-
name: "{{ common_packages }}"
10-
state: present
9+
- name: Install common packages
10+
apt:
11+
name: "{{ common_packages }}"
12+
state: present
13+
rescue:
14+
- name: Fix missing package sources on failure
15+
command: apt-get update --fix-missing
16+
when: ansible_failed_task.name == "Update apt cache" # or check register
17+
- name: Retry package installation
18+
apt:
19+
name: "{{ common_packages }}"
20+
state: present
21+
always:
22+
- name: Log common role completion
23+
copy:
24+
content: "Common role completed at {{ ansible_date_time.iso8601 }}"
25+
dest: /tmp/common_role_complete.log
26+
become: true
27+
tags:
28+
- packages
29+
- common
30+
31+
- name: Create user
32+
block:
33+
- name: Create user
34+
user:
35+
name: "{{ user_name }}"
36+
state: present
37+
shell: /bin/bash
38+
createhome: yes
39+
become: true
40+
tags:
41+
- users
42+
- common
Lines changed: 78 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,87 @@
11
---
2-
- name: Update apt cache
3-
apt:
4-
update_cache: yes
5-
cache_valid_time: 3600
2+
- name: Docker Installation
3+
tags:
4+
- docker_install
5+
- docker
6+
block:
7+
- name: Update apt cache
8+
apt:
9+
update_cache: yes
10+
cache_valid_time: 3600
611

7-
- name: Install required system packages
8-
apt:
9-
name:
10-
- curl
11-
- ca-certificates
12-
state: present
12+
- name: Install required system packages
13+
apt:
14+
name:
15+
- curl
16+
- ca-certificates
17+
state: present
1318

14-
- name: Create keyrings directory
15-
file:
16-
path: /etc/apt/keyrings
17-
state: directory
18-
mode: '0755'
19+
- name: Create keyrings directory
20+
file:
21+
path: /etc/apt/keyrings
22+
state: directory
23+
mode: '0755'
1924

20-
- name: Download Docker's official GPG key
21-
get_url:
22-
url: https://download.docker.com/linux/debian/gpg
23-
dest: /etc/apt/keyrings/docker.asc
24-
mode: '0644'
25+
- name: Download Docker GPG key
26+
block:
27+
- name: Download Docker's official GPG key
28+
get_url:
29+
url: https://download.docker.com/linux/ubuntu/gpg
30+
dest: /etc/apt/keyrings/docker.asc
31+
mode: '0644'
32+
rescue:
33+
- name: Wait 10 seconds and retry GPG key download
34+
pause:
35+
seconds: 10
36+
- name: Retry Docker GPG key download
37+
get_url:
38+
url: https://download.docker.com/linux/ubuntu/gpg
39+
dest: /etc/apt/keyrings/docker.asc
40+
mode: '0644'
2541

26-
- name: Add Docker repository to apt sources
27-
template:
28-
src: docker.sources.j2
29-
dest: /etc/apt/sources.list.d/docker.sources
30-
mode: '0644'
31-
vars:
32-
codename: "{{ ansible_distribution_release }}"
42+
- name: Add Docker repository to apt sources
43+
template:
44+
src: docker.sources.j2
45+
dest: /etc/apt/sources.list.d/docker.sources
46+
mode: '0644'
47+
vars:
48+
codename: "{{ ansible_distribution_release }}"
3349

34-
- name: Update apt cache after adding Docker repo
35-
apt:
36-
update_cache: yes
50+
- name: Update apt cache after adding Docker repo
51+
apt:
52+
update_cache: yes
3753

38-
- name: Install Docker packages
39-
apt:
40-
name:
41-
- "{{ 'docker-ce' if docker_version == '' else 'docker-ce=' + docker_version }}"
42-
- "{{ 'docker-ce-cli' if docker_cli_version == '' else 'docker-ce-cli=' + docker_cli_version }}"
43-
- "{{ 'containerd.io' if containerd_version == '' else 'containerd.io=' + containerd_version }}"
44-
- docker-buildx-plugin
45-
- docker-compose-plugin
46-
state: present
47-
become: yes
48-
notify: restart docker
54+
- name: Install Docker packages
55+
apt:
56+
name:
57+
- "{{ 'docker-ce' if docker_version == '' else 'docker-ce=' + docker_version }}"
58+
- "{{ 'docker-ce-cli' if docker_cli_version == '' else 'docker-ce-cli=' + docker_cli_version }}"
59+
- "{{ 'containerd.io' if containerd_version == '' else 'containerd.io=' + containerd_version }}"
60+
- docker-buildx-plugin
61+
- docker-compose-plugin
62+
state: present
63+
notify: restart docker
64+
always:
65+
- name: Ensure Docker service is running and enabled
66+
systemd:
67+
name: docker
68+
state: started
69+
enabled: yes
70+
become: true
4971

50-
- name: Ensure Docker service is running and enabled
51-
systemd:
52-
name: docker
53-
state: started
54-
enabled: yes
72+
- name: Docker config
73+
tags:
74+
- docker_config
75+
- docker
76+
block:
77+
- name: Add user to docker group
78+
user:
79+
name: "{{ ansible_user }}"
80+
groups: docker
81+
append: yes
5582

56-
- name: Add user to docker group
57-
user:
58-
name: "{{ ansible_user }}"
59-
groups: docker
60-
append: yes
61-
62-
- name: Install python3-docker for Ansible docker modules
63-
apt:
64-
name: python3-docker
65-
state: present
83+
- name: Install python3-docker for Ansible docker modules
84+
apt:
85+
name: python3-docker
86+
state: present
87+
become: true

0 commit comments

Comments
 (0)