Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ansible playbook role variable

fairly new to ansible. having following role e.g.: my-role - i have trouble overriding the default variables from the playbook

follwing files:

my-role/tasks/main.yml
my-role/defaults/main.yml
sample-playbook.yml

my-role/tasks/main.yml

- name: "Add Test User"
  user: name={{ my_config_test_user }} comment="{{ my_config_test_user }}" group={{ my_config_test_user }}

my-role/defaults/main.yml

my_config_test_user: "test"

playbook:

- name: TestCase
  hosts: all
  remote_user: root
  vars:
    my_config_test_user: "override"
  roles:
    - my-role

in the task the value of my_config_test_user stays test instead of my expected result override

any hints?

regards

like image 391
Helmut Januschka Avatar asked May 19 '15 13:05

Helmut Januschka


1 Answers

In the current version of Ansible order of precedence says that the value of my_config_test_user should be override not test so I think you probably have a typo somewhere. Maybe the variable is not spelled correctly?

I suggest removing defaults/main.yml temporarily to ensure that you do not get an undefined variable error. I would also suggest using the debug module to check the value of the variable in your tasks/main.yml

- debug: var={{ my_config_test_user }}

For reference here is the order of precedence (starting with the greatest) in the current version of Ansible:

  1. Vars set on the command line -e foo=set_on_cmd_line
  2. Vars set in the vars_files: block in the play
  3. Vars set in the vars: block in the play
  4. Vars set in host_vars/
  5. Vars set in group_vars/
  6. Role default vars roles/.../defaults/main.yml
like image 107
jarv Avatar answered Oct 08 '22 11:10

jarv