1

I am new to ansible and I have problems when I want to replace variables in a configuration file. The case is that I have tags in this file to be replaced by the value found in ansible-vault that has the same name as the tag in the configuration file.

the configuration file looks like this:

mongo.uri=<%=@dbruchost%> 
mongo.replica.set=set0
mongo.database=<%=@dbrucdb%>
mongo.user=<%=@dbrucuser%>
mongo.password=<%=@dbrucpass%>

and the ansible-vault is as follows

vars:
    dbruchost: "test.test:27017"
    replica.set: "set0"
    dbrucdb: "database1"
    dbrucuser: "data"
    dbrucpass: "d4t4"
    jenkinsuser: "jenkinstest"
    jenkinspassword: "j3nkins"

Actually I change the variables one by one with regex

- name: Replace uri
  replace:
    path: /tmp/artifacts/surveyMonkey/application.properties
    regexp: "<%=@dbruchost%>"
    replace: vars.dbruchost

But I would like to do it dynamically. Is there any possibility for ansible to read the tags from the application properties and look for them in the ansible-vault and replace them?

Dalio141
  • 101
  • 1
  • 1
  • 7

1 Answers1

1

why dont use template for your config file:

use a template file.j2 like this for example:

mongo.uri={{ dbruchost }} 
mongo.replica.set=set0
mongo.database={{ dbrucdb }}
mongo.user={{ dbrucuser }}
mongo.password={{ dbrucpass }}

you add a task to create you config file with the module template

a sample of task: put your config.j2 in templates dir

  tasks: 
    - name: Dump all variables
      template:
        src: file.j2 
        dest: /tmp/artifacts/surveyMonkey/application.properties

i suppose you have access to your vault variables

Frenchy
  • 16,386
  • 3
  • 16
  • 39
  • Do you have a link to this? I would like to take a look at it as I am not a java user @Frenchy – Dalio141 Nov 10 '21 at 12:32
  • you want a sample of task to use template? so i dont know how you trap your vars vault? – Frenchy Nov 10 '21 at 12:33
  • my idea is to use the ansible vault to use it as a variable repository since the configuration files of my applications are completely different. That is why I use ansible vault since the playbook I want to use would be common for all java deployments. I don't think that java templates will be useful for me. – Dalio141 Nov 10 '21 at 12:41
  • i jsut explain how build a template in ansible, with the informations you give, i cant guess if its ok or not for you environnment i just answer to your question and i dont see where is java here.. the template does the samething than your replace task in one time. Show your complete playbook – Frenchy Nov 10 '21 at 12:45