How Do You Write an Ansible Playbook to Retrieve Files from a Directory Relative to where the .YAML File Is?

Problem scenario
You want to refer to files in a subdirectory of the parent directory that houses your Ansible playbook. You have Git repositories with .yaml files and subdirectories. You want the Git repo to be transferred to a variety of different servers. The absolute path to the directory where the files are will vary. How do you have a variable or relative path reference in the playbook so as long as the directory structure is in place, your playbook will run correctly?

Solution

  1. This assumes you have a directory called "files" in the directory with your Ansible playbook (e.g., called good.yaml).
  2. In your playbook (e.g., good.yaml) use this notation:

../files/foo.bar

Here is an example:

- copy:
    src: ../files/foo.bar
    dest:  /tmp/excellent.file
    mode: 0777

Leave a comment

Your email address will not be published. Required fields are marked *