3

In an ansible role, i need to define constants for some paths, not changeable by users in their playbook.

Here's my need:

the role will have a {{app_base_path}} variable (changeable by user), and then i want to set 2 constants:

  • app_instance_path: "{{app_base_path}}/appinstance"
  • app_server_path: "{{app_instance_path}}/appserver"

I need each value several times in my tasks so i can't set only one variable for it

What's the best way to do it?

Thanks.

2 Answers2

1

As far as I know, ansible has no constants.

You can do the following:
In the file <rolname>/defaults/main.yml

---
# Don't change this variables
app_instance_path: "{{ app_base_path }}/appinstance"
app_server_path: "{{ app_instance_path }}/appserver"

And add an assertion task in to the <rolename>/tasks/main.yml file:

---
# ...
- name: Check some constants
  assert:
    that:
      - "app_instance_path == app_base_path + '/appinstance'"
      - "app_server_path == app_instance_path + '/appserver'"

Further more you can document for the users to only set app_base_path and leave app_instance_path and app_server_path as it is.

Sebastian Stigler
  • 6,819
  • 2
  • 29
  • 31
  • Thanks for your answer. That's not exactly what i expected but checking is a cool feature to force some paths. – Jérôme P. Sep 08 '15 at 09:51
  • How useful would these assertions be in practise? Another role or task may easily change these variables after the role tests the assertions. – Derek Mahar Nov 14 '16 at 18:17
1

Finally, i got it with set_fact, unfortunately, it seems to have a very low priority in variables order, so my role execution can fail if the user defines extra_vars in his playbook...