81
ansible/training/lessons/1-intro.md
Normal file
81
ansible/training/lessons/1-intro.md
Normal file
@@ -0,0 +1,81 @@
|
||||
|
||||
# Before starting
|
||||
## Host and Demo
|
||||
You are currently connected to a server which have docker installed.
|
||||
We will call this server: `localhost`.
|
||||
|
||||
You also have a docker container running in the background.
|
||||
We will call this container: `demo`.
|
||||
|
||||
You can check your containers with:
|
||||
`docker ps`
|
||||
|
||||
During these lessons, you will be ask to use ansible to configure both `localhost` and `demo`
|
||||
|
||||
# Installation
|
||||
|
||||
Install ansible on `localhost`
|
||||
|
||||
```
|
||||
apt-get install ansible
|
||||
```
|
||||
|
||||
# First use
|
||||
|
||||
## Check install
|
||||
Check if ansible is correctly installed (and the version you have):
|
||||
```
|
||||
ansible --version
|
||||
```
|
||||
```
|
||||
ansible 2.7.7
|
||||
config file = /etc/ansible/ansible.cfg
|
||||
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
|
||||
ansible python module location = /usr/lib/python3/dist-packages/ansible
|
||||
executable location = /usr/bin/ansible
|
||||
python version = 3.7.3 (default, Jul 25 2020, 13:03:44) [GCC 8.3.0]
|
||||
|
||||
```
|
||||
|
||||
## Ad-hoc ping
|
||||
Use the builtin `ping` module to do your first ad-hoc command:
|
||||
```
|
||||
ansible localhost -m ping
|
||||
```
|
||||
This should answer with `pong`, like this:
|
||||
```
|
||||
localhost | SUCCESS => {
|
||||
"changed": false,
|
||||
"ping": "pong"
|
||||
}
|
||||
```
|
||||
|
||||
Q: What happened behing the scene?
|
||||
|
||||
## Ad-hoc shell
|
||||
|
||||
The `shell` module is a very nice ansible builtin module that can be use to execute commands on a host, e.g. with localhost:
|
||||
```
|
||||
ansible localhost -m shell -a "hostname"
|
||||
```
|
||||
Result:
|
||||
```
|
||||
localhost | CHANGED | rc=0 >>
|
||||
ansible101
|
||||
|
||||
```
|
||||
Q: what command can you use to get the IP of your machine?
|
||||
|
||||
## Ad-hoc setup
|
||||
The `setup`module is a builtin module that collects data (also known as `facts`) on hosts.
|
||||
These data can then be used as variables in your future playbooks (we will see that later).
|
||||
|
||||
```
|
||||
ansible localhost -m setup
|
||||
```
|
||||
```
|
||||
# long list of variables
|
||||
```
|
||||
|
||||
Q: using this module, what other command can you use to retrieve the ip of your host?
|
||||
|
||||
44
ansible/training/lessons/2-inventory.md
Normal file
44
ansible/training/lessons/2-inventory.md
Normal file
@@ -0,0 +1,44 @@
|
||||
|
||||
# Inventory
|
||||
|
||||
Until now, you only used ansible to manage `localhost`.
|
||||
|
||||
But ansible can be used to manage multiple systems that you have in your infrastructure.
|
||||
To do so, we must tell ansible which servers compose our infrastructure. This is done thanks to an `inventory` file.
|
||||
The default `inventory` file for ansible is located in `/etc/ansible/hosts`.
|
||||
|
||||
```
|
||||
cat /etc/ansible/hosts
|
||||
```
|
||||
|
||||
It's empty by default (not really empty, but everything is commented).
|
||||
With an empty `inventory` like that, ansible only knows about `localhost`.
|
||||
|
||||
|
||||
## Demo server
|
||||
|
||||
### Add a server in your inventory
|
||||
|
||||
Add the following line at the end of your inventory:
|
||||
```
|
||||
demo ansible_username=root ansible_password=root ansible_connection=ssh ansible_host=127.0.0.2 ansible_port=2222 ansible_python_interpreter=/usr/bin/python3
|
||||
```
|
||||
|
||||
This line adds our server named `demo` in ansible inventory. It also configure ansible to connect with login `root`, password `root` on port `2222`.
|
||||
The server IP address is `127.0.0.2` which sound weird, but that's because it's a docker container running locally (for the demo).
|
||||
Finally, we tell ansible to use `python3` interpreter (default is `python`, but it's not available in the demo server).
|
||||
|
||||
### Check if running
|
||||
Try to connect on your demo machine, to see if it's running:
|
||||
```
|
||||
ssh -p 2222 root@127.0.0.2
|
||||
```
|
||||
|
||||
### ping
|
||||
Now use an ansible ad-hoc module to check if ansible is able to connect to this machine
|
||||
|
||||
```
|
||||
ansible ... # complete by yourself / see previous lessons
|
||||
```
|
||||
|
||||
|
||||
86
ansible/training/lessons/3-playbooks.md
Normal file
86
ansible/training/lessons/3-playbooks.md
Normal file
@@ -0,0 +1,86 @@
|
||||
# Playbooks
|
||||
Playbooks in Ansible are simple `YAML` file that describe configuration which should be done on a remote server.
|
||||
Playbooks are really simple but very powerful; the tasks mentioned inside the playbook are execute sequentially.
|
||||
`YAML` is human-readable data serialization format, which cares about whitespace like `Python`, it represents tree-like structure.
|
||||
|
||||
## First playbook
|
||||
|
||||
### Step 1
|
||||
Create a file `myplaybook.yml` with the following content:
|
||||
|
||||
```
|
||||
---
|
||||
- hosts: demo
|
||||
gather_facts: yes
|
||||
tasks:
|
||||
- name: Install the git package
|
||||
apt:
|
||||
name: git
|
||||
state: present
|
||||
```
|
||||
Here is the explanation of this playbook:
|
||||
|
||||
`- hosts: demo` is here to tell ansible that this part of the playbook should be applied on server `demo`
|
||||
This playbook is very simple and apply only on `demo`, but you will often have playbooks that apply on multiple hosts. Ansible is great tool to orchestrate installation.
|
||||
|
||||
`gather_facts: yes` tells ansible to execute the `setup` module before doing anything else. The `setup` module will collects the `facts` and store them as variables. Those variables can then be used in playbooks. In this demo playbook, we dont use any variable yet, but you will see that later.
|
||||
|
||||
`tasks:`is a list of task that will be applied on the server. For now we only have one task, which is using the module `apt` to install the `git` package.
|
||||
|
||||
Now run the play:
|
||||
```
|
||||
ansible-playbook myplaybook.yml
|
||||
```
|
||||
After this, your `demo` server is supposed to have `git` installed.
|
||||
|
||||
### Step 2
|
||||
|
||||
Add a task to clone a flask application from github: `https://github.com/arnaudmorin/demo-flask.git`
|
||||
(A flask application is a very simple web server written in python - so basically, it's a website)
|
||||
```
|
||||
---
|
||||
- hosts: demo
|
||||
gather_facts: yes
|
||||
tasks:
|
||||
- name: Install the git package
|
||||
apt:
|
||||
name: git
|
||||
update_cache: yes
|
||||
state: present
|
||||
- name: Clone my flask application
|
||||
git: ... # complete by yourself
|
||||
# see https://docs.ansible.com/ansible/latest/collections/ansible/builtin/git_module.html
|
||||
```
|
||||
|
||||
### Step 3
|
||||
Add a task to install the flask app.
|
||||
Check the github page https://github.com/arnaudmorin/demo-flask for info on how this apps can be installed (but dont do it manually).
|
||||
Then, add a task to your playbook that will do the installation for you.
|
||||
|
||||
### Step 4
|
||||
Add a task to start the app in background.
|
||||
|
||||
### Step 5
|
||||
Your `demo` server is now running the flask application.
|
||||
Unfortunately, this is running in a docker container inside your `localhost`.
|
||||
So the application is not yet accessible from internet.
|
||||
|
||||
What you should do now is to make this application accessible from internet!
|
||||
By chance, the demo container is having port `8080` opened:
|
||||
```
|
||||
docker port demo
|
||||
```
|
||||
will give you:
|
||||
```
|
||||
8080/tcp -> 127.0.0.2:8080
|
||||
22/tcp -> 127.0.0.2:2222
|
||||
```
|
||||
|
||||
So, to access demo from internet, you will have to install a `proxy` server.
|
||||
Here are some tips:
|
||||
* Extend your playbook to not only manage `demo` but also `localhost`.
|
||||
For `localhost`, add a least 2 tasks:
|
||||
* install nginx
|
||||
* configure nginx as a proxy (port 80 --> 8080) (google is your friend)
|
||||
* Bonus: How can we restart nginx only after we changed it's configuration? (tip: ansibles handlers)
|
||||
* Bonus2: using some ansible variables to add a debug task to your playbook to print the URL of your website.
|
||||
14
ansible/training/lessons/4-roles.md
Normal file
14
ansible/training/lessons/4-roles.md
Normal file
@@ -0,0 +1,14 @@
|
||||
# Roles
|
||||
## Introduction
|
||||
Roles in ansible are composed of multiples tasks that are executed together in order to configure a piece of software.
|
||||
Usually, roles are included by playbooks.
|
||||
The nice thing with roles is that they can be reused at different places, and thus it avoid copy/pasting of tasks.
|
||||
Moreover, roles can take variables as input, so the tasks in the roles can do various things based on input parameters.
|
||||
|
||||
## Build your first role
|
||||
In the previous lession, you built a playbook with tasks only.
|
||||
Your job now will be to create 2 roles:
|
||||
* one role for all tasks related to deploying the flask application
|
||||
* one role for all tasks related to deploying nginx as a proxy
|
||||
* for this one, you must also use some variables as input parameters for both ports (80 and 8080 in the previous lessons)
|
||||
|
||||
Reference in New Issue
Block a user