Learn Ansible
上QQ阅读APP看书,第一时间看更新

WordPress plugins and theme installation

The last part of our WordPress installation is downloading and installing the plugins and theme files we defined in the wordpress.plugins and wordpress.theme variables.

Let's start with the tasks that install the plugins, so we don't end up rerunning the task that installs the plugins. When we need to, we will be building a little logic into the tasks. First of all, we run a task to see if all of the plugins are already installed:

- name: do we need to install the plugins?
shell: "{{ wp_cli.path }} plugin is-installed {{ item }}"
args:
chdir: "{{ wordpress_system.home }}"
become_user: "{{ wordpress_system.user }}"
become: true
with_items: "{{ wordpress.plugins }}"
ignore_errors: yes
register: wp_plugin_installed

If the plugins are not installed, then this task should fail, which is why we have the ignore_errors in there. As you can see, we are registering the results of the entire task, because if you remember we are installing several plugins, as wp_plugin_installed. The next two tasks take the results of wp_plugin_installed and use the setfact module to set a fact:

- name: set a fact if we don't need to install the plugins
set_fact:
wp_plugin_installed_skip: true
when: wp_plugin_installed.failed is undefined

- name: set a fact if we need to install the plugins
set_fact:
wp_plugin_installed_skip: false
when: wp_plugin_installed.failed is defined

As you can see, we are setting wp_theme_installed_skip to be true or false: if the fact is set to false, then the next task will loop through installing the plugins:

- name: install the plugins if we need to or ignore if not
shell: "{{ wp_cli.path }} plugin install {{ item }} --activate"
args:
chdir: "{{ wordpress_system.home }}"
become_user: "{{ wordpress_system.user }}"
become: true
with_items: "{{ wordpress.plugins }}"
when: wp_plugin_installed_skip == false

If we add another plugin to the list but leave the others in place, it will show an error, causing the plugin to be installed. We are going to be using this same logic to figure out whether the theme file we have defined as wordpress.theme needs to be installed:

- name: do we need to install the theme?
shell: "{{ wp_cli.path }} theme is-installed {{ wordpress.theme }}"
args:
chdir: "{{ wordpress_system.home }}"
become_user: "{{ wordpress_system.user }}"
become: true
ignore_errors: yes
register: wp_theme_installed

- name: set a fact if we don't need to install the theme
set_fact:
wp_theme_installed_skip: true
when: wp_theme_installed.failed == false

- name: set a fact if we need to install the theme
set_fact:
wp_theme_installed_skip: false
when: wp_theme_installed.failed == true

- name: install the theme if we need to or ignore if not
shell: "{{ wp_cli.path }} theme install {{ wordpress.theme }} --activate"
args:
chdir: "{{ wordpress_system.home }}"
become_user: "{{ wordpress_system.user }}"
become: true
when: wp_theme_installed_skip == false

Now that we have the plugins and theme installed, we can have a go at running our playbook.