Slide 1

Slide 1 text

Managing WordPress with Ansible David Murphy, October 2015

Slide 2

Slide 2 text

@schwuk http://schwuk.com David Murphy, October 2015

Slide 3

Slide 3 text

David Murphy, October 2015

Slide 4

Slide 4 text

opinionated David Murphy, October 2015

Slide 5

Slide 5 text

more than one way to do it David Murphy, October 2015

Slide 6

Slide 6 text

why bother with any of this? David Murphy, October 2015

Slide 7

Slide 7 text

doing it yourself David Murphy, October 2015

Slide 8

Slide 8 text

knowing how to do it yourself David Murphy, October 2015

Slide 9

Slide 9 text

automation David Murphy, October 2015

Slide 10

Slide 10 text

if you are going to do something more than once, automate it David Murphy, October 2015

Slide 11

Slide 11 text

Choices David Murphy, October 2015

Slide 12

Slide 12 text

Choice #1: Software David Murphy, October 2015

Slide 13

Slide 13 text

David Murphy, October 2015

Slide 14

Slide 14 text

Choice #2: Platform David Murphy, October 2015

Slide 15

Slide 15 text

David Murphy, October 2015

Slide 16

Slide 16 text

they are all good David Murphy, October 2015

Slide 17

Slide 17 text

Choice #3: Hosting David Murphy, October 2015

Slide 18

Slide 18 text

David Murphy, October 2015

Slide 19

Slide 19 text

David Murphy, October 2015

Slide 20

Slide 20 text

they are not all good David Murphy, October 2015

Slide 21

Slide 21 text

Choice #4: Tool David Murphy, October 2015

Slide 22

Slide 22 text

David Murphy, October 2015

Slide 23

Slide 23 text

again, they are all good David Murphy, October 2015

Slide 24

Slide 24 text

David Murphy, October 2015

Slide 25

Slide 25 text

Ansible is a powerful automation tool that you can learn quickly. David Murphy, October 2015

Slide 26

Slide 26 text

how does it work? David Murphy, October 2015

Slide 27

Slide 27 text

inventory playbooks roles David Murphy, October 2015

Slide 28

Slide 28 text

inventory playbooks roles David Murphy, October 2015

Slide 29

Slide 29 text

Inventory David Murphy, October 2015

Slide 30

Slide 30 text

Sample inventory [webservers] www1.example.com www2.example.com [dbservers] db0.example.com db1.example.com David Murphy, October 2015

Slide 31

Slide 31 text

Naming patterns [webservers] www[01:50].example.com [databases] db-[a:f].example.com David Murphy, October 2015

Slide 32

Slide 32 text

Groups-of-groups [web] host1 host2 [database] host2 host3 [site:children] web database David Murphy, October 2015

Slide 33

Slide 33 text

Out-of-scope Dynamic inventory (e.g., Amazon EC2) David Murphy, October 2015

Slide 34

Slide 34 text

Our inventory [development] my_vagrant_host ansible_sudo=yes [production] wcmcr.schwuk.com David Murphy, October 2015

Slide 35

Slide 35 text

demo David Murphy, October 2015

Slide 36

Slide 36 text

David Murphy, October 2015

Slide 37

Slide 37 text

Playbooks David Murphy, October 2015

Slide 38

Slide 38 text

Sample playbook --- - hosts: webservers remote_user: root tasks: - name: ensure apache is at the latest version yum: pkg=httpd state=latest - name: write the apache config file template: src=/srv/httpd.j2 dest=/etc/httpd.conf - hosts: databases remote_user: root tasks: - name: ensure postgresql is at the latest version yum: name=postgresql state=latest - name: ensure that postgresql is started service: name=postgresql state=running David Murphy, October 2015

Slide 39

Slide 39 text

Modules David Murphy, October 2015

Slide 40

Slide 40 text

yum Manages packages with the yum package manager1 - yum: pkg=httpd state=latest 1 http://docs.ansible.com/ansible/yum_module.html David Murphy, October 2015

Slide 41

Slide 41 text

template Create/modify files using variables2 - template: src=/srv/httpd.j2 dest=/etc/httpd.conf 2 http://docs.ansible.com/ansible/template_module.html David Murphy, October 2015

Slide 42

Slide 42 text

service Controls services on the remote host(s)3 - service: name=postgresql state=running 3 http://docs.ansible.com/ansible/service_module.html David Murphy, October 2015

Slide 43

Slide 43 text

Modules, modules, modules → command, get_url → apt, apt_key, apt_repository → digital_ocean, linode, ec2, gce, docker → irc, slack, hipchat, mail → mysql_db, mysql_user → git, subversion David Murphy, October 2015

Slide 44

Slide 44 text

But where are... → Apache/NGINX? → PHP? David Murphy, October 2015

Slide 45

Slide 45 text

install → configure → control David Murphy, October 2015

Slide 46

Slide 46 text

apt → template → service David Murphy, October 2015

Slide 47

Slide 47 text

Deploying WordPress David Murphy, October 2015

Slide 48

Slide 48 text

Famous 5-Minute Install4 → download/unzip → create database → edit config → upload 4 http://codex.wordpress.org/InstallingWordPress#Famous5-Minute_Install David Murphy, October 2015

Slide 49

Slide 49 text

A better example → install packages → create database, then database user → download/unzip → edit config → configure webserver → ensure webserver is running David Murphy, October 2015

Slide 50

Slide 50 text

First, some variables vars: db_name: wcmcr db_user: wcmcr db_password: correcthorsebatterystaple site_name: wcmcr.schwuk.com site_alias: wcmcr David Murphy, October 2015

Slide 51

Slide 51 text

Install packages - name: Install mysql-server apt: name=mysql-server state=present↩ update_cache=yes cache_valid_time=3600 - name: Install apache2 apt: name=apache2 state=present - name: Install libapache2-mod-php5 apt: name=libapache2-mod-php5 state=present - name: Install php5-mysql apt: name=php5-mysql state=present - name: Install python-mysqldb apt: name=python-mysqldb state=present - name: Install python-httplib2 apt: name=python-httplib2 state=present David Murphy, October 2015

Slide 52

Slide 52 text

Install packages take two - name: Install required packages apt: name: "{{ item }}" state: present update_cache: yes cache_valid_time: 3600 with_items: - mysql-server - apache2 - libapache2-mod-php5 - php5-mysql - python-mysqldb - python-httplib2 David Murphy, October 2015

Slide 53

Slide 53 text

Database setup - name: Create WordPress database mysql_db: name: "{{ db_name }}" state: present - name: Create WordPress user mysql_user: name: "{{ db_user }}" password: "{{ db_password }}" priv: "{{ db_name }}.*:ALL" state: present David Murphy, October 2015

Slide 54

Slide 54 text

Install WordPress - name: Create directory for WordPress file: path: /srv/{{ site_alias }} state: directory mode: 0755 - name: Get WordPress get_url: url: http://wordpress.org/latest.tar.gz dest: /tmp/wordpress.tar.gz - name: Extract WordPress unarchive: src: /tmp/wordpress.tar.gz dest: /srv/{{ site_alias }} copy: no David Murphy, October 2015

Slide 55

Slide 55 text

Edit config - name: Generate Keys and Salts uri: url: https://api.wordpress.org/secret-key/1.1/salt/ dest: /srv/{{ site_alias }}/keys.php creates: /srv/{{ site_alias }}/keys.php - name: Configure WordPress template: src: wp-config.php dest: /srv/{{ site_alias }}/wordpress/wp-config.php owner: root group: root mode: 0644 David Murphy, October 2015

Slide 56

Slide 56 text

wp-config.php template

Slide 57

Slide 57 text

Configure webserver - name: Configure webserver template: src: apache-site.conf dest: /etc/apache2/sites-available/{{ site_alias }}.conf owner: root group: root mode: 0644 notify: - restart apache - name: Disable default site_alias command: a2dissite 000-default - name: Enable WordPress site command: a2ensite {{ site_alias }} David Murphy, October 2015

Slide 58

Slide 58 text

Handlers handlers: - name: restart apache service: name=apache2 state=restarted David Murphy, October 2015

Slide 59

Slide 59 text

demo David Murphy, October 2015

Slide 60

Slide 60 text

David Murphy, October 2015

Slide 61

Slide 61 text

David Murphy, October 2015

Slide 62

Slide 62 text

Next steps David Murphy, October 2015

Slide 63

Slide 63 text

Questions? David Murphy, October 2015