# Ansible-vault encrypt; return value if already encrypted

**URL:** <https://forum.ansible.com/t/ansible-vault-encrypt-return-value-if-already-encrypted/7624>\
**Category:** Get Help\
**Tags:** ansible-vault\
**Created:** [August 4, 2024, 9:01pm UTC](https://forum.ansible.com/t/ansible-vault-encrypt-return-value-if-already-encrypted/7624 "2024-08-04T21:01:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![DengelFred](https://avatars.discourse-cdn.com/v4/letter/d/87869e/32.png) [@DengelFred](https://forum.ansible.com/u/DengelFred)\
**Post date:** [August 4, 2024, 9:02pm UTC](https://forum.ansible.com/t/ansible-vault-encrypt-return-value-if-already-encrypted/7624/1 "2024-08-04T21:02:00Z")

</div>

Hello,

I would like to run the encryption of some files automatically during git commit.  
Unfortunately ansible-vault returns an error if the files are already encrypted, but this is obviously not an error, it’s the expected state.  
But I need a return value of 0 (even if the files are already encrypted) in order to run it during the pre-commit checks.  
Any Idea how to do that?

Thanks a lot 🙂  
Thomas

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [August 5, 2024, 2:31pm UTC](https://forum.ansible.com/t/ansible-vault-encrypt-return-value-if-already-encrypted/7624/2 "2024-08-05T14:31:02Z")

</div>

No, not currently, but you can ‘skip’ those files if you use the ‘vault\_encrypted’ test:

```auto
- shell: ansible-vault .... {{ item}}
  when: '{{ lookup("file", item) is not vault_encrypted}}'
  loop: ' {{ myfiles}}'

```

---

<div class="post-metadata">

**Author:** ![DengelFred](https://avatars.discourse-cdn.com/v4/letter/d/87869e/32.png) [@DengelFred](https://forum.ansible.com/u/DengelFred)\
**Post date:** [August 6, 2024, 3:52pm UTC](https://forum.ansible.com/t/ansible-vault-encrypt-return-value-if-already-encrypted/7624/3 "2024-08-06T15:52:24Z")

</div>

Thanks a lot!  
I need this in a shell script (git hook) - Is there such a test for the shell as well?

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [August 6, 2024, 4:10pm UTC](https://forum.ansible.com/t/ansible-vault-encrypt-return-value-if-already-encrypted/7624/4 "2024-08-06T16:10:01Z")

</div>

adding a ‘vaulted file test’ [Add vaulted\_file test by bcoca · Pull Request #83717 · ansible/ansible · GitHub](https://github.com/ansible/ansible/pull/83717)

But to test from shell, use ansible? just have a playbook with an assert and have it pass/fail as needed
