Loops 如何在ansible playbook的另一项任务中循环设置_事实属性
在我的ansible剧本中,我正在努力循环使用集合中的项目Loops 如何在ansible playbook的另一项任务中循环设置_事实属性,loops,ansible,ansible-facts,Loops,Ansible,Ansible Facts,在我的ansible剧本中,我正在努力循环使用集合中的项目 - set_fact: TAGNAME: "TEST-EC2-0{{item}}" TAGOWNER: LOGIN TAGROLE: DB with_sequence: start=01 end="{{ count }}" register: tagname 下面是使用debug设置事实的结果 "results": [ {
- set_fact:
TAGNAME: "TEST-EC2-0{{item}}"
TAGOWNER: LOGIN
TAGROLE: DB
with_sequence: start=01 end="{{ count }}"
register: tagname
下面是使用debug设置事实的结果
"results": [
{
"_ansible_ignore_errors": null,
"_ansible_item_label": "1",
"_ansible_item_result": true,
"_ansible_no_log": false,
"ansible_facts": {
"TAGNAME": "TEST-EC2-01",
"TAGOWNER": "LOGIN",
"TAGROLE": "DB"
},
"changed": false,
"failed": false,
"item": "1"
},
{
"_ansible_ignore_errors": null,
"_ansible_item_label": "2",
"_ansible_item_result": true,
"_ansible_no_log": false,
"ansible_facts": {
"TAGNAME": "TEST-EC2-02",
"TAGOWNER": "LOGIN",
"TAGROLE": "DB"
},
"changed": false,
"failed": false,
"item": "2"
}
]
有了这个给定的输出,每当我使用计数模块创建多个实例时,我想使用下面ec2任务中的所有“标记名”进行标记
ec2:
....
....
....
instance_tags:
Name: "{{ item.TAGNAME }}"
Owner: "LOGIN"
with_items: "{{ tagname.results }}"
但是,当我触发剧本时,我的错误率就降低了
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'item' is undefined\n\nThe error appears to have been in 'local/apps/roles/EC2_Deploy/tasks/ec2_creation.yml': line 217, 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 Instances\n ^ here\n"}
我不知道我错过了什么,在哪里。有人能帮我一下吗?
item.ansible\u facts.TAGNAME
更可能是基于您的JSON结构。我收到了这个错误fatal:[localhost]:失败!=>{“msg”:"该任务包含一个带有未定义变量的选项。错误是:“项”未定义\n\n错误似乎出现在“local/apps/roles/EC2\u Deploy/tasks/EC2\u creation.yml”中:第217行第3列,但可能\n位于文件的其他位置,具体取决于语法问题。\n\n有问题的行可能是:\n\n\n-name:Create Instances\n^here\n“}