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 ,就可以在输出结果中打印出一个数组,里边包含所有循环中的值
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 41 42 |
ansible-playbook exmaple.com [WARNING]: No inventory was parsed, only implicit localhost is available [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all' PLAY [test set fact with] ****************************************************************************************************** TASK [Gathering Facts] ********************************************************************************************************* ok: [localhost] TASK [this is a test] ********************************************************************************************************** ok: [localhost] => { "msg": "this is the test message from localhost" } TASK [set fact] **************************************************************************************************************** ok: [localhost] => (item={u'group_id': u'groupid_abc_1', u'group_name': u'Name_1'}) ok: [localhost] => (item={u'group_id': u'groupid_abc_2', u'group_name': u'Name_2'}) ok: [localhost] => (item={u'group_id': u'groupid_abc_3', u'group_name': u'Name_3'}) TASK [show me the security_group_new] ****************************************************************************************** ok: [localhost] => { "security_group_new": "Name_3" } TASK [set fact] **************************************************************************************************************** ok: [localhost] => (item={u'group_id': u'groupid_abc_1', u'group_name': u'Name_1'}) ok: [localhost] => (item={u'group_id': u'groupid_abc_2', u'group_name': u'Name_2'}) ok: [localhost] => (item={u'group_id': u'groupid_abc_3', u'group_name': u'Name_3'}) TASK [show me the security_group_new] ****************************************************************************************** ok: [localhost] => { "security_group_new_append": [ "Name_1", "Name_2", "Name_3" ] } PLAY RECAP ********************************************************************************************************************* localhost : ok=6 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 |
所以我们可以使用这种方式来规避set_fact中的循环问题
Latest posts by Zhiming Zhang (see all)
- aws eks node 自动化扩展工具 Karpenter - 8月 10, 2022
- ReplicationController and ReplicaSet in Kubernetes - 12月 20, 2021
- public key fingerprint - 5月 27, 2021