Resources attached to the Road To DevOps tutorial https://blog.noobtoroot.xyz/road-to-devops/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
4.9 KiB

## TP Imports
* Reprendre le TP Wordpress et découper le Playbook en plusieurs fichiers de tâches.
* Exemples de découpage pouvant être définis :
<small>
- mariadb-install.yaml
- mariadb-configure.yaml
- apache-install.yaml
- apache-configure.yaml
- wordpress-install.yaml
- wordpress-configure.yaml
- test-service.yaml
</small>
* Utiliser `include_tasks` pour charger les fichiers de tâches.
`install-apache-wordpress-mariadb-imports.yaml`
<!-- .slide: data-state="medium-code" -->
```yaml
- hosts: ansible-2
vars:
- DB_NAME: wordpress
- DB_USER: wordpressuser
- DB_PASSWORD: "12345"
- DB_HOST: "{{ hostvars['ansible-2']['ansible_ssh_host'] }}"
- WEB_HOST: "{{ hostvars['ansible-1']['ansible_ssh_host'] }}"
tasks:
- include_tasks: imports/mariadb-install.yaml
- include_tasks: imports/mariadb-wordpress-configure.yaml
- hosts: ansible-1
vars:
- DB_NAME: wordpress
- DB_USER: wordpressuser
- DB_PASSWORD: "12345"
- DB_HOST: "{{ hostvars['ansible-2']['ansible_ssh_host'] }}"
tasks:
- include_tasks: imports/apache-install.yaml
- include_tasks: imports/wordpress-install.yaml
- include_tasks: imports/wordpress-configure.yaml
handlers:
# On utilise import_tasks pour que le fichier contenant
# les handlers soit chargé dès le départ
- import_tasks: imports/apache-handlers.yaml
```
`imports/mariadb-install.yaml`
<!-- .slide: data-state="medium-code" -->
```yaml
- name: Installation of mariadb-server package
yum:
name: mariadb-server
state: present
update_cache: yes
- name: Ensure mariadb-server is running (and enabled at boot)
service:
name: mariadb
state: started
enabled: yes
- name: Allow mariadb traffic on port 3306
firewalld:
service: mysql
permanent: true
state: enabled
immediate: yes
```
`imports/mariadb-wordpress-configure.yaml`
<!-- .slide: data-state="medium-code" -->
```yaml
- name: Installation of MySQL-python package
# package required to use ansible mysql modules
yum:
name: MySQL-python
state: present
update_cache: yes
- name: Create a new database with name '{{ DB_NAME }}'
mysql_db:
name: '{{ DB_NAME }}'
state: present
- name: Create a new database user with name '{{ DB_USER }}'
mysql_user:
name: '{{ DB_USER }}'
password: '{{ DB_PASSWORD }}'
priv: '{{ DB_NAME }}.*:ALL'
host: '{{ WEB_HOST }}'
state: present
```
`imports/apache-install.yaml`
<!-- .slide: data-state="medium-code" -->
```yaml
- name: Installation of apache package
dnf:
name: httpd
state: present
update_cache: yes
- name: Installation of php package
dnf:
name: php
state: present
update_cache: yes
- name: Installation of php-mysqlnd package
dnf:
name: php-mysqlnd
state: present
update_cache: yes
notify: Reload Apache
- name: Installation of wget package
dnf:
name: wget
state: present
update_cache: yes
- name: Ensure apache is running (and enabled at boot)
service:
name: httpd
state: started
enabled: yes
- name: Allow http traffic on port 80
firewalld:
service: http
permanent: true
state: enabled
immediate: yes
```
`imports/apache-handlers.yaml`
<!-- .slide: data-state="medium-code" -->
```yaml
- name: Reload Apache
service:
name: httpd
state: restarted
```
`imports/wordpress-install.yaml`
<!-- .slide: data-state="medium-code" -->
```yaml
- name: Download wordpress archive
get_url:
url: https://wordpress.org/wordpress-5.0.8.tar.gz
dest: /var/www/html/wordpress.tar.gz
mode: 0440
- name: Untar wordpress archive
unarchive:
src: /var/www/html/wordpress.tar.gz
dest: /var/www/html
remote_src: true
- name: Remove wordpress archive
file:
path: /var/www/html/wordpress.tar.gz
state: absent
```
`imports/wordpress-configure.yaml`
<!-- .slide: data-state="medium-code" -->
```yaml
- name: Create wordpress configuration file
copy:
src: /var/www/html/wordpress/wp-config-sample.php
dest: /var/www/html/wordpress/wp-config.php
remote_src: true
- name: Update wordpress configuration file with db name
replace:
dest: /var/www/html/wordpress/wp-config.php
regexp: 'database_name_here'
replace: '{{ DB_NAME }}'
- name: Update wordpress configuration file with user name
replace:
dest: /var/www/html/wordpress/wp-config.php
regexp: 'username_here'
replace: '{{ DB_USER }}'
- name: Update wordpress configuration file with user password
replace:
dest: /var/www/html/wordpress/wp-config.php
regexp: 'password_here'
replace: '{{ DB_PASSWORD }}'
- name: Update wordpress configuration file with host
replace:
dest: /var/www/html/wordpress/wp-config.php
regexp: 'localhost'
replace: '{{ DB_HOST }}'
```