Skip to content

Commit 61711ca

Browse files
Merge pull request #4 from aleyipsoftwire/exercise-4
Exercise 4
2 parents c07ebf5 + 269f31a commit 61711ca

File tree

6 files changed

+153
-0
lines changed

6 files changed

+153
-0
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,29 @@ The project uses pytest to run tests. To run the tests, run the following from y
7171
```bash
7272
$ poetry run pytest
7373
```
74+
75+
## Provisioning a VM from an Ansible Control Node
76+
77+
1. SSH into the control node
78+
2. Copy the `ansible` directory to the control node
79+
3. Replace the IP address in the `inventory` file with the IP address(es) of the managed VM(s)
80+
4. Create a file `ansible-pw.txt` containing the vault password
81+
5. Run the following command in the `ansible` directory, to provision the VM:
82+
```bash
83+
$ ansible-playbook playbook.yml -i inventory --vault-password-file ansible-pw.txt
84+
```
85+
86+
### Note on env variables
87+
88+
The ansible configs contained in this repo contain the encrypted env values required to run the app.
89+
These values can be seen in the `ansible/vars/env.yml` file.
90+
91+
The values are individually encrypted using ansible-vault and added to the file.
92+
93+
To generate a new encrypted value or to replace one of the provided values, for example, the `trello_api_key` value:
94+
95+
```bash
96+
$ ansible-vault encrypt_string --vault-password-file your_password_file --name 'trello_api_key'
97+
```
98+
99+
then enter the value you want to encrypt when prompted.

ansible/.env.j2

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Sets the location of the flask application object: https://flask.palletsprojects.com/en/2.3.x/cli/#application-discovery
2+
# Needed because of the presence of the "todo_app" folder
3+
FLASK_APP=todo_app/app
4+
5+
FLASK_ENV=production
6+
7+
TRELLO_API_KEY={{ trello_api_key }}
8+
TRELLO_API_TOKEN={{ trello_api_token }}
9+
TRELLO_BOARD_ID={{ trello_board_id }}
10+
11+
TRELLO_TODO_LIST_ID={{ trello_todo_list_id }}
12+
TRELLO_DONE_LIST_ID={{ trello_done_list_id }}

ansible/inventory

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[nodes]
2+
3.11.97.48

ansible/playbook.yml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
---
2+
- name: Install tools
3+
hosts: nodes
4+
become: true
5+
tasks:
6+
- name: Install Git and Python 3
7+
yum:
8+
name:
9+
- git
10+
- python3
11+
state: present
12+
update_cache: yes
13+
14+
- name: Install Poetry
15+
hosts: nodes
16+
tasks:
17+
- name: Install Poetry
18+
shell:
19+
cmd: 'curl -sSL https://install.python-poetry.org | python3 -'
20+
creates: ~/.local/bin/poetry
21+
22+
- name: Create To-Do App Directory and clone from GitHub
23+
hosts: nodes
24+
become: true
25+
tasks:
26+
- name: Create directory
27+
file:
28+
path: /opt/todoapp
29+
state: directory
30+
owner: ec2-user
31+
- name: Clone Remote Git Repository
32+
git:
33+
repo: https://github.com/aleyipsoftwire/DevOps-Course-Starter.git
34+
dest: /opt/todoapp
35+
36+
- name: Install dependencies and set env variables
37+
hosts: nodes
38+
vars_files:
39+
- ./vars/env.yml
40+
tasks:
41+
- name: Install dependencies
42+
command: poetry install
43+
args:
44+
chdir: /opt/todoapp
45+
- name: Set env variables
46+
template:
47+
src: .env.j2
48+
dest: /opt/todoapp/.env
49+
50+
- name: Copy service file and start service
51+
hosts: nodes
52+
become: true
53+
tasks:
54+
- name: Copy service file
55+
copy:
56+
src: todoapp.service
57+
dest: /etc/systemd/system/todoapp.service
58+
- name: Start service
59+
systemd:
60+
name: todoapp
61+
daemon_reload: true
62+
state: restarted

ansible/todoapp.service

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[Unit]
2+
Description=My To-Do App
3+
4+
[Service]
5+
User=ec2-user
6+
WorkingDirectory=/opt/todoapp
7+
ExecStart=/home/ec2-user/.local/bin/poetry run flask run --host 0.0.0.0

ansible/vars/env.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
trello_api_key: !vault |
3+
$ANSIBLE_VAULT;1.1;AES256
4+
36353765346338383137376534373861386262346432666266343866653264373232393336663633
5+
3632313164363335616239363530663530393638643161660a323061653334393330366530383264
6+
62393833316439633037666162623762633736656136303039383665363433366465356333663933
7+
6433323730393332660a323837396535343461306333666364366334633836363732313633306136
8+
30303335306663663938643630346535366631316435333065663838636562376130376130666334
9+
6331373565646432623661373336656230356165343361623361
10+
11+
trello_api_token: !vault |
12+
$ANSIBLE_VAULT;1.1;AES256
13+
34323763343432663332353262393363623830356363643062386365386233336530333566383130
14+
3662643036303664333961363132623661383962653865380a626261376665313938383364346636
15+
38643066613665643332363666616362626130343465366666616532613635666535353662376631
16+
3865336265396565620a353136323065383166383162663233346338326530653831316365326330
17+
37666333363735386665313134393332356433613561316561643964373639386234313535663663
18+
39616163326231386331666232656435616564316361653066316435346133653635613639333034
19+
35343138353531373066656434323634396164346336656264653731303932393339353033613739
20+
35376165616138303564
21+
22+
trello_board_id: !vault |
23+
$ANSIBLE_VAULT;1.1;AES256
24+
32646363346165626630656632346131376630636430326465396333373966393337623036303233
25+
3131346331613532323531353331346461653339383436620a303263303633376463383663383236
26+
33383161633735383438313034303130333161666565376361353838613135393935366261343637
27+
6464306335353630620a306333323265393137376166643135623636333061623734346362376531
28+
37396264366533616434303630656363343265356532343735316337363230643630
29+
30+
trello_todo_list_id: !vault |
31+
$ANSIBLE_VAULT;1.1;AES256
32+
35303037373565326666336233666366303463323362373962643431373463613938353239353733
33+
3264396264643732393333373063363734616430633030380a646464663338333564653162643762
34+
65626533333936666530663034653632376539643135303236323634303739343431643266306362
35+
6437326334303965640a343366613334346562643263623265656461383731633634393330366336
36+
30353864383838333736366438383730373832623834613938383338396433363336
37+
38+
trello_done_list_id: !vault |
39+
$ANSIBLE_VAULT;1.1;AES256
40+
63333465633437303633396631356365323939633161323936393763343163343233363266656532
41+
3037343664313430313063373963346534646464396162380a646634393261323763313136333963
42+
34646434313664653136346264653633666162666566306665633731343830633062663637623666
43+
6234373361393338320a356462366139393330333661353130316566623739313565646431343336
44+
62376565643863613039353536626335343739633239346633313339363734336436

0 commit comments

Comments
 (0)