我有一个复杂的环境,包含多个系统的阶段和用户。
阶段是:dev,int等。
每个阶段都有一个应用程序的用户,我们称之为用户 john。这导致用户 johnd 用于 dev 和 johni 用于 int 等等。
为了抽象系统管理,由于阶段中的所有系统都是相同的,我创建了这个数据结构:
users:
john:
dev:
name: "johnd"
int:
name: "johni"
现在我有一个角色“collect_user_information”,它从 LDAP 收集所有类型的信息,并在 set_fact 的帮助下将它们存储在一个变量中。
- name: Get the userhome out of LDAP
shell: 'getent passwd {{ users[ user ][ stage ].name }} | cut -d: -f6'
register: user_home
和 set_fact:
- name: set facts for LDAP user
set_fact:
"{{user}}":
name: "{{ users[ user ][ stage ].name }}"
home: "{{ user_home.stdout }}"
要转储变量,我使用:
- name: debug output for myuser
debug: var="{{user}}"
调试输出看起来很有希望。
TASK [collect_user_information : debug output for user] *******
ok: [host1] => {
"john": {
"home": "/home/johni",-
"name": "johni"
}
}
现在我想执行一个角色来创建用户主页。
- { role: create_user_home, user: "john" }
首先,我转储变量的条目:
- name: debug role create_user_home output for variable user
debug: var=user
TASK [create_user_home : debug role create_user_home output for variable user] ***********
ok: [host1] => {
"user": "john"
}
- name: debug role create_user_home output for variable john
debug: var={{ user }}
TASK [create_user_home : debug role create_user_home output for variable john] **********
ok: [host1] => {
"john": {
"home": "/home/johni",-
"name": "johni"
}
}
现在我想使用这个数据结构。我想我可以通过引用“{{user.name}}”或“{{user['name']}}”来访问这些值,但两者都不起作用。
TASK [create_user_home : Create home directories for john] ***********************
fatal: [host1]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'ansible.parsing.yaml.objects.AnsibleUnicode object' has no attribute 'home'\n\nThe error appears to have been in '/etc/ansible/roles/create_user_home/tasks/main.yml': line 37, 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: Create home directories for john\n ^ here\n"}
那么,它是如何工作的?