Ansible – 使用多个主机创建多个 MySQL 用户

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

Ansible - Create multiple mysql users with multiple hosts

问题

我有以下示例变量:

applications:
   app1:
     app_user:app1
     db_user:db_app1
     db_pass:abc123
     hosts:
       - 10.0.0.25
       - localhost
       - 127.0.0.1
   app2:
     app_user:app2
     db_user:db_app2
     db_pass:abc345
     hosts:
       - 10.0.0.26
       - localhost
       - 127.0.0.1

现在我想使用mysql_users模块创建用户(使用db_user/db_pass/hosts)。

目前我不知道如何解析applications字典并传递db_user/db_pass,另外还要为applications.app*.hosts中的每个主机重复。

原则上,我需要自动生成以下列表:

- { user: "db_app1", password: "abc123" , host: "10.0.0.25" }
- { user: "db_app1", password: "abc123" , host: "localhost" }
- { user: "db_app1", password: "abc123" , host: "127.0.0.1" }
- { user: "db_app2", password: "abc345" , host: "10.0.0.26" }
- { user: "db_app2", password: "abc345" , host: "localhost" }
- { user: "db_app2", password: "abc345" , host: "127.0.0.1" }

解决这个难题的任何提示都将不胜感激。

提前致谢。

英文:

I'm having the following example variables:

applications:
   app1:
     app_user: app1
     db_user: db_app1
     db_pass: abc123
     hosts:
       - 10.0.0.25
       - localhost
       - 127.0.0.1
   app2:
     app_user: app2
     db_user: db_app2
     db_pass: abc345
     hosts:
       - 10.0.0.26
       - localhost
       - 127.0.0.1

Now I want to create the users with the mysql_users module (with db_user/db_pass/hosts).

Right now I've no clue how to parse the applications dictionary and handover db_user/db_pass and additionally repeat for each host in applications.app*.hosts

In principle I would need the following list to be generated automatically:

- { user: "db_app1", password: "abc123" , host: "10.0.0.25" }
- { user: "db_app1", password: "abc123" , host: "localhost" }
- { user: "db_app1", password: "abc123" , host: "127.0.0.1" }
- { user: "db_app2", password: "abc345" , host: "10.0.0.26" }
- { user: "db_app2", password: "abc345" , host: "localhost" }
- { user: "db_app2", password: "abc345" , host: "127.0.0.1" }

Any hints to solve the puzzle would be very much appreciated.

Thx in advance

答案1

得分: 1

使用 dict2items 将字典转换为列表,并与 subelements 一起迭代。例如,

- debug:
    msg: "user: {{ user }} password: {{ pswd }} host: {{ item.1 }}"
  loop: "{{ applications|dict2items|subelements('value.hosts') }}"
  vars:
    user: "{{ item.0.value.db_user }}"
    pswd: "{{ item.0.value.db_pass }}"

给出了缩略

  msg: 'user: db_app1 password: abc123 host: 10.0.0.25'
  msg: 'user: db_app1 password: abc123 host: localhost'
  msg: 'user: db_app1 password: abc123 host: 127.0.0.1'
  msg: 'user: db_app2 password: abc345 host: 10.0.0.26'
  msg: 'user: db_app2 password: abc345 host: localhost'
  msg: 'user: db_app2 password: abc345 host: 127.0.0.1'

完整的测试剧本示例

- hosts: localhost

  vars:

    applications:
      app1:
        app_user: app1
        db_user: db_app1
        db_pass: abc123
        hosts:
          - 10.0.0.25
          - localhost
          - 127.0.0.1
      app2:
        app_user: app2
        db_user: db_app2
        db_pass: abc345
        hosts:
          - 10.0.0.26
          - localhost
          - 127.0.0.1

  tasks:

    - debug:
        msg: "user: {{ user }} password: {{ pswd }} host: {{ item.1 }}"
      loop: "{{ applications|dict2items|subelements('value.hosts') }}"
      loop_control:
        label: "{{ item.0.key }}"
      vars:
        user: "{{ item.0.value.db_user }}"
        pswd: "{{ item.0.value.db_pass }}"

英文:

Use dict2items to convert the dictionary to a list and iterate with subelements. For example,

    - debug:
        msg: "user: {{ user }} password: {{ pswd }} host: {{ item.1 }}"
      loop: "{{ applications|dict2items|subelements('value.hosts') }}"
      vars:
        user: "{{ item.0.value.db_user }}"
        pswd: "{{ item.0.value.db_pass }}"

gives abridged

  msg: 'user: db_app1 password: abc123 host: 10.0.0.25'
  msg: 'user: db_app1 password: abc123 host: localhost'
  msg: 'user: db_app1 password: abc123 host: 127.0.0.1'
  msg: 'user: db_app2 password: abc345 host: 10.0.0.26'
  msg: 'user: db_app2 password: abc345 host: localhost'
  msg: 'user: db_app2 password: abc345 host: 127.0.0.1'

<hr>

<sup>

Example of a complete playbook for testing

- hosts: localhost

  vars:

    applications:
      app1:
        app_user: app1
        db_user: db_app1
        db_pass: abc123
        hosts:
          - 10.0.0.25
          - localhost
          - 127.0.0.1
      app2:
        app_user: app2
        db_user: db_app2
        db_pass: abc345
        hosts:
          - 10.0.0.26
          - localhost
          - 127.0.0.1

  tasks:

    - debug:
        msg: &quot;user: {{ user }} password: {{ pswd }} host: {{ item.1 }}&quot;
      loop: &quot;{{ applications|dict2items|subelements(&#39;value.hosts&#39;) }}&quot;
      loop_control:
        label: &quot;{{ item.0.key }}&quot;
      vars:
        user: &quot;{{ item.0.value.db_user }}&quot;
        pswd: &quot;{{ item.0.value.db_pass }}&quot;

</sup>

huangapple
  • 本文由 发表于 2023年5月25日 06:17:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76327741.html
匿名

发表评论

匿名网友

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

确定