Ansible: Set variable to file content


I’m using the ec2 module with ansible-playbook I want to set a variable to the contents of a file. Here’s how I’m currently doing it.

  1. Var with the filename
  2. shell task to cat the file
  3. use the result of the cat to pass to the ec2 module.

Example contents of my playbook.

I assume there’s a much easier way to do this, but I couldn’t find it while searching Ansible docs.


You can use lookups in Ansible in order to get the contents of a file, e.g.

Caveat: This lookup will work with local files, not remote files.

Here’s a complete example from the docs:

Leave a Reply