AlberrB
AlberrB

Reputation: 45

Ansible-Playbook run custom group_vars

Im newbie using ansible.

I am trying to execute a custom playbook, which executes the roles that have been mounted from an iso, for this I have the following structure.

- /iso/
 - /AnsibleFiles/
     - /roles/
       - ....
       - ....

- myPlaybook.yml
- /myInventory/
  - group_vars/
    - myInventoryFile

Im trying to execute like this:

ansible-playbook myPlaybook.yml -i myInventory/group_vars/myInventoryFile

But itdoes not work.. ansible dont read my vars, return this message:

The conditional check '{{ my_Var }}' failed. The error was: error while evaluating conditional ({{ my_Var }}): 'my_Var ' is undefined\n\nThe error appears to have been in '/home/user/myPlaybook.yml

myInventoryFile

Have the variable defined like this: my_Var: true

myPlaybook.yml

It ave to evaluate this variable to know which roles to execute.

when: "{{my_Var}}"

Upvotes: 0

Views: 2505

Answers (2)

Lewis M
Lewis M

Reputation: 548

I got the following to work.

I created the following directory structure:

.
+ AnsibleFiles
| + roles
|   + testmyvar
|     + tasks
|       + main.yml
+ myInventory
| + localhost
+ myPlaybook.yml

The content of AnsibleFiles/roles/testmyvar/tasks/main.yml:

---
- name: Checking value of my_Var
  debug: var=my_Var
...

The content of myInventory/localhost:

localhost my_Var=yuck

The content of myPlaybook.yml

---
- hosts: localhost
  roles:
    - AnsibleFiles/roles/testmyvar
...

I get the following when I run the following command:

me$ ansible-playbook -i ./myInventory/localhost ./myPlaybook.yml

PLAY [localhost] ***************************************************************

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

TASK [AnsibleFiles/roles/testmyvar : Checking value of my_Var] *****************
ok: [localhost] => {
    "my_Var": "yuck"
}

PLAY RECAP *********************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0

I do not think you can have a group_vars directory and specify that with the -i on the command line. From what I read, you must have an inventory file. That means everything must be in that inventory file, including the vars definitions.

Hopefully this provides some guidance on how to update your Ansible code.

Upvotes: 1

rafal1337
rafal1337

Reputation: 184

inventory file: ansible-playbook myPlaybook.yml -i /etc/ansible/inventory/myInventory.yml

group vars folder/file: /etc/ansible/inventory/group_vars/myInventory.yml / /etc/ansible/inventory/group_vars/myInventory/something.yml

Upvotes: 0

Related Questions