大网数据广告活动
服务器运维问答

How to enumerate hostvars into a role setting in Ansible

· 52CE 我爱测

So I'm using a role to configure my postgresql server. That tole has a way of specifying server users & databases, like so: postgresql_users: - name: baz My hosts.yaml: all: hosts: children: django: hosts: django_1: username: dja…

问题原文

原帖链接 · velis · CC BY-SA 4.0

So I'm using a role to configure my postgresql server. That tole has a way of specifying server users & databases, like so:

postgresql_users:
  - name: baz

My hosts.yaml:

all:
  hosts:
  children:
    django:
      hosts:
        django_1:
          username: django1
          userpass: django1_pass
        django_2:
          username: django2
          userpass: django2_pass
    db:
      hosts:
        db1:

Any my playbook.yaml:

- hosts: db
  become: true
  roles:
    - role: anxs.postgresql

What I want to do with this information is to get all django hosts and get their username & userpass variables.

Then, from this information compose the postgresql_users variable as if it were written by hand (relevant (desired result) section of hosts.yaml below):

db:
  hosts:
    db1:
      postgresql_users:
        - name: django1
          password: django1_pass
          encrypted: true
        - name: django2
          password: django2_pass
          encrypted: true

I have found this question dealing with my problem, but unfortunately I'm too new to Ansible to really understand what's being suggested there.

So how can I make an appropriate iteration that would compose my postgresql_users variable as I want it?

已采纳解答

原帖链接 · Vladimir Botka · CC BY-SA 4.0

Q: "Get all django hosts and get their username & userpass variables."

A: It's possible to create the variable in the first play and use it later. For example

- hosts: all
  tasks:
    - set_fact:
        psql_users: "{{ groups['django']|
                        map('extract', hostvars)|
                        list|
                        json_query('[].{name: username,
                                        password: userpass,
                                        encrypted: `true`}') }}"
      delegate_to: localhost
      run_once: true

- hosts: db
  tasks:
    - debug:
        var: psql_users

gives

ok: [db1] => {
    "psql_users": [
        {
            "encrypted": true, 
            "name": "django1", 
            "password": "django1_pass"
        }, 
        {
            "encrypted": true, 
            "name": "django2", 
            "password": "django2_pass"
        }
    ]
}

It's also possible to use template and create an inventory file. For example with the template

$ cat hosts.j2
db:
  hosts:
    db1:
      postgresql_users:
{{ psql_users|to_nice_yaml|indent(8,true) }}

the task

- template:
    src: hosts.j2
    dest: hosts2

creates inventory

$ cat hosts2
db:
  hosts:
    db1:
      postgresql_users:
        -   encrypted: true
            name: django1
            password: django1_pass
        -   encrypted: true
            name: django2
            password: django2_pass

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-01-13)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。