lineinfile when two lines are the same

I’m installing a piece of software that requires me to edit a user=xyz line into a monolithic config file that is .ini like in format.

[function-x]

user=myuserhere

[function-y]

other stuff here

more stuff here

[function-z]

user=myuserhere

So suppose I have a config that requires me to uncomment the line in [function-Z] and substitute in the right value for the username.

How do you specify which of the two identical lines in the file that you want to edit ?
Any suggestions ?

Would it be acceptable to template that config file? Perhaps save that chunk only, in a templated file that would be later on included.

Try the ini_file module (http://docs.ansible.com/ini_file_module.html) instead of lineinfile.

You want something like this, I think:

- ini_file:
  dest=/etc/anotherconf
        section=function-z
        option=user
        value=myuserhere
        backup=yes

-A