大家好!
我可能有一个愚蠢的问题...没有找到与我有同样问题的其他人。我正在尝试创建一个角色来处理用户/组/ ssh_key的创建,这应该非常简单。但这是我的问题...
首先让我向您介绍架构:
root
├── all.yml
├── ansible.cfg
├── commun.yml
├── hosts
└── roles
└── commun
├── defaults
│ └── main
│ ├── groups.yml
│ ├── home_config.yml
│ ├── packages.yml
│ └── users.yml
├── files
├── handlers
├── tasks
│ ├── config.yml
│ ├── groups.yml
│ ├── main.yml
│ ├── packages.yml
│ └── users.yml
└── templates
我在commun/defaults/main/users.yml
中得到以下声明:
---
users:
segar_h:
fullname: "John Doe"
groups:
- "admins"
ssh_keys:
- "ssh-rsa xxxxx"
和commun/tasks/users.yml
---
- name: "Ensure the user '{{ item }}' exists"
user:
include: users.yml
name: "{{ item }}"
groups: "{{ item.groups }}"
state: present
createhome: yes
append: yes
shell: /bin/zsh
system: yes
with_items:
- users
- name: "Ensure the user '{{ item }}' accepts the SSH key"
authorized_key:
user: "{{ item }}"
key: "{{ item.ssh_keys }}"
state: present
path: "/etc/ssh/keys/%u/authorized_keys"
with_items:
- users
不确定我是否做对了,但是,我似乎无法理解如何访问为此角色声明的变量。 我要做的就是在每个声明的用户上循环并为他创建一个家。特定于该用户的ssh密钥也是如此。
我收到以下错误消息:
{"msg": "The task includes an option with an undefined variable. The error was: 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'groups'\n\nThe error appears to have been in '/xxx/commun/tasks/users.yml': line 2, 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: \"Ensure the user '{{ item }}' exists\"\n ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n with_items:\n - {{ foo }}\n\nShould be written as:\n\n with_items:\n - \"{{ foo }}\"\n"}
我做错什么了吗?有没有这样做的方法而无需在任务中声明变量?就像下面的语句: 您能提供一个示例/资源以便我调查一下吗?
---
- name: "example task"
vars:
- users1:
name
user:
[...]
预先感谢您的帮助!
PS:我来自Saltstack。很抱歉,如果这是新手的问题。
问题2?
正如您告诉我的那样,我将变量声明为列表。
---
groups:
- name: admins
sudo: True
以下任务不适用于同一问题:
任务
- name: "print each group details"
debug:
msg: "name: {{ item.name }}, sudo: {{ item.sudo }}"
with_items:
- "{{ groups }}"
错误消息
FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'name'\n\nThe error appears to have been in '/xxx/commun/tasks/groups.yml': line 2, 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: \"print each group details\"\n ^ here\n"}
解决方案问题2
似乎group
变量已被覆盖,但库存变量已被覆盖。
答案 0 :(得分:1)
从commun/tasks/users.yml
中的任务来看,users
应该是一个列表(当前示例中只有1个项目,但我想将来可能还会有更多项目)。您应该将任务with_items
声明更改为以下格式:
with_items:
- "{{ users }}"
以及使用yml语法声明list
变量的正确方法要求以-
开始每个元素。但此外,我会在列表的每个“用户”元素中添加一个name
属性,使语法采用这种格式。完整文件,并添加第二个用户作为示例:
---
users:
- name: segar_h
fullname: "John Doe"
groups:
- "admins"
ssh_keys:
- "ssh-rsa xxxxx"
- name: nick_dalton
fullname: "Nick Dalton"
groups:
- "admins"
ssh_keys:
- "ssh-rsa xxxxx12"
这是解析每个用户属性的示例任务:
---
- name: print each users details
debug:
msg: "name: {{ item.name }}, 1st group: {{ item.groups[0] }}, ssh_keys: {{ item.ssh_keys }}"
with_items:
- "{{ users }}"
上面将打印:
TASK [test : print each users details] *********************************************************************************************************************************************************************************
ok: [localhost] => (item={'name': 'segar_h', 'fullname': 'John Doe', 'groups': ['admins'], 'ssh_keys': ['ssh-rsa xxxxx']}) => {
"msg": "name: segar_h, 1st group: admins, ssh_keys: ['ssh-rsa xxxxx'], name: {'name': 'segar_h', 'fullname': 'John Doe', 'groups': ['admins'], 'ssh_keys': ['ssh-rsa xxxxx']}"
}
ok: [localhost] => (item={'name': 'nick_dalton', 'fullname': 'Nick Dalton', 'groups': ['admins'], 'ssh_keys': ['ssh-rsa xxxxx12']}) => {
"msg": "name: nick_dalton, 1st group: admins, ssh_keys: ['ssh-rsa xxxxx12'], name: {'name': 'nick_dalton', 'fullname': 'Nick Dalton', 'groups': ['admins'], 'ssh_keys': ['ssh-rsa xxxxx12']}"
}
希望这些帮助!