"msg": "|combine 需要字典,得到 AnsibleUndefined"

"msg": "|combine 需要字典,得到 AnsibleUndefined"

我在推出我的用户角色时遇到问题,也许有人可以帮助我。

我有以下 Ansible 角色,称为“用户”

    - name: Create required groups.
      become: yes
      group:
        name: "{{ item.1 }}"
        state: present
      loop: "{{ lookup('subelements', all_required_users|combine(required_users), 'groups', {'skip_missing': True}, wantlist=True) }}"
    
    - name: Create required users.
      become: yes
      user:
        name: "{{ item.key }}"
        state: "{{ item.value.state | default('present') }}"
        append: yes
        create_home: yes
        groups: "{{ item.value.groups | default([]) }}"
        password: "{{ item.value.password }}"
        shell: "{{ item.value.shell | default('/bin/bash') }}"
      loop: "{{ all_required_users | combine(required_users) | dict2items }}"
      when: "'state' not in item.value or item.value.state == 'present'"
    
    - name: Add ssh public key for required users.
      become: yes
      authorized_key:
        user: "{{ item.key }}"
        key: "{{ item.value.ssh_public_key }}"
        state: "{{ item.value.state | default('present') }}"
      loop: "{{ all_required_users | combine(required_users) | dict2items }}"
      when: "'state' not in item.value or item.value.state == 'present'"
    
    - name: Remove unwanted users.
      become: yes
      user:
        name: "{{ item.key }}"
        state: "{{ item.value.state | default('absent') }}"
        remove: true
      loop: "{{ all_required_users | combine(required_users) | dict2items }}"
      when: "'state' in item.value and item.value.state == 'absent'"

以及以下剧本:

    - name: install users
      hosts: 192.168.70.22
      become: true
      roles: 
        - role: users

当 ) 开始运行剧本时,我收到以下消息:

fatal: [192.168.70.22]: FAILED! => {"msg": "|combine expects dictionaries, got AnsibleUndefined"}

我需要做什么才能让剧本发挥作用?

答案1

错误信息

|combine expects dictionaries, got AnsibleUndefined

报道称变量没有定义的因此不能合并的。可以通过以下测试重现

---
- hosts: localhost
  become: false
  gather_facts: false

  vars:

    # all_required_users: ""
    # required_users: ""

  tasks:

  - name: Create error message
    debug:
      msg: "{{ item.1 }}"
    loop: "{{ lookup('subelements', all_required_users | combine(required_users), 'groups', {'skip_missing': True}, wantlist=True) }}"

导致输出为

TASK [Create error message] ********************************
fatal: [localhost]: FAILED! =>
  msg: '|combine expects dictionaries, got AnsibleUndefined'

我需要做什么才能让剧本发挥作用?

这意味着,您必须提供all_required_users和的输入required_users,检查创建和引用它们的部分。

相关内容