Problem replace with variable

Hi

i want to create a mariaDB active - active replication. And so i need to modify the my.cnf with ansible.
I have two hosts both are defined undser the host variables with:

File for host 1:

default_ip: 127.0.0.1
ansible_ssh_host: 127.0.0.1
host_id : 7

File for host 2:
default_ip: 127.0.0.2
ansible_ssh_host: 127.0.0.2
host_id : 8

in my playbook i copy the my.cnf template from local to both hosts and want to modify it:

  • name: modify it
    replace:
    path: “{{ mysql_conf_dir }}/my.cnf”
    regexp: ‘server-id = 7’
    replace: ‘server-id = {{ hostvars.host_id }}’

i got:

[WARNING]: While constructing a mapping from /mnt/c/svn/SystemsEngineering/Ansible/UBS-OST/roles/maria_cluster/tasks/main.yml, line 15, column 3, found a duplicate dict key (replace). Using last defined value
only.

[WARNING]: Ignoring invalid attribute: path

[WARNING]: Ignoring invalid attribute: regexp

fatal: [ubsostvm001.ka2.smarthouse.local]: FAILED! => {“changed”: false, “msg”: “missing required arguments: path, regexp”}

fatal: [ubsostvm001.ffm3.smarthouse.local]: FAILED! => {“changed”: false, “msg”: “missing required arguments: path, regexp”}

What is the correct way to set an host id in the my.cnf based on the host number.

Your indentation is wrong, path, regexp and the second replace is a property of replace not the task so you are missing 2 spaces on those lines.

In YAML indentation is crucial because it has meaning.
Look at the examples in the documentation and pay attention to the indentation.

Looks like the indentation is off - that's why you get 'invalid
attribute', and 'missing required arguments'.
But even if you indent correctly, I think it'll still fail.
The regex you're using has a ton of spaces in it and hence looks
rather fragile.
Also, you don't seem to be making use of any specific regex features.

So the 'replace' is indeed probably not the best way to lay out your
my.cnf file.
Some simpler options come to mind:

The 'template' module.
The 'line_in_file' module.

Dick

Thanx.

I have found an other way. The my.cnf comes from me. As a template.
I put a field with the server id into my server variables, and refer it in the template.

Server:
default_ip: 127.0.0.1
ansible_ssh_host: 127.0.0.1
mysql_db_id : 7

and in my template i say:
server-id = {{ mysql_db_id }}

thats solve this problem for me.

And yes, the regex part was horrible and wrong, but this because i dont unterstand regex. For me this concept i like dealing with hell. :wink:

greetings
Henrik