以下是我的 ansible 剧本中的相关文件:
group_vars/all.yml
users:
- user1
- user2
库存/生产.yml
ungrouped:
hosts:
host1.test.de:
角色/base/defaults/main.yml
users:
- user2
- user3
- user4
all_users:
user1:
name: ..
groups: ...
user2: ...
user3: ...
user4: ...
角色/基础/任务/user.yml
- name: Create users
ansible.builtin.user:
name: "{{ item.name }}"
password: "{{ item.shadow | default(omit) }}"
groups: "{{ item.groups | default([]) }}"
append: true
shell: "/bin/bash"
state: present
loop: "{{ users | map('extract', all_users) }}"
loop_control:
label: "{{ item.name }}"
角色/基础/任务/main.yml
- name: Create Users
become: yes
import_tasks: user.yml
tags: user
站点.yml
- name: Setup basic linux-server installation
hosts: all
roles:
- base
tags: base
现在我执行用户任务:
$ ansible-playbook -i inventories/production.yml site.yml -t user
然后 ansible 循环遍历
roles/base/defaults/main.yml
{user2, user3, user4} 中的元素。但它应该循环 group_vars/all.yml
{user1, user2} 中的元素,如文档中所述:
https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_variables.html#understanding-variable-precedence
当我从
users
中删除 roles/base/defaults/main.yml
变量时,所有工作都正常,并且可以在 group_vars/all.yml
中的用户列表上进行 Ansible 循环。
我这里有什么问题吗?为什么使用默认变量而不是group_var?
在我的site.yml中发现问题:
- hosts: all
tasks:
- name: Include vars from base role for other roles
include_vars:
file: roles/base/defaults/main.yml
tags: always
我需要其他一些角色,必须想办法避免这种情况..