# ansible.builtin.replace

**URL:** <https://forum.ansible.com/t/ansible-builtin-replace/36548>\
**Category:** Ansible Project\
**Created:** [April 21, 2022, 10:40am UTC](https://forum.ansible.com/t/ansible-builtin-replace/36548 "2022-04-21T10:40:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Meir\_Hazon](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Meir\_Hazon](https://forum.ansible.com/u/Meir_Hazon)\
**Post date:** [April 21, 2022, 10:40am UTC](https://forum.ansible.com/t/ansible-builtin-replace/36548/1 "2022-04-21T10:40:48Z")

</div>

Hello,

I would like to,

1. copy a file (template)
2. edit 2 lines based on regex

I actually did it using copy and replace.

The issue is that on the next ansible run, ansible identifies that the dest file does not matches the src file and it would then copy the file and make the change based on the regex again.

I am sure that others had this issue, could you suggest how it could be solved?

Thanks so much,

Meir

---

<div class="post-metadata">

**Author:** ![Meir\_Hazon](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Meir\_Hazon](https://forum.ansible.com/u/Meir_Hazon)\
**Post date:** [April 21, 2022, 10:48am UTC](https://forum.ansible.com/t/ansible-builtin-replace/36548/2 "2022-04-21T10:48:21Z")

</div>

also, on of the regex is replaced by a random string

---

<div class="post-metadata">

**Author:** ![Meir\_Hazon](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Meir\_Hazon](https://forum.ansible.com/u/Meir_Hazon)\
**Post date:** [April 21, 2022, 12:01pm UTC](https://forum.ansible.com/t/ansible-builtin-replace/36548/3 "2022-04-21T12:01:32Z")

</div>

this is the code line from the template,

server\_id = {{ 32000 | random }}

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [April 21, 2022, 12:07pm UTC](https://forum.ansible.com/t/ansible-builtin-replace/36548/4 "2022-04-21T12:07:46Z")

</div>

Seed your random number generator with some non-changing value that’s unique to the target host. Then your server\_id will be “random” relative to that of other hosts, but constant (idempotent) for that host from run to run.

With that, you should be able to do in a single template whatever you’re trying to do with the pair of copy and line\_in\_file tasks.

---

<div class="post-metadata">

**Author:** ![Meir\_Hazon](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Meir\_Hazon](https://forum.ansible.com/u/Meir_Hazon)\
**Post date:** [April 21, 2022, 12:25pm UTC](https://forum.ansible.com/t/ansible-builtin-replace/36548/5 "2022-04-21T12:25:41Z")

</div>

Thanks, I did something similar, created the random outside of ansible and mapping it as a variable to the template.  
Thanks so much

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [April 21, 2022, 1:57pm UTC](https://forum.ansible.com/t/ansible-builtin-replace/36548/6 "2022-04-21T13:57:28Z")

</div>

You can do that of course, but then you’ve got this outside dependency. You could keep it all inside Ansible like so:

server\_id = {{ 32000 | random(seed=ansible\_ssh\_host\_key\_rsa\_public) }}

That’s going to be the same “random” number each time for any given host, but different for each host.

We had a similar randomization problem, where we wanted to schedule a cron job on all of our hosts, but wanted to spread them out over the work day between 7AM and 7PM. We ended up doing this:

- name: abc cron job  
cron:  
name: “abc cron job”  
day: “\*”  
weekday: “1-5”  
minute: “{{ rng\_minute }}”  
hour: “{{ rng\_hour }}”  
job: “/usr/local/sbin/abc”  
state: “{{ abc\_cron | default(‘present’) }}”  
vars:  
rng\_minute: “{{ 59 | random(seed=ansible\_ssh\_host\_key\_rsa\_public) }}”  
rng\_hour: “{{ 19 | random(seed=(rng\_minute ~ ansible\_ssh\_host\_key\_rsa\_public), start=7) }}”

It’s tempting to move the seeding and the random() calls themselves up into the body of the task, but putting it in vars, and making the second dependent on the first, guarantees an order of evaluation. Otherwise, as we found out the hard way, you can get some inexplicable inconsistencies that make your cron job schedules dance around!
