ansible 中set_fact 支持循环,with_items,但是变量并不是append 例如:第一个set_fact部分用的是with_items,但是,我们打印的结果中只有最后一次循环中的赋值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
--- #source your credential first: export AWS_PROFILE=poc #confirm the origin host have the iam role setting , security group setting #copy the userdata into the j2 file and make necessary change #ansible-playbook create_new_instance_from_old_one.yml -e "origin_host_id=i-095ea1e9b5ea236b0" -e "region=us-east-1" -e "new_host_name=jeremy-test-playbook-instance11" - name: test set fact with hosts: localhost vars: security_group_new_append: [] security_group_dict: - group_id: "groupid_abc_1" group_name: "Name_1" - group_id: "groupid_abc_2" group_name: "Name_2" - group_id: "groupid_abc_3" group_name: "Name_3" tasks: - name: this is a test debug: msg: "this is the test message from localhost" - name: set fact set_fact: security_group_new: "{{ item.group_name }}" with_items: "{{ security_group_dict }}" - name : show me the security_group_new debug: var: security_group_new - name: set fact set_fact: security_group_new_append: "{{ security_group_new_append + [item.group_name] }}" with_items: "{{ security_group_dict }}" - name : show me the security_group_new debug: var: security_group_new_append |
但是如果我们使用第二个task ,就可以在输出结果中打印……