look up variables and change them from an ansible vault dynamically

Viewed 142

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?

1 Answers

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

Related