0
votes

first post here, I got a situation, i'm trying to assign variables which are stored in roles, in my case : /var/lib/awx/projects/test/playbooks/roles/<< my roles >>/vars/main.yml but I want them to be assign by host, so I tried to assign them in my hosts files like this :

[test] 10.102.32.42 id=1 station=red ... but that don't work, my variables are not defined.

I tried with host_vars/ ansible-playbook test_role.yml -i host -f 5 -e guest_vars_file=host_vars/test but same thing, it doesn't take my variables.

my test file: id: 1 station: red

I tried with group_vars/ ansible-playbook test_role.yml -i host -f 5 -e guest_vars_file=group_vars/test I don't know if it's the good way to do it.

I tried a simple ansible-playbook test_role.yml -i host and files in good place but no

I tried with AWX by assign variables in my hosts, didn't worked.

When I'm passing variables with -e, it's working but my variables have to change by hosts.

Any way to do it ? or it's not possible ? I'm using ansible 2.4.3.0 I was wondering if when I launch the task, ansible overrited my variables by what it is in my vars/main.yml where I only put id: station:

Edit: So the solution is to put the right name in host_vars AND ! not to put variables in roles/my_role/vars/main.yml because it will override your vars that are stock in host_vars . Thanks

1

1 Answers

0
votes

Make sure host_vars is relative to playbook, in this case mytest.yml:

user> find roles/mytest
roles/mytest
roles/mytest/tasks
roles/mytest/tasks/main.yml

user> cat roles/mytest/tasks/main.yml 
---

- name: test myvar
  shell: echo "{{ myvar }}" > /tmp/myvar

user> cat hosts2
[test]
10.102.32.42

user> cat host_vars/10.102.32.42 
myvar: "this is 10.102.32.42"

user> cat mytest.yml 
---
- hosts: test
  roles:
    - mytest


user> ansible-playbook --inventory-file=hosts2 mytest.yml 

PLAY [test] *******************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************
ok: [10.0.0.4]

TASK [mytest : test myvar] ****************************************************************************************************
changed: [10.102.32.42]

PLAY RECAP ********************************************************************************************************************
10.102.32.42                   : ok=2    changed=1    unreachable=0    failed=0   

user> ssh 10.102.32.42 "cat /tmp/myvar"
this is 10.102.32.42

Note that group_vars, host_vars folders are NOT relative to some role folder. Consider that any variables set in these folders may be common against several roles, or they may be related to a specific host, not a specific role. Refer to Ansible Best Practices; :

production                # inventory file for production servers
staging                   # inventory file for staging environment

group_vars/
   group1                 # here we assign variables to particular groups
   group2                 # ""
host_vars/
   hostname1              # if systems need specific variables, put them here
   hostname2              # ""

library/                  # if any custom modules, put them here (optional)
module_utils/             # if any custom module_utils to support modules, put them here (optional)
filter_plugins/           # if any custom filter plugins, put them here (optional)

site.yml                  # master playbook
webservers.yml            # playbook for webserver tier
dbservers.yml             # playbook for dbserver tier

roles/
    common/               # this hierarchy represents a "role"
        tasks/            #
            main.yml      #  <-- tasks file can include smaller files if warranted
        handlers/         #
            main.yml      #  <-- handlers file
        templates/        #  <-- files for use with the template resource
            ntp.conf.j2   #  <------- templates end in .j2
        files/            #