Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to tell sops lookup to return '' for non-existant files #33

Conversation

felixfontein
Copy link
Collaborator

Motivation

This makes implementing things a lot easier. Instead of first having to do a stat to see if it really exists, one can simply specify empty_on_not_exist=true to the lookup and obtain an empty string instead of an error.

I'm using this in https://github.com/felixfontein/ansible-acme/ (I'm right now working on adding sops-encrypted keys support).

@codecov
Copy link

codecov bot commented Nov 8, 2020

Codecov Report

Merging #33 (c6b396f) into main (810b4c1) will increase coverage by 0.15%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main      #33      +/-   ##
==========================================
+ Coverage   86.29%   86.45%   +0.15%     
==========================================
  Files           6        6              
  Lines         343      347       +4     
  Branches       61       62       +1     
==========================================
+ Hits          296      300       +4     
  Misses         32       32              
  Partials       15       15              
Impacted Files Coverage Δ
plugins/lookup/sops.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 810b4c1...c6b396f. Read the comment docs.

@felixfontein
Copy link
Collaborator Author

FYI, this is a full example using the new option for the sops lookup and sops_encrypted, together with the new openssl_privatekey_pipe module from community.crypto: https://github.com/felixfontein/ansible-acme/blob/40ed248c7d3b18e604bf6f35aa9d9bdf56377b82/roles/acme_certificate/tasks/main.yml#L71-L101

@endorama
Copy link
Collaborator

endorama commented Nov 9, 2020

👍 I did something very similar for our internal use in my company, so I'm definitely going to keep an eye on felixfontein/ansible-acme

Copy link
Collaborator

@endorama endorama left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering if it make sense to update the behaviour instead of adding a new one. I don't think it would break compatibility as due to how it is implemented right now (you must perform a stat before, and with that safe guard you would not run the plugin at all).

What do you think?

@felixfontein
Copy link
Collaborator Author

Hmm, I'm generally no fan of eating up errors when not explicitly being asked so. For calling roles/playbooks, it would be surprising (IMO) if the community.sops.sops lookup returns '' for non-existant files, while the ansible.builtin.file lookup errors out.

Just image if you're writing a private key to a location on the remote server (by combining copy module with the sops lookup), and because you made a typo in the filename you don't get the key written there but an empty file, and you only notice that when some service doesn't come back up after a restart.

@felixfontein
Copy link
Collaborator Author

@endorama what do you think of releasing 0.2.0 soon? I'd really like to be able to use this new option without having to install from git (or even from this branch) :)

felixfontein added a commit to felixfontein/ansible-acme that referenced this pull request Nov 24, 2020
felixfontein added a commit to felixfontein/ansible-acme that referenced this pull request Nov 24, 2020
@felixfontein
Copy link
Collaborator Author

@endorama can you be chance look at this PR again? (Maybe even at the others?) It would be really awesome to have this in a release, it's a lot easier to use than installing collections from a git repo branch (which doesn't work for Ansible 2.9).

@endorama
Copy link
Collaborator

@felixfontein I'm sorry for the long delay, but these notifications were buried under others so I missed them.

Hmm, I'm generally no fan of eating up errors when not explicitly being asked so. For calling roles/playbooks, it would be surprising (IMO) if the community.sops.sops lookup returns '' for non-existant files, while the ansible.builtin.file lookup errors out.

I pretty much agree with this.

From the liked usage in felixfontein/ansible-acme this change would allow using | default(omit, true) pattern, which is not now possible due to the triggered error, is this correct?

I'm specifically referring to these lines: https://github.com/felixfontein/ansible-acme/blob/40ed248c7d3b18e604bf6f35aa9d9bdf56377b82/roles/acme_certificate/tasks/main.yml#L76-L80

This looks a clean and useful, so I'm definitely ok with the change. I also agree is better to have it under a specific option to explicit the behaviour.

@felixfontein
Copy link
Collaborator Author

From the liked usage in felixfontein/ansible-acme this change would allow using | default(omit, true) pattern, which is not now possible due to the triggered error, is this correct?

Exactly. Otherwise you first would have to use a set_fact task with register and ignore_errors: true, and then in a next task check whether set_fact failed or not, and depending on that use the new fact or omit. Which is not really fun :)

@felixfontein felixfontein merged commit 00fd7e8 into ansible-collections:main Dec 12, 2020
@felixfontein felixfontein deleted the empty-string-for-non-existant branch December 12, 2020 17:58
@felixfontein
Copy link
Collaborator Author

@endorama thanks for reviewing this one as well!

@felixfontein
Copy link
Collaborator Author

@endorama do you mind if I release a 0.2.0 version of the collection? (Or do you want to do that yourself?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants