Search code examples
wordpressansiblewp-cli

Ansible playbook to create WordPress users, using wp-cli tool


Using Ansible ver. core 2.13.3. This is the main task

- name: Add WP admin users
  command: wp user create "{{ item.login }}" "{{ item.email }}"
           --role="{{ item.role }}"
           --user_pass="{{ item.pwd }}"
           --first_name="{{ item.first }}"
           --last_name="{{ item.last }}"
           --user_nicename="{{ item.nice }}"
           --nickname="{{ item.nick }}"
           --display_name="{{ item.display }}"
  args:
      chdir: "/var/www/{{ domain_name }}"
  become: yes
  become_user: www-data
  with_items:
  - { login: user1, email: [email protected], role: administrator, pwd:XXX, first: John, last: Doe, nice: John, nick: John, display: John }
  - { login: user2, email: [email protected], role: administrator, pwd: YYY, first: Mark, last: Twain, nice: Mark, nick: Mark, display: Mark }

This is the group_vars/all.yml

domain_name: "domain.tld/dev"

and this is the inventory file

[servers]
dev.domain.tld ansible_ssh_host=178.xxx.xxx.xxx

and finally this is my playbook:

- name: Setup
  hosts: servers
  gather_facts: false
  become: true
  roles:
  - wpadmin

Everything is working fine. Now I would like to run this same playbook against other websites (Ex. domain.tld/qa, and domain.tld/staging) all sites at once. I tried to add a list of variables like this:

domain_name:
- "domain.tld/dev"
- "domain.tld/qa"

then changing the args line like this: chdir: "/var/www/{{ item.domain_name }}" but an error shows up:

The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'domain_name'

Solution

  • There is no domain_name in the items you declare in your loop as clearly explained by your error message.

    Here is one possible way to fix your task using a list of domains in the original variable.

    Note: once the below modification is made, the var needs to be list even if it contains a single domain. I kept your original var name but for clarity, I suggest you later rename it to domain_names (plural)

    - name: Add WP admin users
      vars:
        wp_users:
          - { login: user1, email: [email protected], role: administrator, pwd: XXX, first: John, last: Doe, nice: John, nick: John, display: John }
          - { login: user2, email: [email protected], role: administrator, pwd: YYY, first: Mark, last: Twain, nice: Mark, nick: Mark, display: Mark }
      command: wp user create "{{ item.0.login }}" "{{ item.0.email }}"
               --role="{{ item.0.role }}"
               --user_pass="{{ item.0.pwd }}"
               --first_name="{{ item.0.first }}"
               --last_name="{{ item.0.last }}"
               --user_nicename="{{ item.0.nice }}"
               --nickname="{{ item.0.nick }}"
               --display_name="{{ item.0.display }}"
      args:
          chdir: "/var/www/{{ item.1 }}"
      become: yes
      become_user: www-data
      loop: "{{ wp_users | product(domain_name) }}"