Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ansible中的JSON文件列表_Ansible - Fatal编程技术网

ansible中的JSON文件列表

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:

有一种方法可以获得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
    - 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
给予


到目前为止你尝试了什么?