linux Ansible -如何获取字典列表中变量的值?

yk9xbfzb  于 2023-03-29  发布在  Linux
关注(0)|答案(2)|浏览(166)

我需要使用模块ansible.builtin.user获取新创建的用户的$HOME路径的值。

---
- name: Create users 
  vars:
    users:
      user1:
        passwd: 
        uid: '1111'
        comment: 'Test User'
        shell: '/bin/zsh'
      user2:
        passwd:
        uid: '1111'
        comment: 'Test User'
        shell: '/bin/zsh'

  tasks:

  - name: Create user "{{ item.key }}"
    ansible.builtin.user:
      name: "{{ item.key }}"
      comment: "{{ item.value.comment }}"
      uid: "{{ item.value.uid }}"
      shell: "{{ item.value.shell }}"
      password: "{{ item.value.passwd }}"
    loop: "{{ lookup('dict', users, wantlist=True) }}"
    register: main_user

    - name: debug
      debug:
        msg: "The home folder is {{main_user.results.home}}"

这里的问题是变量main_user有以下内容:

ok: [192.168.0.98] => {
    "msg": {
        "changed": true,
        "msg": "All items completed",
        "results": [
            {
                "ansible_loop_var": "item",
                "changed": true,
                "comment": "Test",
                "create_home": true,
                "failed": false,
                "group": 100,
                "home": "/home/user",
                "invocation": {
                    "module_args": {
                        "append": false,
                        "authorization": null,
                        "comment": "Test",
                        "create_home": true,

我不知道如何得到results.home的值。
预期:

ok: [192.168.0.98] => {
    "msg": {
        "changed": true,
        "msg: "The home folder is home/user"

有人能给我点光吗?

50pmv0ei

50pmv0ei1#

例如,给定字典 users

users:
    alice: {comment: Alice, shell: /bin/sh, uuid: 1004}
    bob: {comment: Bob, shell: /bin/sh, uuid: 1007}

创建用户。模块 user 只需要参数 name。您可以通过filer default(omit) 使其他参数可选

- user:
        name: "{{ item.key }}"
        comment: "{{ item.value.comment|d(omit) }}"
        uid: "{{ item.value.uid|d(omit) }}"
        shell: "{{ item.value.shell|d(omit) }}"
      loop: "{{ users|dict2items }}"
      register: user

看一下注册的变量 user

  • 字典项:循环项
  • 字典调用:模块的参数,包括默认值
  • 模块返回的结果

您可以选择所需的任何信息。

name_home: "{{ user.results|json_query('[].[name, home]') }}"

给予

name_home:
  - [alice, /home/alice]
  - [bob, /home/bob]

创建一个字典

name_home_dict: "{{ dict(user.results|json_query('[].[name, home]')) }}"

给予

name_home_dict:
    alice: /home/alice
    bob: /home/bob

现在搜索用户的家已经很简单了。就像您可以创建其他字典一样。例如,

name_uid_dict:
    alice: 1004
    bob: 1005
name_shell_dict:
    alice: /bin/sh
    bob: /bin/sh

,也可以创建具有多个属性的字典

name_attr: "{{ dict(user.results|json_query(name_attr_query)) }}"
    name_attr_query: '[].[name, {home: home, uid: uid, shell: shell}]'

给予

name_attr:
    alice:
      home: /home/alice
      shell: /bin/sh
      uid: 1004
    bob:
      home: /home/bob
      shell: /bin/sh
      uid: 1005

完整的测试行动手册示例

- hosts: all

  vars:

    users:
      alice:
        comment: Alice
        shell: /bin/sh
        uuid: 1004
      bob:
        comment: Bob
        shell: /bin/sh
        uuid: 1007

    name_home: "{{ user.results|json_query('[].[name, home]') }}"
    name_home_dict: "{{ dict(user.results|json_query('[].[name, home]')) }}"
    name_uid_dict: "{{ dict(user.results|json_query('[].[name, uid]')) }}"
    name_shell_dict: "{{ dict(user.results|json_query('[].[name, shell]')) }}"

    name_attr: "{{ dict(user.results|json_query(name_attr_query)) }}"
    name_attr_query: '[].[name, {home: home, uid: uid, shell: shell}]'
      
  tasks:

    - debug:
        var: users|to_yaml

    - user:
        name: "{{ item.key }}"
        comment: "{{ item.value.comment|d(omit) }}"
        uid: "{{ item.value.uid|d(omit) }}"
        shell: "{{ item.value.shell|d(omit) }}"
      loop: "{{ users|dict2items }}"
      register: user
    - debug:
        var: user
      when: debug|d(false)|bool

    - debug:
        var: name_home|to_yaml
    - debug:
        var: name_home_dict
    - debug:
        var: name_uid_dict
    - debug:
        var: name_shell_dict
    - debug:
        var: name_attr
hec6srdp

hec6srdp2#

是个数组。索引它。

msg: "The home folder of the first user is {{main_user.results[0].home}}"

相关问题