0

I want to add one role in my playbook dependencies, but based on condition.

- name: Get all install pyenv versions
  command: '{{ pyenv_root }}bin/pyenv versions'
  register: available_versions
  tags:
      - get_pyenv_versions
  environment:
      PYENV_ROOT: "{{ pyenv_root }}"

dependencies:
    - { role: pyenv, python_versions: ["{{ mypython_version }}"], when: "mypython_version not in available_versions.stdout" }

What I want to do is, I want to check, all available pyenv versions, if mypython_version is not available, then only I want to invoke pyenv role, otherwise I dont want to invoke that.

It gives me syntax error

ERROR! Syntax Error while loading YAML.


The error appears to have been in '/root/ansible_playbooks/roles/mydeployment/meta/main.yaml': line 9, column 1, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:


dependencies:
^ here

exception type: <class 'yaml.parser.ParserError'>
exception: while parsing a block collection
  in "<unicode string>", line 1, column 1
did not find expected '-' indicator
  in "<unicode string>", line 9, column 1

How can I define variable get_pyenv_versions and use in my dependencies as condition?

Nilesh
  • 20,521
  • 16
  • 92
  • 148
  • not familiar with role dependencies, but the [documentation](http://docs.ansible.com/ansible/latest/user_guide/playbooks_reuse_roles.html#role-dependencies) says: `Role dependencies allow you to automatically pull in other roles when using a role. Role dependencies are stored in the meta/main.yml file contained within the role directory, as noted above. This file should contain a list of roles and parameters to insert before the specified role, such as the following in an example.` you seem to have added it after a task. Again, not familiar with the feature, hope it helps – ilias-sp May 17 '18 at 16:56
  • maybe you should look into `include_role` module? – ilias-sp May 17 '18 at 16:59
  • 1
    @ilias-sp thanks for comments, but we can provide the condition in `dependencies` check this [Conditional role dependencies](http://edunham.net/2015/09/10/ansible_conditional_role_dependencies.html) I just want to know, how can we do that with user define variable. We can do that with pre defined ansible variables. – Nilesh May 17 '18 at 17:41
  • Can you share where are you adding every piece of code? You cannot mix the `meta` with the `vars`. I guess you have the dependencies under the `meta/main.yml` and you are calling your role with the rest of the vars from a playbook, don't you? – imjoseangel May 17 '18 at 17:46
  • I am building my playbook, for now, I have only `mydepoloyment/meta/main.yaml` file which I gave above. All other are empty for now. – Nilesh May 17 '18 at 17:55
  • Sorry I have `mydeployment/vars/main.yaml` where I defined `pyenv_root=/user/local/pyenv` and `mypython_version=3.5.2` as – Nilesh May 17 '18 at 17:56

1 Answers1

4

To properly define dependencies in a role you have to:

Define your /meta/main.yml dependency under your role:

dependencies:
  - role: pyenv
    when: mypython_version not in versions

You have to call your role from your play:

---
- name: Role Dependency
  hosts: localhost
  gather_facts: False

tasks:
  - name: Get all install pyenv versions
    command: '{{ pyenv_root }}bin/pyenv versions'
    register: available_versions
    environment:
      PYENV_ROOT: "{{ pyenv_root }}"

roles:
  - role: mydeployment
    pyenv_version : "{{ mypython_version }}"
    versions      : "{{ available_versions.stdout }}"

Another example in my sandbox:

---
- name: Role Dependency
  hosts: localhost
  gather_facts: False

  roles:
  - role: role2
    role1 : "Heyr"

Meta:

dependencies:
  - role: role1
    when: role1 == "Hey"

Results:

PLAY [Role Dependency] *******************************************************************************************************

TASK [role1 : Debug] *********************************************************************************************************
skipping: [localhost]

TASK [role2 : Debug] *********************************************************************************************************
ok: [localhost] => {
    "msg": "Hello2"
}

But when variable is fine:

---
- name: Role Dependency
  hosts: localhost
  gather_facts: False

  roles:
  - role: role2
    role1 : "Heyr"

Result:

TASK [role1 : Debug] *********************************************************************************************************
ok: [localhost] => {
    "msg": "Hello1"
}

TASK [role2 : Debug] *********************************************************************************************************
ok: [localhost] => {
    "msg": "Hello2"
}

Hope it helps

imjoseangel
  • 3,543
  • 3
  • 22
  • 30
  • I have to put `tasks` and `roles` in my `mydeployment/tasks/main.yaml` ? – Nilesh May 17 '18 at 18:09
  • 1
    Nope, Use a main playbook to orchestrate your roles. IE: Having a playbook directory and roles directory. From the playbooks you will have whatever playbook to do whatever task. That playbook will call to roles that will be sooooo standard that you will be able to use them for every single playbook you write. Think in a role as an standard actions that you will call for a specific reason. A role is no more than a playbook splitted in directories and with some more power (Like dependencies) – imjoseangel May 17 '18 at 18:12
  • I have `mydeployment.yaml` playbook which has `- hosts: myhosts roles: - common - mydeployment` Is there any way to add this in `mydeployment/tasks` ? that way I have all in `tasks` only. – Nilesh May 17 '18 at 18:17
  • Nope, that's the way ansible works. You have to call the playbook as Ansible standards. If you want to go directly to the role, you can go for other "non-standard" solutions like: https://stackoverflow.com/questions/38350674/ansible-can-i-execute-role-from-command-line – imjoseangel May 17 '18 at 18:22
  • What is the syntax if I want to use `hosts`, `tasks` and `roles` in my playbook, I tried to put `tasks` and `roles` under `hosts` but that not execute when run the playbook. – Nilesh May 17 '18 at 18:41
  • `- hosts: myhosts tasks: ..... roles: ....` this is what I am trying, but `tasks` must execute before the `roles` dependencies execute, but in this case, I cant see them executed – Nilesh May 17 '18 at 18:42
  • 1
    Yes, you have pre and post_task. Sorry for the delay – imjoseangel May 17 '18 at 21:23