Var文件/iterate_hosts.yml
---
hostlist:
- host: '2019'
inventory_name: 'Level 1'
ip: '2019'
mac: 'All industries'
- host: '2020'
inventory_name: 'Level 2'
ip: '2020'
mac: 'All industries'
- host: '2021'
inventory_name: 'Level 3'
ip: '2021'
mac: 'All industries'
main.yml
---
- name: csv
hosts: localhost
gather_facts: no
tasks:
- name: var
include_vars: ./iterate_hosts.yml
- name: includeing role
with_sequence: 0-10
include_role:
name: csv_test
Ansible角色(csv_测试主文件)csv_测试/task/main.yml
---
- set_fact:
count123: '{{item}}'
- name: test
debug:
msg: "{{ hostlist[item]}}"
错误:
"msg": "The task includes an option with an undefined variable. The error was: 'list object' has no attribute '0'\n\nThe error appears to be in '/csv/csv_test/tasks/main.yml
意见:
{{ hostlist[count123] }}
中的值应该是{{ hostlist[0] }}"
,依此类推李>ok: [localhost] => {
"ansible_facts": {
"count123": "0"
},
"changed": false
}
如果值类似于"count123": 0
如何转义引号并获得整数而不是字符串
期望值:我必须遍历/iterate_hosts.yml,为此,我正在使用main.yml文件中的with_sequence: 0-10
因此:
对于hostlist[count123]
(第一次迭代应该是count123=0
),它应该打印内容表单/iterate\u hosts.yml
host: '2019'
inventory_name: 'Level 1'
ip: '2019'
mac: 'All industries'
对于hostlist[count123]
(第二次迭代应该是count123=1
),它应该打印
host: '2020'
inventory_name: 'Level 2'
ip: '2020'
mac: 'All industries'
等等
您可以使用^{} Jinja过滤器进行此操作
因此,您的文件csv_test/task/main.yml最终是:
注意,
with_sequence
正在生成字符串这一事实在文档来源:https://docs.ansible.com/ansible/devel/collections/ansible/builtin/sequence_lookup.html#synopsis
这是一本完整的工作手册:
这让我们概括一下:
相关问题 更多 >
编程相关推荐