1

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: email1@domain.tld, role: administrator, pwd:XXX, first: John, last: Doe, nice: John, nick: John, display: John }
  - { login: user2, email: email2@domain.tld, 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'
Roberto Jobet
  • 153
  • 4
  • 15

1 Answers1

1

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: email1@domain.tld, role: administrator, pwd: XXX, first: John, last: Doe, nice: John, nick: John, display: John }
      - { login: user2, email: email2@domain.tld, 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) }}"
Zeitounator
  • 38,476
  • 7
  • 53
  • 66
  • I added in `group_vars/all.yml` 2 domains: `domain_name: - "domain.tld/dev" - "domain.tld/qa"` (the 2 domain names are in a vertical list) but an error shows up: `dict object has no element 1` – Roberto Jobet Aug 11 '23 at 16:51
  • How should I add the `domain_name` variables? – Roberto Jobet Aug 11 '23 at 17:13
  • 1
    @RobertoJobet Sorry wrote this a bit too fast. In that specific case `with_items` has to be replaced with `loop:` (or eventually with `with_list`). I just fixed my answer. Explanation: `with_items` performs a `flatten` on the list which is not what we want here (i.e. we need a list of lists with 2 elements per top item). Moreover, I fixed a typo in your users vars where a space was missing after `pwd:` for the first object user. – Zeitounator Aug 12 '23 at 09:59
  • Works fine now, thanks for your help! – Roberto Jobet Aug 13 '23 at 17:21