我想编写一个如果用户存在的剧本改变它的pw。 该剧本应该能够取得n个用户并改变这些用户的权重。Ansible中的循环使用寄存器
目前我有问题,因为循环,当我是空的,我尝试使用with_items:{{user_exists.results}}但这是不知何故无法正常工作。 (http://docs.ansible.com/ansible/playbooks_loops.html#using-register-with-a-loop)
我做错了什么?
BR, Numblesix
---
-
become: true
become_method: sudo
hosts: xetest
name: "Updates the password of given User if exists"
tasks:
-
ignore_errors: true
name: "Check if User exists"
register: user_exists
shell: "grep -q {{ item.key }} /etc/passwd &>/dev/null"
with_dict: "{{ users }}"
-
debug:
var: user_exists
-
debug:
msg: "User name is {{ item.key }} and hash is {{ item.value.passwd}} and return code is: "
with_dict: "{{ users }}"
-
debug:
var: user_exists
with_items: "{{user_exists.results }}"
-
name: "updating password for given User"
user: "name={{ item.key }} update_password=always password={{ item.value.passwd}} createhome=no"
when: user_exists.rc == 0
with_dict: "{{ users }}"
with_items: "{{ user_exists.results }}"
vars:
users:
foo:
passwd: $6$random_salt$12A.ar9eNDsgmds3leKoCDZPmq7OHLvhBtQg/Q3K2G/3yeEa/r8Ou4DxJpN6vzccewugvZt7IkfCbHFF2i.QU.
导致错误!
duplicate loop in task: items
WITHOUT with_items: “{{user_exists.results}}” 即时得到这个错误
"failed": true, "msg": "The conditional check 'user_exists.rc == 0' failed.
The error was: error while evaluating conditional (user_exists.rc == 0):
'dict object' has no attribute 'rc'
哇它的工作:)你能解释一点点吗?我使用>/dev/null获得了不同的rc,但生病再次检查。我的代码现在看起来像检查用户是否存在+ - name:“更新给定用户的密码” user:“name = {{item.item .key}} update_password = always password = {{item.item.value.passwd}} createhome = no“ when:item.rc == 0 #with_dict:”{{users}}“ with_items:”{{ user_exists.results}}“ – Sandro
嗨@Sandro,我不确定你想让我解释更多的部分。 –