Ansible 可能的错误:';列出对象';没有属性';名称';使用read_csv模块时
正如标题中所说,“列表对象”没有属性“名称”,当我试图从csv文件中读取时出现错误,只想读取“名称”列 我的csv:Ansible 可能的错误:';列出对象';没有属性';名称';使用read_csv模块时,ansible,Ansible,正如标题中所说,“列表对象”没有属性“名称”,当我试图从csv文件中读取时出现错误,只想读取“名称”列 我的csv: name,uid,gid,password eg1,1,2,password eg2,1,2,password 我遵循了官方的Ansible文件 我的ansible剧本: --- - hosts: localhost become: yes become_method: sudo become_user: root tasks: - name: read
name,uid,gid,password
eg1,1,2,password
eg2,1,2,password
我遵循了官方的Ansible文件
我的ansible剧本:
---
- hosts: localhost
become: yes
become_method: sudo
become_user: root
tasks:
- name: read from CSV
read_csv:
path: user-creation.csv
register: users
- debug:
msg: "{{users.list.name}}"
我得到的错误是:
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'list object' has no attribute 'name'\n\nThe error appears to be in '/root/ansible/main.yml': line 12, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - debug:\n ^ here\n"}
我搜索了所有地方,似乎大多数人要么不使用该模块,要么在使用该模块时没有这个问题。明白了:
- name: read from CSV
read_csv:
path: user-creation.csv
register: users
- debug:
msg: "{{ item.name }}"
with_items:
- "{{ users.list }}"
输出:
ok: [localhost] => (item={'name': 'eg1', 'uid': '1', 'gid': '2', 'password': 'password'}) => {
"msg": "eg1"
}
ok: [localhost] => (item={'name': 'eg2', 'uid': '1', 'gid': '2', 'password': 'password'}) => {
"msg": "eg2"
在旁注上;如果你真的被这样的任务困住了,你可以使用例如awk
来实现你的目标。可以使用。下面的任务
- debug:
msg: "{{ users.list|json_query('[*].name') }}"
给予
啊,但我只想打印“姓名”。例如:“eg1”、“eg2”这正是它所做的。它先打印eg1,然后再打印eg2。这就是它返回的消息。弗拉基米尔他的回答是正确的。
"msg": [
"eg1",
"eg2"
]