Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Managing WordPress with Ansible

David Murphy
October 10, 2015

Managing WordPress with Ansible

Talk given at WordCamp Manchester, October 2015

David Murphy

October 10, 2015
Tweet

More Decks by David Murphy

Other Decks in Technology

Transcript

  1. Managing WordPress
    with Ansible
    David Murphy, October 2015

    View Slide

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

    View Slide

  3. David Murphy, October 2015

    View Slide

  4. opinionated
    David Murphy, October 2015

    View Slide

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

    View Slide

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

    View Slide

  7. doing it
    yourself
    David Murphy, October 2015

    View Slide

  8. knowing how to
    do it yourself
    David Murphy, October 2015

    View Slide

  9. automation
    David Murphy, October 2015

    View Slide

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

    View Slide

  11. Choices
    David Murphy, October 2015

    View Slide

  12. Choice #1:
    Software
    David Murphy, October 2015

    View Slide

  13. David Murphy, October 2015

    View Slide

  14. Choice #2:
    Platform
    David Murphy, October 2015

    View Slide

  15. David Murphy, October 2015

    View Slide

  16. they are
    all good
    David Murphy, October 2015

    View Slide

  17. Choice #3:
    Hosting
    David Murphy, October 2015

    View Slide

  18. David Murphy, October 2015

    View Slide

  19. David Murphy, October 2015

    View Slide

  20. they are not
    all good
    David Murphy, October 2015

    View Slide

  21. Choice #4:
    Tool
    David Murphy, October 2015

    View Slide

  22. David Murphy, October 2015

    View Slide

  23. again,
    they are
    all good
    David Murphy, October 2015

    View Slide

  24. David Murphy, October 2015

    View Slide

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

    View Slide

  26. how does it work?
    David Murphy, October 2015

    View Slide

  27. inventory
    playbooks
    roles
    David Murphy, October 2015

    View Slide

  28. inventory
    playbooks
    roles
    David Murphy, October 2015

    View Slide

  29. Inventory
    David Murphy, October 2015

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  35. demo
    David Murphy, October 2015

    View Slide

  36. David Murphy, October 2015

    View Slide

  37. Playbooks
    David Murphy, October 2015

    View Slide

  38. 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

    View Slide

  39. Modules
    David Murphy, October 2015

    View Slide

  40. 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

    View Slide

  41. 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

    View Slide

  42. 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

    View Slide

  43. 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

    View Slide

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

    View Slide

  45. install → configure → control
    David Murphy, October 2015

    View Slide

  46. apt → template → service
    David Murphy, October 2015

    View Slide

  47. Deploying
    WordPress
    David Murphy, October 2015

    View Slide

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

    View Slide

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

    View Slide

  50. 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

    View Slide

  51. 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

    View Slide

  52. 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

    View Slide

  53. 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

    View Slide

  54. 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

    View Slide

  55. 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

    View Slide

  56. wp-config.php template
    define('DB_NAME', '{{ db_name }}');
    define('DB_USER', '{{ db_user }}');
    define('DB_PASSWORD', '{{ db_password }}');
    define('DB_HOST', 'localhost');
    define('DB_CHARSET', 'utf8');
    define('DB_COLLATE', '');
    include dirname( __FILE__ ) . '../keys.php';
    $table_prefix = 'wp_';
    define('WP_DEBUG', false);
    if ( !defined('ABSPATH') )
    define('ABSPATH', dirname(__FILE__) . '/');
    require_once(ABSPATH . 'wp-settings.php');
    David Murphy, October 2015

    View Slide

  57. 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

    View Slide

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

    View Slide

  59. demo
    David Murphy, October 2015

    View Slide

  60. David Murphy, October 2015

    View Slide

  61. David Murphy, October 2015

    View Slide

  62. Next
    steps
    David Murphy, October 2015

    View Slide

  63. Questions?
    David Murphy, October 2015

    View Slide