Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible Get a string from a file and register a variable with that output

Tags:

ansible

My requirement as follows:

  <Resource auth="Container" logAbandoned="true" name="jdbc/InfraForms"
            type="javax.sql.DataSource" url="jdbc:jtds:sqlserver://<MY_SERVER_IP>:143;lastUpdateCount=true;useLOBs=false" 
            username="XXXXX" password="qqqTyIDd7Rd4JahLDjHJH6LvQ=="
            testOnBorrow="true" validationQuery="SELECT 1"  closeMethod="close"/>
  <Manager pathname=""/>
</Context>

Above is the content in one file contact.xml where i need to get value of the password qqqTyIDd7Rd4JahLDjHJH6LvQ==.

How can I achive that through an Ansible task?

like image 731
Devaraju Avatar asked Sep 20 '25 05:09

Devaraju


1 Answers

If you wanted to try to use module that are included you could use the slurp module and the set_fact module and with jinja2 you can extract the password using a regex like such:

- name: Slurp file
  ansible.builtin.slurp:
    src: /your/file
  register: passwordfile

- name: Set Password
  ansible.builtin.set_fact:
    your_password: "{{ passwordfile['content'] | b64decode | regex_findall('\bpassword\b\=\"(.+)\"') }}"
like image 171
Darney Avatar answered Sep 22 '25 18:09

Darney