ansible中的JSON文件列表
有一种方法可以获得JSON文件列表,每个文件在Ansible的vars.yml文件中代表一个特定的用户 例如:ansible中的JSON文件列表,ansible,Ansible,有一种方法可以获得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:
-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
- 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
剧本
shell> cat pb.yml
- hosts: localhost
tasks:
- include_vars:
dir: user.d
name: users
- debug:
var: users
给予
如果需要,可以创建列表。比如说
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
- 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
给予
到目前为止你尝试了什么?