0

I have config files that come from a pre-existing deployment process. (Varying formats, but java.properties files comprise most of them, if it matters.)

I need to change (if the corresponding property exists), or add (if it doesn't) multiple properties in these files.

However, I have no control over the creation of the pre-existing files. They are created externally, and the variables I want to modify are scattered throughout the lines of the file.

It looks like blockinfile won't be useful here, since it seems to work on contiguous blocks of properties (is that correct?)

Does this mean I need to use multiple lineinfile declarations (or one using a with_items loop as per: ansible: lineinfile for several lines?)?

Or is there a better more canonical way to deal with this.

For example, consider the following pre-existing file.

config.item_1=bear1
config.item_2=bear2
config.item_3=bear3
config.item_4=bear4
config.item_5=bear5

And I just want to modify config.item1, config.item_3 to be (dog1, cat1). I also want to add config.arbitrary.param.name=40 resulting in:

config.item_1=dog1
config.item_2=bear2
config.item_3=cat1
config.item_4=bear4
config.item_5=bear5
config.arbitrary.param.name=40
user1848244
  • 425
  • 3
  • 12
  • What is the exact problem with using `lineinfile` with a loop? – Zeitounator Aug 24 '20 at 06:20
  • Nothing. The ask is just what is the canonical way to do this. If it is that, then that’s fine. I just wanted to make sure there wasn’t something else that most people tend to do for this use case. – user1848244 Aug 24 '20 at 08:09
  • 1
    The best is to be in complete control of your configuration file and use a template. If this is not possible and you just want to make sure some sporadic lines are present/absent, use lineinfile. If this is to control a full block of contiguous lines, use blockinfile. – Zeitounator Aug 24 '20 at 08:15

0 Answers0