I'm trying to add the json values into a dynamic ansible playbook via python code, the data getting fetched from mysql, tried below code but it does not append those values in the resulting playbook. However, printing the json values shows that the values have been pulled from mysql database correctly
Here's the snippet of the code, i used jinja2, json
query = "SELECT vm_id, install, uninstall FROM vm_released_applications"
cursor.execute(query)
data = cursor.fetchall()
cursor.close()
connection.close()
return data
def create_dynamic_playbook(template_file, output_file, context):
env = Environment(loader=FileSystemLoader(searchpath="./"))
template = env.get_template(template_file)
playbook_content = template.render(context)
with open(output_file, 'w') as file:
file.write(playbook_content)
def main():
data = fetch_data_from_mysql()
for record in data:
vm_id = record['vm_id']
install = json.loads(record['install']) if record['install'] else []
uninstall = json.loads(record['uninstall']) if record['uninstall'] else []
# Print statements to verify the JSON parsing
print(f"VM ID: {vm_id}")
print(f"Install Data: {json.dumps(install, indent=4)}")
print(f"Uninstall Data: {json.dumps(uninstall, indent=4)}")
context = {
'vm_id': vm_id,
'install': install,
'uninstall': uninstall
}
playbook_filename = f'playbook_vm_{vm_id}.yml'
create_dynamic_playbook('playbook_template.yml.j2', playbook_filename, context)
print(f"Playbook {playbook_filename} created successfully.")
if __name__ == "__main__":
main()
Output of JSON Parsing
VM ID: 43
Install Data: [
"{\"id\":4,\"name\":\"Google Chrome\",\"package_name\":\"googlechrome\",\"version\":\"\"}"
]
Uninstall Data: []
Playbook playbook_vm_43.yml created successfully.
Below is the sample data getting fetched from database, I'm trying to get package_name and name values from install column, the values are stored in .json, in below table i need to get the value of package_name which is 'googlechrome' and same for name which has value "Google Chrome"
id;vm_id;install;uninstall;created_at;updated_at
2;52;["{\"id\":4,\"name\":\"Google Chrome\",\"package_name\":\"googlechrome\",\"version\":\"\"}"];;2024-07-10 08:03:29;2024-07-10 08:03:29
below is the sample playbookfile which I'm storing data into to create dynamic playbook, iterating through each and every package as i add those
---
- name: Playbook for VM {{ vm_id }}
hosts: all
become: true
tasks:
{% for item in install %}
- name: Install {{ item.name }}
win_chocolatey:
name: "{{ item.package_name }}"
state: present
{% endfor %}
{% for item in uninstall %}
- name: Uninstall {{ item.name }}
win_chocolatey:
name: "{{ item.package_name }}"
state: absent
{% endfor %}
Below is the output playbook file in which i desire to add the parsed json values as explained above
---
- name: Playbook for VM 52
hosts: all
become: true
tasks:
- name: Install #name is expected here (Google Chrome)
win_chocolatey:
name: "" #package_name is expected here (googlechrome)
state: present
json.loads(record['install'])gives you a list of strings, each string needs to be then parsed as JSON again. See this answer, particularly the section about "Dealing with embedded data".