Vadiem Janssens
Vadiem Janssens

Reputation: 4109

Ansible copy module ignores role files directory

I'm having the following issue and am not sure if it's a bug or my setup is wrong. I've created a role ssh with the following structure:

.
├── roles
    ├── ssh
    │   ├── files
    │   │   └── sshd_config
    │   └── tasks
    │       └── main.yml

The main.yml file looks like this:

---
- hosts: all
  tasks:
    - name: "Set sshd configuration"
      copy:
        src: sshd_config
        dest: /etc/ssh/sshd_config

Because sshd_config is stored in the recommended files directory, I expected the copy command to automatically fetch that file when referencing it from the task. Instead, Ansible looks for sshd_config in the following directories:

ansible.errors.AnsibleFileNotFound: Could not find or access 'sshd_config'
Searched in:
    <redacted>/roles/ssh/tasks/files/sshd_config
    <redacted>/roles/ssh/tasks/sshd_config
    <redacted>/roles/ssh/tasks/files/sshd_config
    <redacted>/roles/ssh/tasks/sshd_config on the Ansible

Notice it does look in a files directory, but does so in the tasks folder!

Main goal is to send a local file (on my host machine) to the remote server.

I run the playbook with following command:

ansible-playbook -i hosts ./roles/ssh/tasks/main.yml -vvv

Questions:

  1. Is my assumption right Ansible should look for the file in the files directory adjacent to tasks directory?
  2. Did I mess up my setup?

Upvotes: 1

Views: 770

Answers (1)

Tupteq
Tupteq

Reputation: 3105

I think you confused roles with playbooks. You created a playbook in place where the role should be. You rather should create a role and then create a playbook (outside of /roles dir) that uses it.

Here's example /roles/ssh/tasks/main.yml:

- name: "Set sshd configuration"
  copy:
    src: sshd_config
    dest: /etc/ssh/sshd_config

and playbook using ssh role:

---
- hosts: all
  tasks:
   - import_role:
        name: ssh

Upvotes: 2

Related Questions