我正在使用ipa_user模块来设置用户。有变量密码,强制新密码。对于一些用户(当var不在dict中时)我想在迭代中跳过它,但它总是失败。
这是我的剧本片段。 Ansible版本是2.7
任务:
- name: adding ipa users
ipa_user:
name: "{{ item.value.login }}"
state: "{{ item.value.state }}"
givenname: "{{ item.value.givenname }}"
sn: "{{ item.value.surname }}"
mail: "{{ item.value.mail }}"
telephonenumber: "{{ item.value.telephonenumber }}"
title: "{{ item.value.title }}"
password: "{{ item.value.password }}" <<- to be skipped if not found
ipa_host: ipa.gdi.telekom.de
ipa_user: admin
ipa_pass: "{{ ipa_pass }}"
with_dict: "{{ipausers}}"
when: item.key in ipausers.keys()
register: output_ipa_users
日志:
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'password'\n\nThe error appears to have been in '/builds/gitlab/infra/user-management/roles/free-ipa/tasks/main.yml': line 13, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: adding ipa users\n ^ here\n"}
注意:我尝试过:
with_dict: "{{ipausers|default({})}}"
ignore_errors: yes
没有成功
如果您想在未定义密码时完全跳过ipa_user
模块执行,请检查它在when
子句中的存在:
when: item.value.password | default('') | length > 0
如果你想在没有为用户指定密码的情况下执行ipa_user
模块(如果它不存在),请使用模块参数中的omit
placeholder:
password: "{{ item.value.password | default(omit) }}"
注意:您当前的when
子句可以删除。当你循环遍历一个字典并稍后检查循环中的当前键是否是该字典的一部分时,它将始终返回true。
有一个special omit variable可以省略模块参数。
password: "{{ item.value.password|default(omit) }}"
要使剧本或角色可重用,最好在任务中声明模块的所有参数,并默认(省略)不需要的参数。