Reputation: 143
This is my first Ansible Role, so appologies for my ignorance... I wish to start/stop/status the services on my server. I've a role for each task. In my playbook I 've a conditional check to decide which role should be invoked.
---
- name: Manage Kore Cluster
hosts: all
gather_facts: true
become: yes
become_method: sudo
become_user: root
tasks:
- name: kor_start
include_role:
name: kor_start
when: action == kor_start
- name: kor_status
include_role:
name: kor_status
when: action == kor_status
- name: kor_stop
include_role:
name: kor_stop
when: action == kor_stop
name/action names match the role names.
The conditional check 'action == kor_start' failed. The error was: error while evaluating conditional (action == kor_start): 'action' is undefined
Could you advise what do I miss? Thanks!
Upvotes: 1
Views: 2889
Reputation: 68144
The error says 'action' is undefined
. Fix the conditions. For example, default to an empty string. Also, kor_start, kor_status, and kor_stop are strings, not variables, I think. In this case, quote them
---
- name: Manage Kore Cluster
hosts: all
gather_facts: true
become: yes
become_method: sudo
become_user: root
tasks:
- name: kor_start
include_role:
name: kor_start
when: action|d('') == 'kor_start'
- name: kor_status
include_role:
name: kor_status
when: action|d('') == 'kor_status'
- name: kor_stop
include_role:
name: kor_stop
when: action|d('') == 'kor_stop'
You can test the playbook, for example
shell> ansible-playbook playbook.yml -e action=kor_status
You can simplify the playbook
---
- name: Manage Kore Cluster
hosts: all
gather_facts: true
become: yes
become_method: sudo
become_user: root
vars:
valid_roles: [kor_start, kor_stop, kor_status]
tasks:
- name: "{{ action|d('undefined') }}"
include_role:
name: "{{ action }}"
when: action|d('undefined') in valid_roles
Upvotes: 2