Plugin activation task during vagrant provisioning

Hi there,

First of all thanks for your great tools. Amazing work!

In order to better understand ansible and trellis in general, I tried to add a task to activate wordpress plugins.
I managed to do it but I’d like to chalenge this solution because I’m totally new to ansible and trellis. I’d like to hear from you to know if you have better solution, suggestions, etc.

Here is how I do it:

  • I’ve added this task to roles/wordpress-install/tasks/main.yml
- name: Activate Plugins
  command: wp plugin activate
           {{ item.1.name }}
           --allow-root
  args:
    chdir: "{{ www_root }}/{{ item.0.site_id }}/current/"
  register: wp_activate_results
  with_subelements:
    - wordpress_extra
    - plugins
  when: item.1.activate == True
  changed_when: "'Success: Plugin' in wp_activate_results.stdout"

just after the existing wordpress-install tasks Install WP and Install WP Multisite.

  • I also created a new file inside group_vars/development / called wordpress_extra.yml:
# Documentation: https://roots.io/trellis/docs/local-development-setup/
wordpress_extra:
  wfv2.com:
    site_id: wfv2.com
    plugins:
      - { name: 'plugin1', activate: true }
      - { name: 'plugin2', activate: true }

Of course those plugins are also declared inside composer.json of the wfv2.com project.

I think the ugly thing is the way I manage the loop with subelements, a new file (wordpress_extra) and site_id which repeat the key name.

Alternative :
Use the following command as a task. With this one, no need to create wordpress_extra.yml :
wp plugin activate --all --allow-root
but I’d like to keep the oportunity to install a plugin but not activate during provisioning

Any suggestion is welcome!

Your solution looks pretty good for the most part.

Not sure if there’s a reason for creating a separate wordpress_extra.yml file but you can just add that new plugins object to the existing WP site in wordpress_sites.yml. You can customize that object by adding any additional keys and it won’t break anything.

This is exactly the point. I tried to append a plugins node at the end of wordpress_sites but I didn’t find the way to iterate through both plugins entries and main key name (example.com or whatever site id) for the chdir condition. None of with_dict or with_items give me a solution for that. Thats why I used an additional file. If you can advise on this, it’s with pleaser :slightly_smiling:
By the way, I also realise that I can get rid of the activate attribute: plugins are installed though composer and activated by my new task, then if I do want a plugin to be installed but not activated I just need to omit it in the plugin list in wordpress_extra.yml.

Unless I’m missing something, your solution with wordpress_extra mirrors the same structure. So you can just swap that to wordpress_sites.

Here’s what you’d want:

- name: Activate Plugins
  command: wp plugin activate {{ item.1.name }} --allow-root
  args:
    chdir: "{{ www_root }}/{{ item.0.key}}/current/"
  register: wp_activate_results
  with_subelements:
    - wordpress_sites
    - plugins
  when: item.1.activate == True
  changed_when: "'Success: Plugin' in wp_activate_results.stdout"

Only change is item.0.site_id to either item.0.key or item.key. Hopefully those would work. Maybe you already tried those though but I’d assume there’s some way to do this.

I’d like to say “Ho yeah! You found it” but I already tried this and it doesn’t work:
fatal: [default]: FAILED! => {"failed": true, "msg": "ERROR! 'dict object' has no attribute 'key'"} :frowning:
I think this is because with_subelements creates only lists not dict. I cannot find a way to retrieve the site name (the key) except using the site_id attribute in my extra file.

As you mentioned, there’s some way to do this. Its definitively not a blocking issue. I’m currently facing another kind of problem… I’m about to create a new thread.