wordpress api unique keys/salts not properly inserted with lineinfile and uri module in ansible

Lethargos asked:

This is what I’m using to insert the text in wp-config:

- name: insert unique key and salts in wp-config
    path: /var/www/wordpress/wp-config.php
    regex: "put your unique phrase here"
    insertafter: "put your unique phrase here"
    line: "{{ api_info }}"

The api_info variables comes from main.yml under “handles”:

 - name: get API information for wp-config
    url: "https://api.wordpress.org/secret-key/1.1/salt/"
    return_content: yes
   register: api_info

The problem with this setup is that, instead of getting the nicely formatted keys wordpress provides, I get the text below. As you can see, there’s also meta information, besides the content itself. Also \n is not translated into a new line but inserted as such:

{‘status’: 200, ‘cookies’: {}, ‘date’: ‘Thu, 25 Oct 2018 19:57:00 GMT’, ‘url’: ‘https://api.wordpress.org/secret-key/1.1/salt/‘, ‘transfer_encoding’: ‘chunked’, ‘changed’: False, ‘server’: ‘nginx’, ‘content’: “define(‘AUTH_KEY’, ‘7Y1}w1mmjo ))zyP(E,[email protected]: %[email protected]#Rj,%[email protected][8^/]aZHj!Wpz-‘);\ndefine(‘SECURE_AUTH_KEY’, ‘1e>3Msn1,~E/^.R}hx7R%GE}j/8q_;^qL|XcF|y#e2<Kr;:n%+BwWQ$,U*0%Na’);\ndefine(‘LOGGED_IN_KEY’, ‘8UJMH[email protected]~l]+_O0(/HTz3wCGT=-#$++].bX Ry;A1+yw=>Acokm?)$+gneve+c db,/TQkR!262-9>-m’);\ndefine(‘SECURE_AUTH_SALT’, ‘#n-i=.{7} [z3/-ay*qb+boD2D^nVjTqjUsKr,@3ACUxL).Un2K!Pf2|&’);\ndefine(‘LOGGED_IN_SALT’, ‘+-!T;,|l,HNly(tA^[email protected]’);\n”, ‘failed’: False, ‘connection’: ‘close’, ‘content_type’: ‘text/plain;charset=utf-8’, ‘msg’: ‘OK (unknown bytes)’, ‘redirected’: False, ‘x_frame_options’: ‘SAMEORIGIN’, ‘cookies_string’: ”}

My answer:

The content you desire is plainly there, in api_info.content, so that is what you should insert.

    line: "{{ api_info.content }}"

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.