Ansible playbook 使用 wp-cli 工具创建 WordPress 用户。

huangapple go评论141阅读模式
英文:

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

问题

使用Ansible版本2.13.3核心。
这是主要任务

- name: 添加WP管理员用户
  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 }

这是group_vars/all.yml

domain_name: "domain.tld/dev"

这是清单文件

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

最后这是我的剧本:

- name: 设置
  hosts: servers
  gather_facts: false
  become: true
  roles:
  - wpadmin

一切都正常工作。
现在我想对其他网站(例如domain.tld/qa和domain.tld/staging)同时运行相同的剧本。
我尝试添加变量列表,如下所示:

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

然后将args行更改为:

chdir: "/var/www/{{ item }}"

但是出现错误:

该任务包含一个未定义变量的选项。错误是:“字典对象”没有属性“domain_name”。
英文:

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'

答案1

得分: 1

在你的循环中,根据错误消息明确解释,你声明的项中没有domain_name

以下是修复任务的一种可能方式,使用原始变量中的域名列表。

注意:一旦进行下面的修改,即使它只包含一个域名,变量必须是列表。我保留了你的原始变量名,但为了更清晰起见,建议你以后将其重命名为domain_names(复数形式)

- name: 添加WP管理员用户
  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 }
    domain_names:
      - domain.tld
  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_names) }}"
英文:

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) }}"

huangapple
  • 本文由 发表于 2023年8月11日 05:04:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76879298.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定