Playbook, to outsource variable definition to dedicated file

28 views
Skip to first unread message

kdt

unread,
Jul 17, 2019, 12:25:42 PM7/17/19
to Ansible Project
My playbook includes one single play. Every tasks in play imports - 'import_role:' - one single role each. Every of imported roles is stored to own dedicated repository. This playbook is usually run from wrapper playbook which includes - 'include_playbook:' - the former one. Wrapper playbook imports more playbooks to conduct complete whole mission.
Tasks in my playbook should be carried out only under one condition - which is under implementation.
So I started to look for location in whole structure where is the optimal place for 'when:'. Conditionals  are not supported at import_playbook's level so I started to have a look
into my playbook and placed 'when:' in every task which imports a role. In end-effect the condition is checked in my playbook.
Next step is to find optimal location for variable default value enforcement. Ansible documentation mentions: "As described in Roles, variables can also be included in the playbook via include files, which may or may not be part of an Ansible Role."  Actually documentation chapter "Roles" describes outsourcing variable settlement to intended files in role tree structure. My repository with playbooks tree inside is free however from tree of directories/files typical for role.
How to apply recommendations made for roles directory tree onto repository with playbooks which is free of role directories tree?

kdt

unread,
Jul 19, 2019, 5:31:01 AM7/19/19
to Ansible Project
To put it in other words:

Following tree of files is used
./
├── playbook-fw-xxx.yml
├── playbook.yml
├── README.md
└── requirements.yml

$ cat playbook-fw-xxx.yml
---
- hosts: all
  vars
:
       populate_host_with_framework
: true

  tasks
:
   
- name: Ensure /dev/xxx device nodes are ready-for-use
      import_role
:
         name
: vendorA.dev-nodes
     
when: {{populate_host_with_framework}}

Setting variable to its default value is planned for out-source to special file. How should the location of variable initialization look like ?

kdt

unread,
Jul 19, 2019, 5:36:17 AM7/19/19
to Ansible Project
Addendum to previous update: Please notice there is no roles sub-structure in used file tree. Ansible documentation uses merely roles to show how place variable initialization in dedicated file.

kdt

unread,
Jul 22, 2019, 5:54:38 AM7/22/19
to Ansible Project
Myself followed instructions made in https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#defining-variables-in-files and put all play's tasks into block in order to apply conditional to all tasks in play then outsource variable's default value settlement to dedicated file. Test completion is still pending.
Reply all
Reply to author
Forward
0 new messages