0

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 ?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
MSchultz
  • 3
  • 3

1 Answers1

0

Below is an answer taking a slighty different approach that yours not requiring to pick elements in your results based on its index. If you still want to understand how to write expressions to target such elements, you can have a look at this other answer where there is an explanation.


  1. Create a var in you playbook that will hold your mysql instances data (rather than hardcoding it in your last task loop, we will reuse this)
    mysql_instances:
      - instance: 1
        port: 3306
      - instance: 2
        port: 3307
      - instance: 3
        port: 3308
    
  2. Use that var rather than a range to loop over your log files. Note that when you debug the result, the item field inside each result is now reported as the current item (i.e a mysql instance definition) being looped over

    - name: Parse temporary password from mysql log
      become: yes
      shell: "cat /var/log/mysql-instance{{ item.instance }}/error.log | sed -n 's/.*temporary password is generated for root@localhost: //p'"
      register: temppass
      loop: "{{ mysql_instances }}"
    
  3. You now have all the info you need in each temppass.results (captured password, instance number and port). You just have to loop over it for to set your password. (Note: I kept your example but you might want to have a look at the mysql_user module rather than using shell)

    - name: Set root password using temp password to log in
      shell: 'mysql -e --protocol=TCP -P {{ item.item.port }}"ALTER USER ''root''@''localhost'' \
             IDENTIFIED WITH mysql_native_password AS ''{{ mysql_root_hash }}'';" \
             --connect-expired-password -uroot -p"{{ item.stdout }}"'
     loop: "{{ temppass.results }}"
    
Zeitounator
  • 38,476
  • 7
  • 53
  • 66