lock in linenifile please!!

Hi,

I desperately need https://github.com/ansible/ansible-modules-core/issues/2769. It has been languishing for a year and not been pulled. I was hoping it could get pulled to devel.
It’s a combination of connection: local, running in a role and lineinfile that seems to be deadly for me, and the lock would solve all my problems. There should probably be a clearlock function that can be called as a separate task.

Here’s the situation I need it for:
I have my ansible machine as my dhcp server as well. When I add a new network device, the role for the device generate a dhcp line with ansible for zero touch provisioning. I do this by first going a set_fact lookup_template to get the line to add to the dhcp file, then I do lineinfile to make sure that this is the only line in this file with a given mac address.

I run this in a role for all the devices in a class. I don’t want to use serial: 1 in the calling playbook as there are many other steps that want to run in parallel including interactions with the device. The run_once/with_items hack doesn’t work because they are all going to the same system.

thanks,
jerry

Viable workarounds could be to split it in multiple plays in the playbook, then you can use serial: 1 on some.

Or use the assemble module,

create a drop directory where you add all the files and then assemble it to one dhcpd.conf file with the assemble module.

Explain to me how this works when this is the same role playbook?
I want this to be part of what is checked every time the role is run.

jerry

It would be easier to explain with some code, but I'm on vacation so I do not have access to the code I use at work.

On the dhcp server create a directory /etc/dhcp/dhcpd.d (or some other directory)
In here you need to but files with content that in the end is going to be assembles to dhcpd.conf.

So instead of lineinfile you use template module and create a file in this directory with a unique name. This way if you managing several host they will create it one file and no locking is necessary.

After that you just need to assemble all the file in to one dhcpd.conf
- assemble:
     src: /etc/dhcp/dhcpd.d
     dest: /etc/dhcp/dhcpd.conf

The file will be assembles in sorted order so you need to make files with the correct order.

Hopefully this gives you an idea of how to circumvent the lack of file lock in Ansible.

Sorry, I meant how you can do different plays when this is in a role’s main.yml?

I have implemented assemble, but IMO it is an ugly thing when each host generates a one line file. (first time I forgot the run_once on the assemble.)

My bigger question is why has the lock function not been accepted into the project. It solves a type of problem that other things are not as nice at. I never saw a comment as to why it was not accepted, it just sat there and then closed.

jerry

Sorry, I meant how you can do different plays when this is in a role's
main.yml?

I'm sorry, but I don't understand what you mean.

My bigger question is why has the lock function not been accepted into the
project. It solves a type of problem that other things are not as nice at.
I never saw a comment as to why it was not accepted, it just sat there and
then closed.

If you refer to the PR for the lineinfile module, I think that's because that was just for lineinfile.
To solve this is must be a solution for all modules that have this issue, like blockinfile, replace....

One thing I would like to see is the possibility
to set serial: 1 on task level and not just the play level.
That would solve this challenge for me, but serial: 1 on task would not solve it if multiple play is run in parallel and needed to modify the same file.