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

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

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

问题

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

  1. - name: 添加WP管理员用户
  2. command: wp user create "{{ item.login }}" "{{ item.email }}"
  3. --role="{{ item.role }}"
  4. --user_pass="{{ item.pwd }}"
  5. --first_name="{{ item.first }}"
  6. --last_name="{{ item.last }}"
  7. --user_nicename="{{ item.nice }}"
  8. --nickname="{{ item.nick }}"
  9. --display_name="{{ item.display }}"
  10. args:
  11. chdir: "/var/www/{{ domain_name }}"
  12. become: yes
  13. become_user: www-data
  14. with_items:
  15. - { login: user1, email: email1@domain.tld, role: administrator, pwd: XXX, first: John, last: Doe, nice: John, nick: John, display: John }
  16. - { login: user2, email: email2@domain.tld, role: administrator, pwd: YYY, first: Mark, last: Twain, nice: Mark, nick: Mark, display: Mark }

这是group_vars/all.yml

  1. domain_name: "domain.tld/dev"

这是清单文件

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

最后这是我的剧本:

  1. - name: 设置
  2. hosts: servers
  3. gather_facts: false
  4. become: true
  5. roles:
  6. - wpadmin

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

  1. domain_name:
  2. - "domain.tld/dev"
  3. - "domain.tld/qa"

然后将args行更改为:

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

但是出现错误:

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

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

  1. - name: Add WP admin users
  2. command: wp user create "{{ item.login }}" "{{ item.email }}"
  3. --role="{{ item.role }}"
  4. --user_pass="{{ item.pwd }}"
  5. --first_name="{{ item.first }}"
  6. --last_name="{{ item.last }}"
  7. --user_nicename="{{ item.nice }}"
  8. --nickname="{{ item.nick }}"
  9. --display_name="{{ item.display }}"
  10. args:
  11. chdir: "/var/www/{{ domain_name }}"
  12. become: yes
  13. become_user: www-data
  14. with_items:
  15. - { login: user1, email: email1@domain.tld, role: administrator, pwd:XXX, first: John, last: Doe, nice: John, nick: John, display: John }
  16. - { 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

  1. domain_name: "domain.tld/dev"

and this is the inventory file

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

and finally this is my playbook:

  1. - name: Setup
  2. hosts: servers
  3. gather_facts: false
  4. become: true
  5. roles:
  6. - 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:

  1. domain_name:
  2. - "domain.tld/dev"
  3. - "domain.tld/qa"

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

  1. 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(复数形式)

  1. - name: 添加WP管理员用户
  2. vars:
  3. wp_users:
  4. - { login: user1, email: email1@domain.tld, role: administrator, pwd: XXX, first: John, last: Doe, nice: John, nick: John, display: John }
  5. - { login: user2, email: email2@domain.tld, role: administrator, pwd: YYY, first: Mark, last: Twain, nice: Mark, nick: Mark, display: Mark }
  6. domain_names:
  7. - domain.tld
  8. command: wp user create "{{ item.0.login }}" "{{ item.0.email }}"
  9. --role="{{ item.0.role }}"
  10. --user_pass="{{ item.0.pwd }}"
  11. --first_name="{{ item.0.first }}"
  12. --last_name="{{ item.0.last }}"
  13. --user_nicename="{{ item.0.nice }}"
  14. --nickname="{{ item.0.nick }}"
  15. --display_name="{{ item.0.display }}"
  16. args:
  17. chdir: "/var/www/{{ item.1 }}"
  18. become: yes
  19. become_user: www-data
  20. 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)

  1. - name: Add WP admin users
  2. vars:
  3. wp_users:
  4. - { login: user1, email: email1@domain.tld, role: administrator, pwd: XXX, first: John, last: Doe, nice: John, nick: John, display: John }
  5. - { login: user2, email: email2@domain.tld, role: administrator, pwd: YYY, first: Mark, last: Twain, nice: Mark, nick: Mark, display: Mark }
  6. command: wp user create "{{ item.0.login }}" "{{ item.0.email }}"
  7. --role="{{ item.0.role }}"
  8. --user_pass="{{ item.0.pwd }}"
  9. --first_name="{{ item.0.first }}"
  10. --last_name="{{ item.0.last }}"
  11. --user_nicename="{{ item.0.nice }}"
  12. --nickname="{{ item.0.nick }}"
  13. --display_name="{{ item.0.display }}"
  14. args:
  15. chdir: "/var/www/{{ item.1 }}"
  16. become: yes
  17. become_user: www-data
  18. 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:

确定