英文:
Not able to read/print the csv file data
问题
以下是您要求的代码部分的翻译:
运行以下剧本并尝试打印该任务的值。它失败并显示以下错误。
> FAILED! => {"msg": "该任务包含一个具有未定义变量的选项。错误是:'list object'没有'name'属性\n\n错误出现在'/Users/srikanth.venugopalan/ansible/roles/rmi_staging/tasks/main.yml'文件中:第9行,第4列,但根据确切的语法问题,文件的其他地方也可能有问题。\n\n有问题的行似乎是:\n\n register: topics\n - debug:\n ^ 这里\n"}
代码如下
- name: 登录到集群
shell: /home/srikanth/ls.sh
- name: 读取CSV文件
read_csv:
path: /home/srikanth/template1.csv
register: topics
- debug:
msg: '名称 {{ topics.list.name }}'
我也进行了翻译,确保只包含您要求的翻译文本。如果您有任何其他问题,请随时提出。
英文:
Running the below playbook and trying to print the value of that task. It fails with below error.
> 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 '/Users/srikanth.venugopalan/ansible/roles/rmi_staging/tasks/main.yml': line 9, column 4, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n register: topics\n - debug:\n ^ here\n"
The code is
- name: Login to cluster
shell: /home/srikanth/ls.sh
- name: Read the CSV file
read_csv:
path: /home/srikanth/template1.csv
register: topics
- debug:
msg: 'Name {{ topics.list.name }}'
I have tried below as well, but no luck
- name: Login to cluster
shell: /home/srikanth/ls.sh
- name: Read the CSV file
read_csv:
path: /home/srikanth/template1.csv
register: topics
- debug:
msg: 'Name {{ topics.list.1.name }}'
CSV
file format as per below
name,replica,partition
test-topic,3,3
I have gone through the link https://stackoverflow.com/questions/53799730/how-to-read-csv-file-data-in-ansible-playbook-using-with-lines, but one thing dont know in the debug
how it come user.username
.
Please guide me.
Actual goal is to take name alone from the debug
output.
答案1
得分: 1
你的.1.name
尝试没有成功,因为topics.list
就像Python中的所有列表数据结构一样,是从0开始索引的。
你可以自己观察这种行为,因为评论要求你这样做:
tasks:
- copy:
dest: ./template1.csv
content: |
name,replica,partition
test-topic,3,3
- read_csv:
path: ./template1.csv
register: topics
- debug:
var: topics
- debug:
msg: 'Name {{ topics.list.0.name }}'
输出如下:
ok: [localhost] => {
"topics": {
"changed": false,
"dict": {},
"failed": false,
"list": [
{
"name": "test-topic",
"partition": "3",
"replica": "3"
}
]
}
}
ok: [localhost] => {
"msg": "Name test-topic"
}
英文:
Your .1.name
attempt didn't work because topics.list
is, like all list data structures in python, 0-indexed
You can observe the behavior for yourself, as the comments asked you to:
tasks:
- copy:
dest: ./template1.csv
content: |
name,replica,partition
test-topic,3,3
- read_csv:
path: ./template1.csv
register: topics
- debug:
var: topics
- debug:
msg: 'Name {{ topics.list.0.name }}'
emits
ok: [localhost] => {
"topics": {
"changed": false,
"dict": {},
"failed": false,
"list": [
{
"name": "test-topic",
"partition": "3",
"replica": "3"
}
]
}
}
ok: [localhost] => {
"msg": "Name test-topic"
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论