zoomer
zoomer

Reputation: 87

ansible - If files has different values then update them, otherwise ignore

I have been struggling to get this logic correct now for weeks .. What I am trying to do is create an ansible playbook that will install my application on remote hosts ... I have that bit working ...

Now the problem is, like with all app installs it means i need to do a sanity check and make sure that my config is correct.

One of the things I am struggling with is if i have a massive chunk of text (like the one below i need to make sure that the values in that are correct

I am using the Ansible Replace and Lineinfile modules for other things but i dont think it is correct to use those for what I am trying to achieve with this issue I am having ... Has anyone else done something like this where you have a playbook looking at a file and only if the value is not correct update it

        <Set name="env"><SystemProperty name="address" default="0.0.0.0"/></Set>
        <Set name="port"><SystemProperty name="https" default="6328" /></Set>
        <Set name="idle">3</Set>
        <Set name="soLingerTime"><Property name="http" default="-1568"/></Set>
        <Set name="acceptorDelta"><Property name="ssol" default="9524"/></Set>
        <Set name="PriorityDelta"><Property name="ssl34" default="9635"/></Set>
        <Set name="Size">15874</Set>

Upvotes: 0

Views: 1528

Answers (2)

zoomer
zoomer

Reputation: 87

To achieve what I want best thing to do is use templates.... Get the new files, update as needed.... Save them in templates and move across....

Upvotes: 0

Michael
Michael

Reputation: 61

The elegant solution would probably be to just replace the values with {{ variables }} and then let ansible write the correct values with the template module. If your files adhere to some kind of standard (the example looks like XML?), can you use the xml module?

Otherwise, yeah, I can't think of a better idea than lineinfile/replace either..


EDIT to add xml example:

I'm no xpath expert, but I suppose something like this would (sort of) work..

- hosts: localhost
  tasks:
  - name: Sanity Checks
    xml:
      path: file.xml
      xpath: /Set[@name='port']/SystemProperty[@name='https']
      attribute: default
      value: "1000"

Upvotes: 1

Related Questions