ansible

Ansible trim string


I'm trying to write a role to install MySQL 8 and get a problem with this:

- name: Extract root password from logs into {{ mysql_root_old_password }} variable
  ansible.builtin.slurp:
    src: "{{ mysql_logfile_path }}"
  register: mysql_root_old_password
  #when: "'mysql' in ansible_facts.packages" 

- name: Extract root password from logs into {{ mysql_root_old_password }} variable
  set_fact:
    mysql_root_old_password: "{{ mysql_root_old_password.content | b64decode | regex_findall('generated for root@localhost: (.*)$', 'multiline=true') }}"
  #when: "'mysqld' in ansible_facts.packages" 

- name: Get Server template
  ansible.builtin.template:                                                                     
    src: "{{ item.name }}.j2" 
    dest: "{{ item.path }}"
  loop:
    - { name: "my.cnf", path: "/root/.my.cnf" }
  notify: 
    - Restart mysqld

on the .my.cnf I get password with quotes and brackets:

[client]
user=root
password=['th6k(gZeJSt4']

How to trim that?

What I try:

- name: trim password
  set_fact:
    mysql_root_old_password2: "{{ mysql_root_old_password | regex_findall('[a-zA-Z0-9,()!@#$%^&*]{12}')}}"

Thanks.


Solution

  • The result of regex_findall is a list because there might be more matches. Take the last item

    - set_fact:
        mysql_root_old_password: "{{ mysql_root_old_password.content|
          b64decode|
          regex_findall('generated for root@localhost: (.*)$', 'multiline=true')|
          last }}"