- Learn Ansible
- Russ McKendrick
- 1404字
- 2025-04-04 16:37:53
Running the WordPress playbook
To run the playbook and install WordPress, we need a few things, starting with the inventory file called production:
box1 ansible_host=192.168.50.5.nip.io
[wordpress]
box1
[wordpress:vars]
ansible_connection=ssh
ansible_user=vagrant
ansible_private_key_file=~/.ssh/id_rsa
host_key_checking=False
As you can see, it takes into account the updated IP address of the Vagrant box we defined at the start of the chapter. Also, we need the playbook itself; site.yml should look as follows:
---
- hosts: wordpress
gather_facts: true
become: yes
become_method: sudo
vars_files:
- group_vars/common.yml
roles:
- roles/stack-install
- roles/stack-config
- roles/wordpress
Now, start the Vagrant box by running one of the following two commands:
$ vagrant up
$ vagrant up --provider=vmware_fusion
Once your Vagrant box is up and running, we can start the playbook run with this command:
$ ansible-playbook -i production site.yml
When the playbook is first executed, you should see something like the following results:
PLAY [wordpress] ***********************************************************************************
TASK [Gathering Facts] *****************************************************************************
ok: [box1]
TASK [roles/stack-install : install the repo packages] *********************************************
changed: [box1] => (item=[u'epel-release', u'https://centos7.iuscommunity.org/ius-release.rpm'])
TASK [roles/stack-install : add the NGINX mainline repo] *******************************************
changed: [box1]
TASK [roles/stack-install : update all of the installed packages] **********************************
changed: [box1]
TASK [roles/stack-install : remove the packages so that they can be replaced] **********************
changed: [box1] => (item=[u'mariadb-libs.x86_64'])
TASK [roles/stack-install : install the stack packages] ********************************************
changed: [box1] => (item=[u'postfix', u'MySQL-python', u'policycoreutils-python', u'nginx', u'mariadb101u', u'mariadb101u-server', u'mariadb101u-config', u'mariadb101u-common', u'mariadb101u-libs', u'php72u', u'php72u-bcmath', u'php72u-cli', u'php72u-common', u'php72u-dba', u'php72u-fpm', u'php72u-fpm-nginx', u'php72u-gd', u'php72u-intl', u'php72u-json', u'php72u-mbstring', u'php72u-mysqlnd', u'php72u-process', u'php72u-snmp', u'php72u-soap', u'php72u-xml', u'php72u-xmlrpc', u'vim-enhanced', u'git', u'unzip'])
TASK [roles/stack-config : add the wordpress user] *************************************************
changed: [box1]
TASK [roles/stack-config : copy the nginx.conf to /etc/nginx/] *************************************
changed: [box1]
TASK [roles/stack-config : create the global directory in /etc/nginx/] *****************************
changed: [box1]
TASK [roles/stack-config : copy the restrictions.conf to /etc/nginx/global/] ***********************
changed: [box1]
TASK [roles/stack-config : copy the wordpress_shared.conf to /etc/nginx/global/] *******************
changed: [box1]
TASK [roles/stack-config : copy the default.conf to /etc/nginx/conf.d/] ****************************
changed: [box1]
TASK [roles/stack-config : copy the www.conf to /etc/php-fpm.d/] ***********************************
changed: [box1]
TASK [roles/stack-config : configure php.ini] ******************************************************
changed: [box1] => (item={u'regexp': u'^;date.timezone =', u'replace': u'date.timezone = Europe/London'})
changed: [box1] => (item={u'regexp': u'^expose_php = On', u'replace': u'expose_php = Off'})
changed: [box1] => (item={u'regexp': u'^upload_max_filesize = 2M', u'replace': u'upload_max_filesize = 20M'})
TASK [roles/stack-config : start php-fpm] **********************************************************
changed: [box1]
TASK [roles/stack-config : start nginx] ************************************************************
changed: [box1]
TASK [roles/stack-config : configure the mariadb bind address] *************************************
changed: [box1]
TASK [roles/stack-config : start mariadb] **********************************************************
changed: [box1]
TASK [roles/stack-config : change mysql root password] *********************************************
changed: [box1] => (item=127.0.0.1)
changed: [box1] => (item=::1)
changed: [box1] => (item=192.168.50.5.nip.io)
changed: [box1] => (item=localhost)
TASK [roles/stack-config : set up .my.cnf file] ****************************************************
changed: [box1]
TASK [roles/stack-config : delete anonymous MySQL user] ********************************************
ok: [box1] => (item=127.0.0.1)
ok: [box1] => (item=::1)
changed: [box1] => (item=192.168.50.5.nip.io)
changed: [box1] => (item=localhost)
TASK [roles/stack-config : remove the MySQL test database] *****************************************
changed: [box1]
TASK [roles/stack-config : set the selinux allowing httpd_t to be permissive is required] **********
changed: [box1]
TASK [roles/wordpress : download wp-cli] ***********************************************************
changed: [box1]
TASK [roles/wordpress : update permissions of wp-cli to allow anyone to execute it] ****************
changed: [box1]
TASK [roles/wordpress : create the wordpress database] *********************************************
changed: [box1]
TASK [roles/wordpress : create the user for the wordpress database] ********************************
changed: [box1] => (item=127.0.0.1)
ok: [box1] => (item=::1)
ok: [box1] => (item=192.168.50.5.nip.io)
ok: [box1] => (item=localhost)
TASK [roles/wordpress : are the wordpress files already there?] ************************************
ok: [box1]
TASK [roles/wordpress : download wordpresss] *******************************************************
changed: [box1]
TASK [roles/wordpress : set the correct permissions on the homedir] ********************************
changed: [box1]
TASK [roles/wordpress : is wordpress already configured?] ******************************************
ok: [box1]
TASK [roles/wordpress : configure wordpress] *******************************************************
changed: [box1]
TASK [roles/wordpress : do we need to install wordpress?] ******************************************
fatal: [box1]: FAILED! => {"changed": true, "cmd": "/usr/local/bin/wp core is-installed", "delta": "0:00:00.364987", "end": "2018-03-04 20:22:16.659411", "msg": "non-zero return code", "rc": 1, "start": "2018-03-04 20:22:16.294424", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
...ignoring
TASK [roles/wordpress : install wordpress if needed] ***********************************************
changed: [box1]
TASK [roles/wordpress : do we need to install the plugins?] ****************************************
failed: [box1] (item=jetpack) => {"changed": true, "cmd": "/usr/local/bin/wp plugin is-installed jetpack", "delta": "0:00:01.366121", "end": "2018-03-04 20:22:20.175418", "item": "jetpack", "msg": "non-zero return code", "rc": 1, "start": "2018-03-04 20:22:18.809297", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
failed: [box1] (item=wp-super-cache) => {"changed": true, "cmd": "/usr/local/bin/wp plugin is-installed wp-super-cache", "delta": "0:00:00.380384", "end": "2018-03-04 20:22:21.035274", "item": "wp-super-cache", "msg": "non-zero return code", "rc": 1, "start": "2018-03-04 20:22:20.654890", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
failed: [box1] (item=wordpress-seo) => {"changed": true, "cmd": "/usr/local/bin/wp plugin is-installed wordpress-seo", "delta": "0:00:00.354021", "end": "2018-03-04 20:22:21.852955", "item": "wordpress-seo", "msg": "non-zero return code", "rc": 1, "start": "2018-03-04 20:22:21.498934", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
failed: [box1] (item=wordfence) => {"changed": true, "cmd": "/usr/local/bin/wp plugin is-installed wordfence", "delta": "0:00:00.357012", "end": "2018-03-04 20:22:22.673549", "item": "wordfence", "msg": "non-zero return code", "rc": 1, "start": "2018-03-04 20:22:22.316537", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
failed: [box1] (item=nginx-helper) => {"changed": true, "cmd": "/usr/local/bin/wp plugin is-installed nginx-helper", "delta": "0:00:00.346194", "end": "2018-03-04 20:22:23.389176", "item": "nginx-helper", "msg": "non-zero return code", "rc": 1, "start": "2018-03-04 20:22:23.042982", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
...ignoring
TASK [roles/wordpress : set a fact if we don't need to install the plugins] ************************
skipping: [box1]
TASK [roles/wordpress : set a fact if we need to install the plugins] ******************************
ok: [box1]
TASK [roles/wordpress : install the plugins if we need to or ignore if not] ************************
changed: [box1] => (item=jetpack)
changed: [box1] => (item=wp-super-cache)
changed: [box1] => (item=wordpress-seo)
changed: [box1] => (item=wordfence)
changed: [box1] => (item=nginx-helper)
TASK [roles/wordpress : do we need to install the theme?] ******************************************
fatal: [box1]: FAILED! => {"changed": true, "cmd": "/usr/local/bin/wp theme is-installed sydney", "delta": "0:00:01.451018", "end": "2018-03-04 20:23:02.227557", "msg": "non-zero return code", "rc": 1, "start": "2018-03-04 20:23:00.776539", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
...ignoring
TASK [roles/wordpress : set a fact if we don't need to install the theme] **************************
skipping: [box1]
TASK [roles/wordpress : set a fact if we need to install the theme] ********************************
ok: [box1]
TASK [roles/wordpress : install the theme if we need to or ignore if not] **************************
changed: [box1]
RUNNING HANDLER [roles/stack-config : restart nginx] ***********************************************
changed: [box1]
RUNNING HANDLER [roles/stack-config : restart php-fpm] *********************************************
changed: [box1]
PLAY RECAP *****************************************************************************************
box1 : ok=42 changed=37 unreachable=0 failed=0
As you can see during the playbook, we have fatal errors for the check to see whether we need to install WordPress, and also for the plugin and theme checks, as we have accounted for these in the tasks the playbook ran as expected and installed the software stack, WordPress, plugins, and theme.
Rerunning the playbook gives us the following results for the sections we errored previously:
TASK [roles/wordpress : do we need to install wordpress?] ******************************************
changed: [box1]
TASK [roles/wordpress : install wordpress if needed] ***********************************************
skipping: [box1]
TASK [roles/wordpress : do we need to install the plugins?] ****************************************
changed: [box1] => (item=jetpack)
changed: [box1] => (item=wp-super-cache)
changed: [box1] => (item=wordpress-seo)
changed: [box1] => (item=wordfence)
changed: [box1] => (item=nginx-helper)
TASK [roles/wordpress : set a fact if we don't need to install the plugins] ************************
ok: [box1]
TASK [roles/wordpress : set a fact if we need to install the plugins] ******************************
skipping: [box1]
TASK [roles/wordpress : install the plugins if we need to or ignore if not] ************************
skipping: [box1] => (item=jetpack)
skipping: [box1] => (item=wp-super-cache)
skipping: [box1] => (item=wordpress-seo)
skipping: [box1] => (item=wordfence)
skipping: [box1] => (item=nginx-helper)
TASK [roles/wordpress : do we need to install the theme?] ******************************************
changed: [box1]
TASK [roles/wordpress : set a fact if we don't need to install the theme] **************************
ok: [box1]
TASK [roles/wordpress : set a fact if we need to install the theme] ********************************
skipping: [box1]
TASK [roles/wordpress : install the theme if we need to or ignore if not] **************************
skipping: [box1]
PLAY RECAP *****************************************************************************************
box1 : ok=34 changed=3 unreachable=0 failed=0
Now that WordPress is installed, we should be able to access in a browser by going to http://192.168.50.5.nip.io/. As you can see here, the theme we defined is running rather than the WordPress default theme:

Also, if you go to http://192.168.50.5.nip.io/wp-admin/, you should be able to log in to WordPress using the username and password we defined:
- Username: ansible
- Password: password
Once logged in, you should see a few messages about the plugins we installed during the playbook run needing to be configured:

Feel free to have a play with the WordPress installation; also, you can remove the Vagrant box by running:
$ vagrant destroy
Then bring up a fresh copy and redeploy it using the commands at that start of this section.