How to encrypt a file on the remote host using Ad-Hoc command 'ansible-vault'.

for example, a myfile.txt on a remote machine needs to be encrypted from the host machine

  • 1
    What encryption command do you envision? What happened when you tried your existing solution? – Jeff Schaller May 1 '19 at 12:58

It sounds like you are misusing Ansible Vault.

The Ansible Vault Documentation states:

Ansible Vault is a feature of ansible that allows you to keep sensitive data such as passwords or keys in encrypted files, rather than as plaintext in playbooks or roles.

You are not able to encrypt files directly on the remote host.

What you are able to do is to encrypt a file on your local host and then copy the encrypted file using the copy module onto the remote host. By default, the copy module will decrypt the file, but you can overwrite this by specifying decrypt: no. For more information have a look at the copy module documentation.

| improve this answer | |
  • 1
    Nice answer. Welcome to U&L. – 0xSheepdog May 1 '19 at 14:50
  • @0xSheepdog, thanks for completing my answer :) – fgk May 1 '19 at 17:22

To add on to the answer @fgk posted (please consider their answer, this is simply extending the info), ansible-vault is not a general file encryption utility. It is specifically for encrypting information for use within the Ansible family of commands, to protect sensitive data that would otherwise have to be stored plain-text in the playbooks and configuration files.

If you want to encrypt files on a remote host, using ansible to execute the task, you need to find an appropriate file encryption utility and use it to complete the encryption, then automate that process via ansible.

So, two major tasks:

  1. Determine how to encrypt a file on a system. Here is a good example of how it can be done.
  2. Automate the process from step one via tasks in an ansible playbook. Another fine set of examples.
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.