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
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
For more information have a look at the copy module documentation.
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: