I'm having trouble getting variables from ansible. I might be misunderstanding it all, but;
I have a play, creating 3 mysql instances that I'd like to get set new root pass from temporary pass in mysql log.
I can do it with one instance, but when I try to loop through my 3 instances it kills me.
The play:
- name: "Percona new install password retrieval"
block:
- name: Parse temporary password from mysql log
become: yes
shell: "cat /var/log/mysql-instance{{ item }}/error.log | sed -n 's/.*temporary password is generated for root@localhost: //p'"
register: temppass
with_sequence: start=1 end=3
tags: hest888
- debug: var=temppass.results
tags: hest888
this gets the temporary passwords and stores in variable temppass. temppass.results show:
ok: [X-sql08] => {
"temppass.results": [
{
"_ansible_ignore_errors": null,
"_ansible_item_label": "1",
"_ansible_item_result": true,
"_ansible_no_log": false,
"_ansible_parsed": true,
"changed": true,
"cmd": "grep 'A temporary password is generated for root@localhost' /var/log/mysql-instance1/error.log | awk -F ' ' '{print $(NF)}'",
"delta": "0:00:00.004820",
"end": "2020-01-21 16:37:54.164040",
"failed": false,
"invocation": {
"module_args": {
"_raw_params": "grep 'A temporary password is generated for root@localhost' /var/log/mysql-instance1/error.log | awk -F ' ' '{print $(NF)}'",
"_uses_shell": true,
"argv": null,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"warn": true
}
},
"item": "1",
"rc": 0,
"start": "2020-01-21 16:37:54.159220",
"stderr": "",
"stderr_lines": [],
"stdout": "(7FJWDd8uEfs",
"stdout_lines": [
"(7FJWDd8uEfs"
]
},
{
"_ansible_ignore_errors": null,
"_ansible_item_label": "2",
"_ansible_item_result": true,
"_ansible_no_log": false,
"_ansible_parsed": true,
"changed": true,
"cmd": "grep 'A temporary password is generated for root@localhost' /var/log/mysql-instance2/error.log | awk -F ' ' '{print $(NF)}'",
"delta": "0:00:00.004983",
"end": "2020-01-21 16:37:54.621576",
"failed": false,
"invocation": {
"module_args": {
"_raw_params": "grep 'A temporary password is generated for root@localhost' /var/log/mysql-instance2/error.log | awk -F ' ' '{print $(NF)}'",
"_uses_shell": true,
"argv": null,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"warn": true
}
},
"item": "2",
"rc": 0,
"start": "2020-01-21 16:37:54.616593",
"stderr": "",
"stderr_lines": [],
"stdout": "mwY9h1y17r+D",
"stdout_lines": [
"mwY9h1y17r+D"
]
},
{
"_ansible_ignore_errors": null,
"_ansible_item_label": "3",
"_ansible_item_result": true,
"_ansible_no_log": false,
"_ansible_parsed": true,
"changed": true,
"cmd": "grep 'A temporary password is generated for root@localhost' /var/log/mysql-instance3/error.log | awk -F ' ' '{print $(NF)}'",
"delta": "0:00:00.004704",
"end": "2020-01-21 16:37:55.073540",
"failed": false,
"invocation": {
"module_args": {
"_raw_params": "grep 'A temporary password is generated for root@localhost' /var/log/mysql-instance3/error.log | awk -F ' ' '{print $(NF)}'",
"_uses_shell": true,
"argv": null,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"warn": true
}
},
"item": "3",
"rc": 0,
"start": "2020-01-21 16:37:55.068836",
"stderr": "",
"stderr_lines": [],
"stdout": "GZ!xq=ard9mz",
"stdout_lines": [
"GZ!xq=ard9mz"
]
}
]
}
ok ... so the temppass.results have all the info that I need, but i cant seem to get to it ?
what i would like to do ( in the perfect world completely aligned to serve my every whim ) is to get the "item" and "stdout" result from the temppass.results and use them as input to the update password section.
- name: Set root password using temp password to log in
shell: 'mysql -e --protocol=TCP -P {{ item.port }}"ALTER USER ''root''@''localhost'' \
IDENTIFIED WITH mysql_native_password AS ''{{ mysql_root_hash }}'';" \
--connect-expired-password -uroot -p"{{ temppass_{{ item.instance }}.stdout }}"'
with_items:
- { instance: 1, port: 3306 }
- { instance: 2, port: 3307 }
- { instance: 3, port: 3308 }
tags: hest888
or something in that nature.
I have found that the double {{ in double {{ doesn't work but hope you get an idea of what I'm trying to do.
Any ideas ?