Ansible

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

  • February 6, 2022

我在推出我的使用者角色時遇到問題,也許有人可以幫助我。

我有以下稱為“使用者”的 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"}

我必須做什麼才能讓劇本發揮作用?

錯誤資訊

|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,檢查創建和引用它們的部分。

引用自:https://unix.stackexchange.com/questions/689322