# blockinfile & lineinfile both are not working in RHEL 7.6

**URL:** <https://forum.ansible.com/t/blockinfile-lineinfile-both-are-not-working-in-rhel-7-6/29777>\
**Category:** Ansible Project\
**Tags:** rhel\
**Created:** [February 26, 2019, 2:49pm UTC](https://forum.ansible.com/t/blockinfile-lineinfile-both-are-not-working-in-rhel-7-6/29777 "2019-02-26T14:49:35Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Deepan\_M](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@Deepan\_M](https://forum.ansible.com/u/Deepan_M)\
**Post date:** [February 26, 2019, 2:49pm UTC](https://forum.ansible.com/t/blockinfile-lineinfile-both-are-not-working-in-rhel-7-6/29777/1 "2019-02-26T14:49:35Z")

</div>

Hello Guys,

Can someone please help me to resolve this issue.

below playbook i have created to remove dns entries from resolv.conf and its working perfect on rhel 6 but not in rhel 7.6, is there any bug or i need to add any parameters ?

**Playbook :- blockinfile**

---

<div class="post-metadata">

**Author:** ![Deepan\_M](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@Deepan\_M](https://forum.ansible.com/u/Deepan_M)\
**Post date:** [February 26, 2019, 3:36pm UTC](https://forum.ansible.com/t/blockinfile-lineinfile-both-are-not-working-in-rhel-7-6/29777/2 "2019-02-26T15:36:54Z")

</div>

Can someone please help me.

---

<div class="post-metadata">

**Author:** ![Luca\_Cazzaniga](https://avatars.discourse-cdn.com/v4/letter/l/977dab/32.png) [@Luca\_Cazzaniga](https://forum.ansible.com/u/Luca_Cazzaniga)\
**Post date:** [February 26, 2019, 6:23pm UTC](https://forum.ansible.com/t/blockinfile-lineinfile-both-are-not-working-in-rhel-7-6/29777/3 "2019-02-26T18:23:25Z")

</div>

Are you sure the problem is ansible?  
the playbook fails? what’s the shown message?

Or the problem is that the configuration are reset on reboot?

regards

Luca

---

<div class="post-metadata">

**Author:** ![Luca\_Cazzaniga](https://avatars.discourse-cdn.com/v4/letter/l/977dab/32.png) [@Luca\_Cazzaniga](https://forum.ansible.com/u/Luca_Cazzaniga)\
**Post date:** [February 26, 2019, 6:34pm UTC](https://forum.ansible.com/t/blockinfile-lineinfile-both-are-not-working-in-rhel-7-6/29777/4 "2019-02-26T18:34:04Z")

</div>

take a look at /var/log/messages for line with

```auto
updated /etc/resolv.conf
see more details at https://access.redhat.com/solutions/7412

```

---

<div class="post-metadata">

**Author:** ![Luca\_Cazzaniga](https://avatars.discourse-cdn.com/v4/letter/l/977dab/32.png) [@Luca\_Cazzaniga](https://forum.ansible.com/u/Luca_Cazzaniga)\
**Post date:** [February 26, 2019, 7:27pm UTC](https://forum.ansible.com/t/blockinfile-lineinfile-both-are-not-working-in-rhel-7-6/29777/5 "2019-02-26T19:27:48Z")

</div>

Furthermore I don’t get why you use the argument block with blockinfile in order to remove the block… isn’t unnecessary?

regards

Luca

---

<div class="post-metadata">

**Author:** ![Deepan\_M](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@Deepan\_M](https://forum.ansible.com/u/Deepan_M)\
**Post date:** [February 27, 2019, 9:54am UTC](https://forum.ansible.com/t/blockinfile-lineinfile-both-are-not-working-in-rhel-7-6/29777/6 "2019-02-27T09:54:45Z")

</div>

Hello Luca,

Are you sure the problem is ansible?  
:-Not Sure, same playbook is working on rhel6 but not in rhel7, means no error.

the playbook fails? what’s the shown message?

:-no, playbook is working successfully in rhel 6, but rhel 7 its failing, not only resolv.conf even i tried with dummy file created on /tmp however that also failed.

:-Or the problem is that the configuration are reset on reboot?

no configuration issue

Furthermore I don’t get why you use the argument block with blockinfile in order to remove the block… isn’t unnecessary?

:- is there any module did ansible is having it for removing block in file ?

Thanks,  
Deepan M

---

<div class="post-metadata">

**Author:** ![Deepan\_M](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@Deepan\_M](https://forum.ansible.com/u/Deepan_M)\
**Post date:** [February 27, 2019, 10:16am UTC](https://forum.ansible.com/t/blockinfile-lineinfile-both-are-not-working-in-rhel-7-6/29777/7 "2019-02-27T10:16:16Z")

</div>

Hello Luca,

One more information about this blockinfile, i facing problem with state: absent only not with state: present, means i can able to add entries, not able to remove entries.

Thanks,  
Deepan M

---

<div class="post-metadata">

**Author:** ![Luca\_Cazzaniga](https://avatars.discourse-cdn.com/v4/letter/l/977dab/32.png) [@Luca\_Cazzaniga](https://forum.ansible.com/u/Luca_Cazzaniga)\
**Post date:** [February 27, 2019, 10:27am UTC](https://forum.ansible.com/t/blockinfile-lineinfile-both-are-not-working-in-rhel-7-6/29777/8 "2019-02-27T10:27:36Z")

</div>

blockinfile is the right choice  
anyway I’m not sure if the argument block is necessary

fusillator@catorcio:~/Code/ansible/test$ cat resolv.conf  
nameserver 8.8.8.8  
nameserver 13.2.7.8  
nameserver 13.2.7.9  
nameserver 13.2.7.10  
search [eu.ro.net](http://eu.ro.net) [mrtools.ro.com](http://mrtools.ro.com) [mit.gbr.ro.com](http://mit.gbr.ro.com)

fusillator@catorcio:~/Code/ansible/test$ cat blockinfile.yml

- hosts: localhost  
tasks:
- name: remove block regexps  
blockinfile:  
path: “./resolv.conf”  
marker: “{mark}”  
marker\_begin: “nameserver 13.2.7.8”  
marker\_end: “search [eu.ro.net](http://eu.ro.net) [mrtools.ro.com](http://mrtools.ro.com) [mit.gbr.ro.com](http://mit.gbr.ro.com)”  
state: absent  
backup: yes

fusillator@catorcio:~/Code/ansible/test$ ansible-playbook blockinfile.yml

PLAY [localhost] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\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\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
changed: [localhost]

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
localhost : ok=2 changed=1 unreachable=0 failed=0

fusillator@catorcio:~/Code/ansible/test$ cat resolv.conf  
nameserver 8.8.8.8

Can you launch the playbook with increased verbosity to analyze the failure?

Maybe it could be useful the debug feature to shorten the test:

[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_debugger.html](https://docs.ansible.com/ansible/latest/user_guide/playbooks_debugger.html)

Unfortunatly at the moment I’m not on a rhel host.  
Try also to check audit log for selinux restrictions

sealert -a /var/log/audit.log

regards

Luca

---

<div class="post-metadata">

**Author:** ![Luca\_Cazzaniga](https://avatars.discourse-cdn.com/v4/letter/l/977dab/32.png) [@Luca\_Cazzaniga](https://forum.ansible.com/u/Luca_Cazzaniga)\
**Post date:** [February 27, 2019, 10:42am UTC](https://forum.ansible.com/t/blockinfile-lineinfile-both-are-not-working-in-rhel-7-6/29777/9 "2019-02-27T10:42:46Z")

</div>

try to check if the marker line as blank/control/not printable characters at the end of the line

xxd resolv.conf

---

<div class="post-metadata">

**Author:** ![Deepan\_M](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@Deepan\_M](https://forum.ansible.com/u/Deepan_M)\
**Post date:** [February 27, 2019, 10:59am UTC](https://forum.ansible.com/t/blockinfile-lineinfile-both-are-not-working-in-rhel-7-6/29777/10 "2019-02-27T10:59:05Z")

</div>

Hi Luca,

Perfectly working what you have suggested.

I need one more suggestion, below is the content i want remove but when the line ends with #name its not working ? do you know how to remove with that also?

**Playbook:-**

cat resolve.yml

---

<div class="post-metadata">

**Author:** ![Luca\_Cazzaniga](https://avatars.discourse-cdn.com/v4/letter/l/977dab/32.png) [@Luca\_Cazzaniga](https://forum.ansible.com/u/Luca_Cazzaniga)\
**Post date:** [February 27, 2019, 11:06am UTC](https://forum.ansible.com/t/blockinfile-lineinfile-both-are-not-working-in-rhel-7-6/29777/11 "2019-02-27T11:06:57Z")

</div>

So you solved…  
The blank counts for the matching of the marker lines…

fusillator@catorcio:~/Code/ansible/test$ ansible-playbook blockinfile.yml

PLAY [localhost] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\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\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
changed: [localhost]

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
localhost : ok=2 changed=1 unreachable=0 failed=0

fusillator@catorcio:~/Code/ansible/test$ cat resolv.conf  
nameserver 8.8.8.8  
fusillator@catorcio:~/Code/ansible/test$ cat resolv.conf  
resolv.conf resolv.conf.3339.2019-02-26@20:21:57~ resolv.conf.3740.2019-02-27@11:09:34~ resolv.conf.4381.2019-02-27@12:03:36~  
fusillator@catorcio:~/Code/ansible/test$ cat resolv.conf  
resolv.conf resolv.conf.3339.2019-02-26@20:21:57~ resolv.conf.3740.2019-02-27@11:09:34~ resolv.conf.4381.2019-02-27@12:03:36~  
fusillator@catorcio:~/Code/ansible/test$ cat resolv.conf.4381.2019-02-27@12:03:36~  
nameserver 8.8.8.8  
nameserver 13.2.7.8 #dns  
nameserver 13.2.7.9  
nameserver 13.2.7.10  
search [eu.ro.net](http://eu.ro.net) [mrtools.ro.com](http://mrtools.ro.com) [mit.gbr.ro.com](http://mit.gbr.ro.com)  
fusillator@catorcio:~/Code/ansible/test$ cat blockinfile.yml

- hosts: localhost  
tasks:
- name: remove block regexps  
blockinfile:  
path: “./resolv.conf”  
marker: “{mark}”  
marker\_begin: “nameserver 13.2.7.8 #dns”  
marker\_end: “search [eu.ro.net](http://eu.ro.net) [mrtools.ro.com](http://mrtools.ro.com) [mit.gbr.ro.com](http://mit.gbr.ro.com)”  
state: absent  
backup: yes

regards

Luca

---

<div class="post-metadata">

**Author:** ![Deepan\_M](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@Deepan\_M](https://forum.ansible.com/u/Deepan_M)\
**Post date:** [February 27, 2019, 11:17am UTC](https://forum.ansible.com/t/blockinfile-lineinfile-both-are-not-working-in-rhel-7-6/29777/12 "2019-02-27T11:17:11Z")

</div>

Hi Luca,

I tried by removing blank space, even though its not working! seems # is not allowing to execute, can you please tell what can be done here!

cat resolve.yml

---

<div class="post-metadata">

**Author:** ![Luca\_Cazzaniga](https://avatars.discourse-cdn.com/v4/letter/l/977dab/32.png) [@Luca\_Cazzaniga](https://forum.ansible.com/u/Luca_Cazzaniga)\
**Post date:** [February 27, 2019, 11:25am UTC](https://forum.ansible.com/t/blockinfile-lineinfile-both-are-not-working-in-rhel-7-6/29777/13 "2019-02-27T11:25:01Z")

</div>

I dont know for me it s working… see my test in the prevoius answer. try to double check the yaml specification

---

<div class="post-metadata">

**Author:** ![Deepan\_M](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@Deepan\_M](https://forum.ansible.com/u/Deepan_M)\
**Post date:** [February 27, 2019, 11:36am UTC](https://forum.ansible.com/t/blockinfile-lineinfile-both-are-not-working-in-rhel-7-6/29777/14 "2019-02-27T11:36:37Z")

</div>

Hi Luca,

See below highlighted, seems on both playbook and file, you have removed blank space, thats why its working for you! but in my case blank is present, please see my output.

can you please my below info on your system and check what its showing.

fusillator@catorcio:~/Code/ansible/test$ cat resolv.conf.4381.2019-02-27@12:03:36~  
nameserver 8.8.8.8  
**nameserver 13.2.7.8 #dns**  
nameserver 13.2.7.9  
nameserver 13.2.7.10  
search [eu.ro.net](http://eu.ro.net/) [mrtools.ro.com](http://mrtools.ro.com/) [mit.gbr.ro.com](http://mit.gbr.ro.com/)  
fusillator@catorcio:~/Code/ansible/test$ cat blockinfile.yml

- hosts: localhost  
tasks:
- name: remove block regexps  
blockinfile:  
path: “./resolv.conf”  
marker: “{mark}”  
**marker\_begin: “nameserver 13.2.7.8 #dns”**  
marker\_end: “search [eu.ro.net](http://eu.ro.net/) [mrtools.ro.com](http://mrtools.ro.com/) [mit.gbr.ro.com](http://mit.gbr.ro.com/)”  
state: absent  
backup: yes

**myoutput:-**

cat resolv.conf  
nameserver 13.2.7.8 # dns1  
nameserver 13.2.7.9 # dns2  
nameserver 13.2.7.10 # dns3  
search [eu.ro.net](http://eu.ro.net) [mrtools.ro.com](http://mrtools.ro.com) [mit.gbr.ro.com](http://mit.gbr.ro.com)

cat resolve.yml

---

<div class="post-metadata">

**Author:** ![Luca\_Cazzaniga](https://avatars.discourse-cdn.com/v4/letter/l/977dab/32.png) [@Luca\_Cazzaniga](https://forum.ansible.com/u/Luca_Cazzaniga)\
**Post date:** [February 27, 2019, 12:11pm UTC](https://forum.ansible.com/t/blockinfile-lineinfile-both-are-not-working-in-rhel-7-6/29777/15 "2019-02-27T12:11:57Z")

</div>

Interesting I’m commuting at the moment, I will test It tonight

---

<div class="post-metadata">

**Author:** ![Luca\_Cazzaniga](https://avatars.discourse-cdn.com/v4/letter/l/977dab/32.png) [@Luca\_Cazzaniga](https://forum.ansible.com/u/Luca_Cazzaniga)\
**Post date:** [February 27, 2019, 3:48pm UTC](https://forum.ansible.com/t/blockinfile-lineinfile-both-are-not-working-in-rhel-7-6/29777/16 "2019-02-27T15:48:48Z")

</div>

Consider using the replace module to normalize the space incongruence before the blockinfile task…

Something like

```auto
- replace:
    path: resolv.conf
    regexp: '^(.*)\s+#\s+(.*)$'
    replace: ' # '
    backup: yes

unfortunately I can't try the playbook in this moment. 

Regards

Luca

```

---

<div class="post-metadata">

**Author:** ![Deepan\_M](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@Deepan\_M](https://forum.ansible.com/u/Deepan_M)\
**Post date:** [February 27, 2019, 4:26pm UTC](https://forum.ansible.com/t/blockinfile-lineinfile-both-are-not-working-in-rhel-7-6/29777/17 "2019-02-27T16:26:40Z")

</div>

Hello Luca,

Replace can be removed single line, but how we can remove block ?

Thanks,  
Deepan M

---

<div class="post-metadata">

**Author:** ![Deepan\_M](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@Deepan\_M](https://forum.ansible.com/u/Deepan_M)\
**Post date:** [February 27, 2019, 4:59pm UTC](https://forum.ansible.com/t/blockinfile-lineinfile-both-are-not-working-in-rhel-7-6/29777/18 "2019-02-27T16:59:45Z")

</div>

Hello Luca,

Below playbook no result, seems because of #name its not working i guess.

---

<div class="post-metadata">

**Author:** ![Luca\_Cazzaniga](https://avatars.discourse-cdn.com/v4/letter/l/977dab/32.png) [@Luca\_Cazzaniga](https://forum.ansible.com/u/Luca_Cazzaniga)\
**Post date:** [February 27, 2019, 5:58pm UTC](https://forum.ansible.com/t/blockinfile-lineinfile-both-are-not-working-in-rhel-7-6/29777/19 "2019-02-27T17:58:09Z")

</div>

Replace should substitute all instance matching the regexp, as for the manual

This module will replace all instances of a pattern within a file.

Try this

- hosts: localhost  
tasks:
- name: sort out blank discrepancy  
replace:

```auto
   path: resolv.conf
   regexp: '^(.*)\s+#\s+(.*)$'
   replace: ' # '
   backup: yes

```

- name: remove block regexps  
blockinfile:  
path: “./resolv.conf”  
marker: “{mark}”  
marker\_begin: “nameserver 13.2.7.8 #dns”  
marker\_end: “search [eu.ro.net](http://eu.ro.net) [mrtools.ro.com](http://mrtools.ro.com) [mit.gbr.ro.com](http://mit.gbr.ro.com)”  
state: absent  
backup: yes

Anyway I didn’t test it yet.  
Regards

Luca

---

<div class="post-metadata">

**Author:** ![Luca\_Cazzaniga](https://avatars.discourse-cdn.com/v4/letter/l/977dab/32.png) [@Luca\_Cazzaniga](https://forum.ansible.com/u/Luca_Cazzaniga)\
**Post date:** [February 28, 2019, 12:48am UTC](https://forum.ansible.com/t/blockinfile-lineinfile-both-are-not-working-in-rhel-7-6/29777/20 "2019-02-28T00:48:09Z")

</div>

I’m finally home

Sorry the previous regexp was completely incorrect. Now I fixed it with a couple of tests…

And here it is

fusillator@catorcio:~/Code/ansible/test$ cat blockinfile.yml

- hosts: localhost  
tasks:
- name: sort out blank discrepancy  
replace:  
path: resolv.conf  
regexp: ‘^nameserver\s+([^]+)\s+#(.\*)’  
replace: ‘nameserver \1 #\2’  
backup: yes
- name: remove block regexps  
blockinfile:  
path: “./resolv.conf”  
marker: “{mark}”  
marker\_begin: “nameserver 13.2.7.8 # dns1”  
marker\_end: “search [eu.ro.net](http://eu.ro.net) [mrtools.ro.com](http://mrtools.ro.com) [mit.gbr.ro.com](http://mit.gbr.ro.com)”  
state: absent  
fusillator@catorcio:~/Code/ansible/test$ cat resolv.conf  
nameserver 8.8.8.8  
nameserver 13.2.7.8 # dns1  
nameserver 13.2.7.9 # dns2  
nameserver 13.2.7.10 # dns3  
search [eu.ro.net](http://eu.ro.net) [mrtools.ro.com](http://mrtools.ro.com) [mit.gbr.ro.com](http://mit.gbr.ro.com)  
fusillator@catorcio:~/Code/ansible/test$ ansible-playbook blockinfile.yml

PLAY [localhost] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*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