1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Run a Python script main.py with a config file from Ansible

Discussão em 'Python' iniciado por Stack, Setembro 23, 2024.

  1. Stack

    Stack Membro Participativo

    I have created several Python scripts, including a main.py script that sequentially runs 4 other Python scripts.

    I need to deploy and run them using Ansible. The issue is that I can't use ansible.builtin.script because only main.py is used and not the others, so I'm using ansible.builtin.command.

    My problem is that I'm using a config file variables.json, which I first populate with ansible.builtin.template, and then it's sent to the files directory of my role, where the Python scripts are located. But every time, in the return code (rc) of my Python script, I get "undefined variables". I think my scripts can't "see" the config file.

    Can you help me, please?

    Thank you! :)

    `---
    - name: Generate the JSON configuration file
    template:
    src: variable.json.j2
    dest: "{{ input_path }}/variable.json" # Generate the variable.json file in /tmp

    - name: Copy all necessary files to the server
    ansible.builtin.copy:
    src: "{{ item }}"
    dest: "{{ input_path }}//{{ item }}"
    loop:
    - main.py
    - migration_zabbix_dezip_config_file.py
    - migration_zabbix_hostname_recovery.py
    - migration_zabbix_create_csv_file.py
    delegate_to: localhost

    - name: Run the main.py script
    ansible.builtin.command:
    cmd: python3 "{{ input_path }}//main.py"`

    Continue reading...

Compartilhe esta Página