apenella / go-ansible

Go-ansible is a Go package that enables the execution of ansible-playbook or ansible commands directly from Golang applications. It supports a wide range of options for each command, enabling smooth integration of Ansible functionality into your projects.
MIT License
905 stars 143 forks source link

How to pass ansible environment variables for the ansible playbook? #82

Closed ranjanp75 closed 2 years ago

ranjanp75 commented 2 years ago

Hi @apenella, I am trying to understand how I can set the env variables for running Ansible playbook. I tried setting the env variable "ANSIBLE_LOG_PATH" by doing os.Setenv("ANSIBLE_LOG_PATH", "test.log") before triggering the following command. The ansible does not seem to log even with the env set. Could you please help me with this?

The code snippet is as follows


        os.Setenv("DEFAULT_LOG_PATH", "test.log")  
    ansibleConnectionOption := &playbookOptions.AnsibleConnectionOptions{
        Connection: "local",
    }

    ansiblePlaybookOptions := &playbook.AnsiblePlaybookOptions{
        ExtraVars: extraVars,
    }

    playbook := &playbook.AnsiblePlaybookCmd{
        Playbooks:         []string{playFile},
        ConnectionOptions: ansibleConnectionOption,
        Options:           ansiblePlaybookOptions,
    }

    return playbook.Run(context.Background())```
apenella commented 2 years ago

Hi @ranjanp75 You are not using the proper environment variable, to configure the default log path. ansible documentation could be misunderstood, because the name of the parameter and the name of environment variable that sets that parameter, does not have the same name. You need to use ANSIBLE_LOG_PATH Take a look at these: https://docs.ansible.com/ansible/latest/reference_appendices/config.html#default-log-path

Just to let you know, thanks to @alexandrevilain, who has prepared #81, there will be soon on master a better way to set up environment variables on DefaultExecutor.

apenella commented 2 years ago

hi @ranjanp75 there is any update about this issue?

apenella commented 2 years ago

@ranjanp75 since there is no update on that issue, I am going to close it