sadv1r / ansible-vault-editor-idea-plugin

Ansible Vault Editor IntelliJ Plugin with auto encryption/decryption
https://plugins.jetbrains.com/plugin/14278-ansible-vault-editor
MIT License
37 stars 5 forks source link

read ansible.cfg from project/current directory #302

Open foto-andreas opened 1 month ago

foto-andreas commented 1 month ago

Hi,

is this only a forgotten TODO or did you encounter problems implementing the usage of ansible.config in current directory?

64 | //TODO ansible.cfg (in the current directory)
65 |  
66 | // in the home directory
67 | getPasswordFilePathFromConfig(SystemProperties.getUserHome() + "/.ansible.cfg")

Andreas

foto-andreas commented 1 month ago

Adding my idea:

    // in project directory
    for (p in ProjectManager.getInstance().openProjects) {
        for (r in ProjectRootManager.getInstance(p).contentRoots) {
            getPasswordFilePathFromConfig(r.path + "/ansible.cfg")
                ?.let { return r.path + "/" + it }
        }
        break;
    }

    // in current directory
    getPasswordFilePathFromConfig("ansible.cfg")
        ?.let { return File(it).absolutePath }

Beware. I have no idea on the correct IJ-APIs to use, but this solution works for me.

Open:

Andreas

foto-andreas commented 1 month ago

working on pull request #304 on it

Grogdunn commented 1 month ago

In our company we use to have all ansible files under subdirectory, this PR can support this? Eg:

[root]/ansible/ansible.cfg
or
[root]/infrastructure/ansible/ansible.cfg
foto-andreas commented 1 month ago

not currently, only if the vaulted files are also in that directory. how will ansible-playbook find these files when called?

Grogdunn commented 1 month ago

Simply we have a Makefile with a target like this:

deploy: build
    ANSIBLE_CONFIG=ansible/ansible.cfg ansible-playbook \
        --private-key ~/.ssh/superkey.pem \
        -i ansible/inventories/prod/hosts \
        --user admin \
        ansible/main.yml

or simple cd and execute the playbook

foto-andreas commented 1 month ago

Ah, ANSIBLE_CONFIG environment variable. I think this is already supported. just place vault_password_file property in the ansible.cfg or set a second environment variable ANSIBLE_VAULT_PASSWORD_FILE to the password file path.

Grogdunn commented 1 month ago

How to configure IDEA with ANSIBLE_CONFIG? I don't know.

foto-andreas commented 1 month ago

For complex setups: https://plugins.jetbrains.com/plugin/14353-ansible-vault-integration/tutorials/vault-file-as-script

sadv1r commented 1 month ago

How to configure IDEA with ANSIBLE_CONFIG? I don't know.

Hi 👋!

You can set an environment variable on the system level, like for the default tool.

sadv1r commented 1 month ago

@foto-andreas, thanks for the issue. I really forgot about that to-do.

I'm trying to stick with the default locations from Ansible documentation, but even complex setups supported using a password file with a script, where we can do literally anything.

Grogdunn commented 1 month ago

How to configure IDEA with ANSIBLE_CONFIG? I don't know.

Hi 👋!

You can set an environment variable on the system level, like for the default tool.

Yes and no... Because I need to set multiple time environment variable (that is global) once for each project I open, and sometimes I open 2 to 4 projects, at same time, all with ansible inside (and a lot of other things), and sometimes ansible.cfg files are under a path or sometimes under some "funky" path. And cherry on the cacke, each project has a different vault password file. So I can specify the ansible.cfg file position per project or can be done a sort of a discovery on project paths/index.

What do you think?