gaetano
gaetano

Reputation: 907

Ansible collection usage

I am trying to use Ansible Collection for example the nginx one.

The directory tree structure looks like this:

    ├── ansible_collections
    │   └── nginxinc
    │       └── nginx_core
    │           ├── CHANGELOG.md
.......
    │           ├── README.md
    │           └── roles
    │               ├── nginx
    │               │   ├── tasks
    │               │   │   ├── amplify
    │               │   │   ├── config
    │               │   │   ├── keys
    │               │   │   ├── main.yml
    │               │   │   ├── modules
    │               │   │   ├── opensource
    │               │   │   │   └── install-debian.yml
    │               │   │   └── unit
    ....
        ├── hosts
        └── site.yaml

the site.yaml file I wrote is:

- name: Demo
  hosts: all
  connection: local
  gather_facts: no

  tasks:
  - name: test
    include_role:
      name: nginxinc.nginx_core.nginx
      tasks_from: install-debian

I am trying to run the task install-debian from the role nginx.

I run the playbook:

ansible-playbook -i hosts site.yaml

I get this error:

ERROR! the role 'nginxinc.nginx_core.nginx' was not found.....

I need help on how I should fix the site.yaml file

Upvotes: 2

Views: 1300

Answers (2)

MaxiReglisse
MaxiReglisse

Reputation: 3336

I regret to say that this is not working for me. this gives the impression that collections_paths is not used.

ansible --version

ansible 2.9.17

ansible-config view

[defaults]
inventory=/usr/local/ansible-admin/my_hosts
roles_path=/usr/local/galaxy-roles:./roles
collections_paths=/usr/local/ansible_collections
log_path=./ansible.log

the collections are installed in the /usr/local/ansible_collections folder:

tree -L 2 /usr/local/ansible_collections/nginxinc/

/usr/local/ansible_collections/nginxinc/
└── nginx_core
    ├── CHANGELOG.md
    ├── CODE_OF_CONDUCT.md
    ├── CONTRIBUTING.md
    ├── docs
    ├── FILES.json
    ├── LICENSE
    ├── MANIFEST.json
    ├── playbooks
    ├── plugins
    ├── README.md
    └── roles

here is the very basic content of the playbook:

cat playbooks/nginx_core.yml

- name: Test collection ansible with nginxinc
 hosts: "{{ target }}"
  collections:
    - nginxinc.nginx_core
  tasks:
    - import_role:
        name: nginx

we get the following error message when it is launched:

ansible-playbook playbooks/nginx_core.yml --extra-vars target=myvm.mydomain.org

ERROR! the role 'nginx' was not found in nginxinc.nginx_core:ansible.legacy:/usr/local/ansible-admin/playbooks/roles:/usr/local/galaxy-roles:/usr/local/ansible-admin/roles:/usr/local/ansible-admin/playbooks

it doesn't find the role in the collections, and worse, it doesn't say that it looked in the collections_path...

But here is a solution that works, but it is very very ugly: add the nginx role of the collection in roles_path!

roles_path=/usr/local/galaxy-roles:./roles:/usr/local/ansible_collections/nginxinc/nginx_core/roles

warning: this is obviously a misuse of the ansible collections!

any help would be appreciated.

Ernest.

Upvotes: 1

kir0ul
kir0ul

Reputation: 69

If I understand correctly, you should just install the Nginx collection with the following command, as explained here:

ansible-galaxy collection install nginxinc.nginx_core

It should install it in ~/.ansible/collections/ansible_collections/nginxinc/nginx_core/. Then create a playbook following these examples and the Ansible docs:

---
- hosts: all
  collections:
    - nginxinc.nginx_core
  roles:
    - role: nginx

Finally run your playbook:

ansible-playbook -i hosts my_nginx_playbook.yaml

It'll pick the Debian version for you if your host is Debian.

Upvotes: 2

Related Questions