Ansible 如何使用在with_元素中创建并在with_子元素循环中使用的变量
我希望包含任务并使用with_items函数创建的item var创建用户 这是一个大代码的一部分。我不能在这里发布全部代码,但我需要通过多种方式和非常不同的方式创建一个用户 在我的create.yml中,我做了不同的测试来选择要包含的好文件。在这个例子中,我是因为用户的类型是“simple”,所以我包括create_simple.yml文件 例如: 文件create.yml:Ansible 如何使用在with_元素中创建并在with_子元素循环中使用的变量,ansible,Ansible,我希望包含任务并使用with_items函数创建的item var创建用户 这是一个大代码的一部分。我不能在这里发布全部代码,但我需要通过多种方式和非常不同的方式创建一个用户 在我的create.yml中,我做了不同的测试来选择要包含的好文件。在这个例子中,我是因为用户的类型是“simple”,所以我包括create_simple.yml文件 例如: 文件create.yml: - name: 'Create' include_tasks: create_simple.yml user='{
- name: 'Create'
include_tasks: create_simple.yml user='{{ item }}'
with_items:
- '{{ userslist }}'
when: item.simple
下面是userslist的一个示例:
userslist:
- name: 'user1'
simple: False
uid: '1002'
password: 'test'
shell: '/bin/bash'
comment: 'comment user1'
primary: 'groupe1'
gid: '30002'
groups:
- gname: 'secondary'
gid: 30000
remote_groups:
- gname: 'remote'
expires: 1580472000
generate_ssh_key: true
ssh_key_type: 'rsa'
- name: 'test'
simple: True
uid: '1002'
password: 'test'
shell: '/bin/bash'
comment: 'test'
gid: '30003'
groups:
- gname: 'test'
gid: 30000
expires: 1580472000
generate_ssh_key: true
ssh_key_type: 'rsa'
在create_simple.yml文件中,我有以下内容:
- name: 'test'
debug: msg='{{ resultat.1.gname }}'
with_subelements:
- "{{ lookup('dict', user) }}"
- groups
loop_control:
loop_var: resultat
但我有一个信息:
FAILED! => {"msg": "An unhandled exception occurred while running the lookup plugin 'dict'. Error was a <class 'ansible.errors.AnsibleError'>, original message: with_dict expects a dict"}
失败!=>{“msg”:“运行查找插件'dict'时发生未经处理的异常。错误为,原始消息:with_dict需要dict”}
create.yml文件的with_项中的用户变量似乎是dict
我有一个转换dict中用户变量的测试,但它不起作用。
和_subelements
需要在列表中的每个元素中查找,但是查找('dict',user)
返回的哈希列表将返回类似以下内容:
[
{
"key": "comment",
"value": "comment user1"
},
{
"key": "shell",
"value": "/bin/bash"
},
{
"key": "name",
"value": "user1"
}
...
]
和with_subelements
将尝试在此列表中的每个字典中查找键组
,但失败
with_subelements:
- "{{ lookup('dict', user) }}" # Wrong: items in this list of dict does not have 'groups' keys
- groups # OK: this a field name to lookup
在包含的create\u simple.yml
中,变量user
将是一个字典,例如:
name: 'user1'
simple: False
groups:
- gname: 'secondary'
gid: 30000
...
如果每次包含create\u simple.yml
时都要列出组
,可以在create\u simple.yml
中执行以下操作:
- name: 'test'
debug:
msg: '{{ resultat.gname }}'
with_items: "{{ user.groups }}"
loop_control:
loop_var: resultat
# this will lookup field 'groups' in each element of the 'userslist' variable
- name: 'Create'
debug:
msg: "Group for user {{ resultat.0.name }}: {{ resultat.1.gname }}"
with_subelements:
- "{{ userslist }}"
- groups
loop_control:
loop_var: resultat
或者更简单,如果您只需要在create\u simple.yml
中执行单个任务,则可以直接将与子元素一起使用,而不是在create.yml
中使用包含任务:
- name: 'test'
debug:
msg: '{{ resultat.gname }}'
with_items: "{{ user.groups }}"
loop_control:
loop_var: resultat
# this will lookup field 'groups' in each element of the 'userslist' variable
- name: 'Create'
debug:
msg: "Group for user {{ resultat.0.name }}: {{ resultat.1.gname }}"
with_subelements:
- "{{ userslist }}"
- groups
loop_control:
loop_var: resultat
似乎是一句话
=>似乎是不够的。一定是这样。是什么让你认为这是一份口述?给我们看看。此外:1)与其将项
传递给用户
,不如直接将循环变量
更改为用户
?2) 您不需要将与{u items
声明为列表与{u items:“{userslist}}”
就足够了。我感觉所有这些都可以在一个任务中完成。你能显示userslist
的结构吗?我在执行include时会将该项传递给用户,因为我认为该项在include\u任务中不可访问,因此要在该项中执行另一个循环,以将所有组都包括在内。