Delete sample page

Hi,
I am trying to delete all the Wordpress sample page in the main tasks of wordpress-install
For it i added this code:

- name: Remove sample page command: wp post delete $(wp post list --post_type=page --posts_per_page=1 --post_status=publish --pagename="sample-page" --field=ID --format=ids) --allow-root args: chdir: "{{ www_root }}/{{ item.key }}/current/" with_dict: "{{ wordpress_sites }}"

in roles/wordpress-install/tasks/main.yml of my ansible rep

Unfortunately I got this error:
Error: Parameter errors: unknown --post_type parameter unknown --posts_per_page parameter unknown --post_status parameter unknown --pagename parameter unknown --field parameter unknown --format parameter

The wp cli command work well in an another context…

First question:
Is’it a good practice to put this command in this file ?

Second question:
Can I use this wp cli syntax ? or is’it related to the wp-cli version ?

Thanks a lot for your help :slight_smile:

Try shell instead of command possibly?

- name: Remove sample page
  shell: wp post delete $(wp post list --post_type=page --posts_per_page=1 --post_status=publish --pagename="sample-page" --field=ID --format=ids) --allow-root
  args:
    chdir: "{{ www_root }}/{{ item.key }}/current/"
  with_dict: "{{ wordpress_sites }}"

From the Ansible docs:

The given command will be executed on all selected nodes. It will not be processed through the shell, so variables like $HOME and operations like “<”, “>”, “|”, and “&” will not work (use the shell module if you need these features).

The $(...) syntax is used for command substitution, so I guess your command might need to be run through a shell.

1 Like

Trellis has WP-CLI command parameters on separate lines, might be worth a try?

# example.com/trellis/roles/wordpress-install/tasks/main.yml
- name: Install WP
  command: wp core install
           --allow-root
           --url="{{ site_env.wp_home }}"
           --title="{{ item.value.site_title | default(item.key) }}"
           --admin_user="{{ item.value.admin_user | default('admin') }}"
           --admin_password="{{ vault_wordpress_sites[item.key].admin_password }}"
           --admin_email="{{ item.value.admin_email }}"
  args:
    chdir: "{{ www_root }}/{{ item.key }}/current/"
  register: wp_install_results
  with_dict: "{{ wordpress_sites }}"
  when: item.value.site_install | default(true) and not item.value.multisite.enabled | default(false)
  changed_when: "'WordPress is already installed.' not in wp_install_results.stdout"
1 Like

Verified working:

- name: Delete sample page
  shell: "wp post delete --allow-root $(wp post list --allow-root --post_type=page --posts_per_page=1 --post_status=publish --pagename=\"sample-page\" --field=ID --format=ids)"
  args:
    chdir: "{{ www_root }}/{{ item.key }}/{{ item.value.current_path | default('current') }}/"
  with_dict: "{{ wordpress_sites }}"

See method #3 here on the best way to test this (best way imho anyway).

1 Like

Thanks a lot, that work :slight_smile:

If that can help someone else, I improved a bit the command for remove all default contents:

- name: Delete sample page
  shell: "wp post delete --allow-root $(wp post list --allow-root --post_type=page,post --format=ids) && wp post delete --allow-root $(wp post list --allow-root --post_type=page,post --post_status=trash --format=ids)"
  args:
    chdir: "{{ www_root }}/{{ item.key }}/{{ item.value.current_path | default('current') }}/"
  with_dict: "{{ wordpress_sites }}"

Just one last question: how to exec it only once after the Wordpress installation ? (I am worried to remove all my content if I use provision again)

If you need to only run this once then you might just consider making this its own playbook which you can then run as needed.

1 Like