gpt4 book ai didi

ansible - ansible 中的 JSON 文件列表

转载 作者:行者123 更新时间:2023-12-04 10:03:31 25 4
gpt4 key购买 nike

有一种方法可以获取 JSON 文件列表,每个文件都代表 Ansible 中 vars.yml 文件中的特定用户。

例子:

-users:

- username: jsmith
project_role: administrators
full_name: John Smith
email: johnsmith@mail.com
- username: pmorrison
project_role: developer
full_name: Paul Morrison
email: paulmorrison@mail.com

例如,我特别想将单个用户设计为单个 json 文件。 John smith 的一个 json 文件包含他的所有信息,一个 paul morrison 的 json 文件包含他的所有信息,依此类推。

谢谢

最佳答案

这可以通过字典中的数据轻松解决。例如

shell> cat user.d/jsmith.yml
jsmith:
project_role: administrators
full_name: John Smith
email: johnsmith@mail.com

shell> cat user.d/pmorrison.yml
pmorrison:
project_role: developer
full_name: Paul Morrison
email: paulmorrison@mail.com

剧本
shell> cat pb.yml 
- hosts: localhost
tasks:
- include_vars:
dir: user.d
name: users
- debug:
var: users


    "users": {
"jsmith": {
"email": "johnsmith@mail.com",
"full_name": "John Smith",
"project_role": "administrators"
},
"pmorrison": {
"email": "paulmorrison@mail.com",
"full_name": "Paul Morrison",
"project_role": "developer"
}
}

如果需要,可以创建列表。例如
    - set_fact:
users_list: "{{ users_list|d([]) +
[{'username': item.0}|combine(item.1)] }}"
with_together:
- "{{ users.keys()|list }}"
- "{{ users.values()|list }}"
- debug:
var: users_list


    "users_list": [
{
"email": "johnsmith@mail.com",
"full_name": "John Smith",
"project_role": "administrators",
"username": "jsmith"
},
{
"email": "paulmorrison@mail.com",
"full_name": "Paul Morrison",
"project_role": "developer",
"username": "pmorrison"
}
]

关于ansible - ansible 中的 JSON 文件列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61709817/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com