Refactoring variables to avoid hard coding the same value over and over again

14 views
Skip to first unread message

Behrang Saeedzadeh

unread,
Oct 19, 2017, 8:10:38 PM10/19/17
to Ansible Project

I want to refactor the following vars file/playbook pair, to avoid hard-coding the app name in the value for log_dir attributes.


---
# some-role-vars.yml
some_role_config: 
  - name: app1
    log_dir: "/var/log/app1/app1.log" 
  - name: app2
    log_dir: "/var/log/app2/app2.log" 
  - name: app3
    log_dir: "/var/log/app3/app3.log"
# some-playbook.yml
- hosts: all
  become: True
  gather_facts: True
  vars_files: 
    - some-role-vars.yml
  roles: 
    - role: some-role
      config: "{{ some_role_config }}"


Other than using set_fact in a way similar to this, what other approaches (best practices/conventions) are available?


- name: Set some_role_config
  set_fact: 
    some_role_config: "{{ some_role_config|default([]) + [ {'name': item, 'path': '/var/log/' + item + '/' + item + '.log' } ] }}"
  with_items: 
    - app1
    - app2
    - app3
Reply all
Reply to author
Forward
0 new messages