我想从注册结果中获取两个帐户的状态并重命名它们,但Ansible在循环该寄存器时出错。
我循环的变量是:
user_accounts:
- old_name: account1
new_name: account11
- old_name: account2
new_name: account22
我查了账目。
- name: get the state of the users account
win_user:
name: "{{ item.old_name }}"
state: query
loop: "{{ user_accounts }}"
register: accounts_to_rename
- name: debug
debug:
var: accounts_to_rename
调试结果:
{
"accounts_to_rename":{
"changed":false,
"msg":"All items completed",
"results":[
{
"ansible_loop_var":"item",
"changed":false,
"failed":false,
"invocation":{
"module_args":{
"account_disabled":null,
"account_locked":null,
"description":null,
"fullname":null,
"groups":null,
"groups_action":"replace",
"home_directory":null,
"login_script":null,
"name":"account1",
"password":null,
"password_expired":null,
"password_never_expires":null,
"profile":null,
"state":"query",
"update_password":"always",
"user_cannot_change_password":null
}
},
"item":{
"old_name":"account1",
"new_name":"account11"
},
"name":"account1",
"state":"present"
},
{
"ansible_loop_var":"item",
"changed":false,
"failed":false,
"invocation":{
"module_args":{
"account_disabled":null,
"account_locked":null,
"description":null,
"fullname":null,
"groups":null,
"groups_action":"replace",
"home_directory":null,
"login_script":null,
"name":"account2",
"password":null,
"password_expired":null,
"password_never_expires":null,
"profile":null,
"state":"query",
"update_password":"always",
"user_cannot_change_password":null
}
},
"item":{
"old_name":"account2",
"new_name":"account22"
},
"name":"account2",
"state":"present"
}
],
"skipped":false
}
}
然后,我想:
- 将
account1
重命名为account11
- 将
account2
重命名为account22
这基于旧帐户的状态:
- name: W2K19.319_L1_Rename_administrator_account
win_shell: "Rename-LocalUser -Name '{{ item.old_name }}' -NewName '{{ item.new_name }}'"
loop: "{{ user_accounts | flatten(1) }}"
when: accounts_to_rename.results[*].state == present
我得到的错误:
"msg":"条件检查" accounts_to_rename. results [*]. state == present "失败。错误为:模板字符串时发生模板错误:意外的"*"。
如果不使用[*]
,Ansible给出:
"msg":"条件检查" accounts_to_rename. results. state == present "失败。错误为:评估条件时出错(accounts_to_rename. results. state ==存在):"列表对象"没有属性。
1条答案
按热度按时间yr9zkbsy1#
而是在
accounts_to_rename
上循环并通过其item.item.new_name
属性访问新名称。因此: