# how to restrict scope for set\_fact variable

**URL:** <https://forum.ansible.com/t/how-to-restrict-scope-for-set-fact-variable/33162>\
**Category:** Ansible Project\
**Created:** [June 12, 2020, 1:17pm UTC](https://forum.ansible.com/t/how-to-restrict-scope-for-set-fact-variable/33162 "2020-06-12T13:17:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Diyawary](https://avatars.discourse-cdn.com/v4/letter/d/d2c977/32.png) [@Diyawary](https://forum.ansible.com/u/Diyawary)\
**Post date:** [June 12, 2020, 1:17pm UTC](https://forum.ansible.com/t/how-to-restrict-scope-for-set-fact-variable/33162/1 "2020-06-12T13:17:29Z")

</div>

Hi All,

below is my playbook  
mserver: [1.1.1.1,2.2.2.2]  
doamin: [3.3.3.3,4.4.4.4]

tasks:

- include\_role:  
name: validateip  
when: object\_type == “ip”  
with\_together:
- “{{ mserver\_hostname }}”
- “{{ domain }}”

under that role login,validation and logout tasks are performed  
validation script-

- name: Checking Network objects  
uri:  
url: “https://{{item.0}}/web\_api/show-objects”  
validate\_certs: False  
method: POST  
headers:  
x-chkp-sid: “{{ login.json.sid }}”  
body:  
type: host  
filter: “{{ip}}”  
ip-only: true  
body\_format: json  
register: check\_host\_result  
when: item.0 != “”

- debug:  
var: check\_host\_result

- name: Checking if Network Object exists  
set\_fact:  
item\_ip\_exists: true  
obj\_name: “{{ item2[‘name’] }}”  
loop: “{{ check\_host\_result.json.objects }}”  
loop\_control:  
loop\_var: item2  
when:

- item2[‘ipv4-address’] is defined and item2[‘ipv4-address’] == ip

- debug:  
msg: “Network Object exists with name [{{obj\_name}}]”  
when: item\_ip\_exists is defined

- debug:  
msg: " Network Object ({{ip}}) will be created"  
when: item\_ip\_exists is not defined

I am facing issue for set\_fact variable like obj\_name and item\_ip\_exists  
so when loop runs on first item and if object is present so it set both the variable (obj\_name and item\_ip\_exists ) and print the correct debug messages.  
but when 2nd item executed and there if object is not present so it is printing the wrong debug message due to the set\_fact variables( obj\_name and item\_ip\_exists) which has already the value from the first items execution  
so how i can restrict the scope of set\_fact variables ( obj\_name and item\_ip\_exists ) so when second item execute the variables take the value from there not from previously set\_fact values.  
I am totally stuck here.  
Please help me. Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Diyawary](https://avatars.discourse-cdn.com/v4/letter/d/d2c977/32.png) [@Diyawary](https://forum.ansible.com/u/Diyawary)\
**Post date:** [June 14, 2020, 10:22am UTC](https://forum.ansible.com/t/how-to-restrict-scope-for-set-fact-variable/33162/2 "2020-06-14T10:22:41Z")

</div>

Could any one please help me on it . Appriciate your help in advance .

---

<div class="post-metadata">

**Author:** ![Diyawary](https://avatars.discourse-cdn.com/v4/letter/d/d2c977/32.png) [@Diyawary](https://forum.ansible.com/u/Diyawary)\
**Post date:** [June 15, 2020, 10:24am UTC](https://forum.ansible.com/t/how-to-restrict-scope-for-set-fact-variable/33162/3 "2020-06-15T10:24:06Z")

</div>

Hi,  
can any one please suggest on it or any other way through I can accomplish it.

---

<div class="post-metadata">

**Author:** ![Diyawary](https://avatars.discourse-cdn.com/v4/letter/d/d2c977/32.png) [@Diyawary](https://forum.ansible.com/u/Diyawary)\
**Post date:** [June 19, 2020, 6:57am UTC](https://forum.ansible.com/t/how-to-restrict-scope-for-set-fact-variable/33162/4 "2020-06-19T06:57:07Z")

</div>

Hi ,

how to resolve this issue.I guess we can not restrict the scope of set\_fact variable we can set the value null but in my case ,i wanted the set\_facts variables value is undefined so when the 2nd item of loop executed so it will get the value from that execution.

can any one suggest any-other way to accomplish my task.

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [June 19, 2020, 7:15am UTC](https://forum.ansible.com/t/how-to-restrict-scope-for-set-fact-variable/33162/5 "2020-06-19T07:15:11Z")

</div>

> can any one suggest any-other way to accomplish my task.

Make it [mcve] and include an expected result  
[mcve] [https://stackoverflow.com/help/minimal-reproducible-example](https://stackoverflow.com/help/minimal-reproducible-example)

Hints:  
\* Does your problem depend on the included role? If not, remove it.  
\* Does your problem depend on the uri module? If not, remove it.  
\* Create the lists as simple as possible to demonstrate the problem

It seems that your problem might be limited to a single task. Please help us  
to help you.

---

<div class="post-metadata">

**Author:** ![Diyawary](https://avatars.discourse-cdn.com/v4/letter/d/d2c977/32.png) [@Diyawary](https://forum.ansible.com/u/Diyawary)\
**Post date:** [June 19, 2020, 9:46am UTC](https://forum.ansible.com/t/how-to-restrict-scope-for-set-fact-variable/33162/6 "2020-06-19T09:46:40Z")

</div>

Thanks Vladmir for replying on my post.  
in my question inculd\_role and the validation task script is necessary to understand my question.

so basically in my playbook i am calling a role and looping that role with two lists mserver and domain

. I am performing validation of object on firewall using the firewall api and getting the r present object in “ **check\_host\_result.json.objec** t” and then i write the task to check object is present .where I am using set\_fact variable item\_ip\_exists and item\_name. my problem is when list’s first item executed then both set\_fact variables have the values but when lists second item executed and if object is not present on there that task is referencing the previous set\_fact varaibles value and printing the debug message incorrect.

below is the lists  
mserver: [1.1.1.1,2.2.2.2]  
doamin: [3.3.3.3,4.4.4.4]

- name: Checking Network objects  
uri:  
url: “https://{{item.0}}/web\_api/show-objects”  
validate\_certs: False  
method: POST  
headers:  
x-chkp-sid: “{{ login.json.sid }}”  
body:  
type: host  
filter: “{{ip}}”  
ip-only: true  
body\_format: json  
register: check\_host\_result  
when: item.0 != “”

- name: Checking if Network Object already exists  
set\_fact:  
item\_ip\_exists: true  
item\_name: “{{ item2[‘name’] }}”  
loop: “{{ check\_host\_result.json.objects }}”  
loop\_control:  
loop\_var: item2  
when:

- item2[‘ipv4-address’] is defined and item2[‘ipv4-address’] == ip

- debug:  
msg: “Network Object exists with name [{{item\_name}}]”  
when: item\_ip\_exists is defined

- debug:  
msg: “Network Object does not exist”  
when: item\_ip\_exists is not defined
