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?
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