# My first "role" isn't running

**URL:** <https://forum.ansible.com/t/my-first-role-isnt-running/20913>\
**Category:** Ansible Project\
**Created:** [March 18, 2016, 12:14am UTC](https://forum.ansible.com/t/my-first-role-isnt-running/20913 "2016-03-18T00:14:48Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gilberto\_Valentin](https://avatars.discourse-cdn.com/v4/letter/g/b4bc9f/32.png) [@Gilberto\_Valentin](https://forum.ansible.com/u/Gilberto_Valentin)\
**Post date:** [March 18, 2016, 12:14am UTC](https://forum.ansible.com/t/my-first-role-isnt-running/20913/1 "2016-03-18T00:14:48Z")

</div>

I am creating a role to install some packages and then join a system to a domain controller. However, when I run my playbook, it comes back saying **ERROR! ERROR! ‘test’ is undefined.** Here is how I am running my playbook:

> ansible-playbook -i server\_hosts app\_install\_main.yml

I don’t see where I need to define my host properly. My role consist of the following files:

`

├── server\_hosts  
├── app\_install  
│ ├── README.md  
│ ├── defaults  
│ │ └── main.yml  
│ ├── files  
│ ├── handlers  
│ │ └── main.yml  
│ ├── meta  
│ │ └── main.yml  
│ ├── tasks  
│ │ └── app\_install\_task.yml  
│ ├── templates  
│ ├── tests  
│ │ ├── inventory  
│ │ └── test.yml  
│ └── vars  
│ └── main.yml  
├── app\_install\_main.yml

`

My app\_install\_task.yml contains the following:

`

---

<div class="post-metadata">

**Author:** ![Arthur\_Reyes](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@Arthur\_Reyes](https://forum.ansible.com/u/Arthur_Reyes)\
**Post date:** [March 18, 2016, 12:26am UTC](https://forum.ansible.com/t/my-first-role-isnt-running/20913/2 "2016-03-18T00:26:16Z")

</div>

Just a thought. Did you export ANSIBLE\_INVENTORY? And is it equal to the path and name of your inventory file?

---

<div class="post-metadata">

**Author:** ![Gilberto\_Valentin](https://avatars.discourse-cdn.com/v4/letter/g/b4bc9f/32.png) [@Gilberto\_Valentin](https://forum.ansible.com/u/Gilberto_Valentin)\
**Post date:** [March 18, 2016, 12:33am UTC](https://forum.ansible.com/t/my-first-role-isnt-running/20913/3 "2016-03-18T00:33:58Z")

</div>

Hi Arthur, can you elaborate on exporting ANSIBLE\_INVENTORY? All I did was call my inventory file like this: **ansible-playbook -i server\_hosts app\_install\_main.yml**

Thanks for the quick response 🙂

---

<div class="post-metadata">

**Author:** ![Arthur\_Reyes](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@Arthur\_Reyes](https://forum.ansible.com/u/Arthur_Reyes)\
**Post date:** [March 18, 2016, 2:37am UTC](https://forum.ansible.com/t/my-first-role-isnt-running/20913/4 "2016-03-18T02:37:01Z")

</div>

It looks like your directory structure isn't quite right. app\_install should be a sub directory under roles, which might explain why your playbook never finds vars/main.yml.

---

<div class="post-metadata">

**Author:** ![Gilberto\_Valentin](https://avatars.discourse-cdn.com/v4/letter/g/b4bc9f/32.png) [@Gilberto\_Valentin](https://forum.ansible.com/u/Gilberto_Valentin)\
**Post date:** [March 18, 2016, 12:03pm UTC](https://forum.ansible.com/t/my-first-role-isnt-running/20913/5 "2016-03-18T12:03:05Z")

</div>

Hi Arthur,

Actually, I do have my app\_install as a sub directory under roles. It’s just that initially I only pasted everything within roles and only showing you that. Here’s a better view:

`

~/git/ansible/roles$ tree  
.  
└── project1  
├── server\_hosts  
├── app\_install  
│ ├── README.md  
│ ├── defaults  
│ │ └── main.yml  
│ ├── files  
│ ├── handlers  
│ │ └── main.yml  
│ ├── meta  
│ │ └── main.yml  
│ ├── tasks  
│ │ └── app\_install\_task.yml  
│ ├── templates  
│ ├── tests  
│ │ ├── inventory  
│ │ └── test.yml  
│ └── vars  
│ └── main.yml  
├── app\_install\_main.yml

`

---

<div class="post-metadata">

**Author:** ![Uditha\_Desilva](https://avatars.discourse-cdn.com/v4/letter/u/f475e1/32.png) [@Uditha\_Desilva](https://forum.ansible.com/u/Uditha_Desilva)\
**Post date:** [March 18, 2016, 1:57pm UTC](https://forum.ansible.com/t/my-first-role-isnt-running/20913/6 "2016-03-18T13:57:38Z")

</div>

Unless I’ve missed something, I can’t see where your top-level “app\_install\_main.yml” play is expecting to get “test” from – it needs to know the list of hosts to work with before it starts opening up the roles, and it doesn’t import those variables from anywhere itself?

As far as I know, a role should not define values like the host list – that is provided to it by the play itself – so perhaps you meant to write “- hosts: testservers” in that app\_install\_main.yml instead?

---

<div class="post-metadata">

**Author:** ![Gilberto\_Valentin](https://avatars.discourse-cdn.com/v4/letter/g/b4bc9f/32.png) [@Gilberto\_Valentin](https://forum.ansible.com/u/Gilberto_Valentin)\
**Post date:** [March 18, 2016, 3:06pm UTC](https://forum.ansible.com/t/my-first-role-isnt-running/20913/7 "2016-03-18T15:06:47Z")

</div>

Ok, if I change my vars main.yml

From

`

---

<div class="post-metadata">

**Author:** ![Mike\_Biancaniello](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@Mike\_Biancaniello](https://forum.ansible.com/u/Mike_Biancaniello)\
**Post date:** [March 18, 2016, 5:38pm UTC](https://forum.ansible.com/t/my-first-role-isnt-running/20913/8 "2016-03-18T17:38:35Z")

</div>

I think your tree should look more like this. Also notice the change of tasks/app\_install\_task.yml to tasks/main.yml

`

~/git/ansible$ tree  
.  
└── project1

├── server\_hosts  
├── roles  
`│` `└`── app\_install  
`│` `├── README.md `│` `├── defaults  
`│` `│ └── main.yml `│` `├── files  
`│` `├── handlers `│` `│ └── main.yml  
`│` `├── meta `│` `│ └── main.yml  
`│` `├── tasks `│` `│ └── main.yml  
`│` `├── templates `│` `├── tests  
`│` `│ ├── inventory `│` `│ └── test.yml  
`│` `└── vars `│` `└── main.yml  
├── app\_install\_main.yml

`

---

<div class="post-metadata">

**Author:** ![Gilberto\_Valentin](https://avatars.discourse-cdn.com/v4/letter/g/b4bc9f/32.png) [@Gilberto\_Valentin](https://forum.ansible.com/u/Gilberto_Valentin)\
**Post date:** [March 18, 2016, 7:01pm UTC](https://forum.ansible.com/t/my-first-role-isnt-running/20913/9 "2016-03-18T19:01:57Z")

</div>

Mike,

I got a bit further with your suggestion. I changed my tree to look like the one you have and now it looks like it’s trying to kick. However, now it’s not finding the value of my **hosts:** “{{ test }}”

When I run the command, I see this now:

`

[WARNING]: provided hosts list is empty, only localhost is available

Sudo password:

PLAY [install app and join appropriate ou] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
skipping: no hosts matched

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

`

My vars/main.yml contains the following:

`

---

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://forum.ansible.com/u/Dick_Davies)\
**Post date:** [March 18, 2016, 7:13pm UTC](https://forum.ansible.com/t/my-first-role-isnt-running/20913/10 "2016-03-18T19:13:29Z")

</div>

Is there any way to put this up as e.g. a github repo?

I'm pretty sure your layout is wrong but it's hard to keep track of  
what's where without having it to hand.

---

<div class="post-metadata">

**Author:** ![Mike\_Biancaniello](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@Mike\_Biancaniello](https://forum.ansible.com/u/Mike_Biancaniello)\
**Post date:** [March 18, 2016, 7:33pm UTC](https://forum.ansible.com/t/my-first-role-isnt-running/20913/11 "2016-03-18T19:33:26Z")

</div>

I believe that the hosts is loaded before the role (or its vars).

Try changing app\_install\_main.yml to

`

- name: install app and join systems to domain  
hosts: testservers  
become: yes

`

Or, passing -e ‘{“test”:“testservers”}’ when you run ansible.

---

<div class="post-metadata">

**Author:** ![Mike\_Biancaniello](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@Mike\_Biancaniello](https://forum.ansible.com/u/Mike_Biancaniello)\
**Post date:** [March 18, 2016, 7:34pm UTC](https://forum.ansible.com/t/my-first-role-isnt-running/20913/12 "2016-03-18T19:34:12Z")

</div>

Or … your problem could be that you don’t have a group defined as “testservers”. Only “testsystems”.

---

<div class="post-metadata">

**Author:** ![Gilberto\_Valentin](https://avatars.discourse-cdn.com/v4/letter/g/b4bc9f/32.png) [@Gilberto\_Valentin](https://forum.ansible.com/u/Gilberto_Valentin)\
**Post date:** [March 18, 2016, 7:52pm UTC](https://forum.ansible.com/t/my-first-role-isnt-running/20913/13 "2016-03-18T19:52:46Z")

</div>

I can’t put this up on github. I had to sanitize it a lot just to get it here too :/.

If the structure is wrong, then I am not sure what should be changed. I got that structure initially by running **ansible-galaxy init role\_name** and have modified the structure here and there based on suggestions on this thread

---

<div class="post-metadata">

**Author:** ![Gilberto\_Valentin](https://avatars.discourse-cdn.com/v4/letter/g/b4bc9f/32.png) [@Gilberto\_Valentin](https://forum.ansible.com/u/Gilberto_Valentin)\
**Post date:** [March 18, 2016, 8:01pm UTC](https://forum.ansible.com/t/my-first-role-isnt-running/20913/14 "2016-03-18T20:01:03Z")

</div>

Mike,

So before I make that change, let me show you what I have in my hosts file:

`

[testsystems]  
testserver1.domain.tld

[developmentsystems]  
devserver1.domain.tld  
devserver2.domain.tld

[productionsystems]  
prodserver1.domain.tld  
prodserver2.domain.tld

[testservers:children]  
testsystems

[dev-servers:children]  
developmentsystems

[prod-servers:children]  
productionsystems

`

And then I call the following from vars/main.yml

`

---

<div class="post-metadata">

**Author:** ![Gilberto\_Valentin](https://avatars.discourse-cdn.com/v4/letter/g/b4bc9f/32.png) [@Gilberto\_Valentin](https://forum.ansible.com/u/Gilberto_Valentin)\
**Post date:** [March 18, 2016, 8:03pm UTC](https://forum.ansible.com/t/my-first-role-isnt-running/20913/15 "2016-03-18T20:03:38Z")

</div>

Mike,

And then in my app\_install\_main.yml, I have the following:

`

---

<div class="post-metadata">

**Author:** ![Mike\_Biancaniello](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@Mike\_Biancaniello](https://forum.ansible.com/u/Mike_Biancaniello)\
**Post date:** [March 18, 2016, 8:33pm UTC](https://forum.ansible.com/t/my-first-role-isnt-running/20913/16 "2016-03-18T20:33:25Z")

</div>

Ok, I missed the ‘testservers’ group.

I just tested on my local machine and the only way I can find to reference a var in the ‘hosts:’ setting is if you pass it in with --extra-vars.

So, I refer to my previous suggestion:

Try changing app\_install\_main.yml to

`

- name: install app and join systems to domain

hosts: testservers  
become: yes

`

Or, passing -e ‘{“test”:“testservers”}’ when you run ansible.

---

<div class="post-metadata">

**Author:** ![Gilberto\_Valentin](https://avatars.discourse-cdn.com/v4/letter/g/b4bc9f/32.png) [@Gilberto\_Valentin](https://forum.ansible.com/u/Gilberto_Valentin)\
**Post date:** [March 18, 2016, 8:34pm UTC](https://forum.ansible.com/t/my-first-role-isnt-running/20913/17 "2016-03-18T20:34:08Z")

</div>

Mike and Uditha,

Ok, I see what you guys are saying about the hosts. I moved it one level up and now we are getting somewhere. Now, the issue is that it can’t run my task because it doesn’t see the value of my conditional. For example, I want my task to run **when: “{{ testserver }}”**. In other words, I want that task to run only on the servers in my **testserver** group.

Here is the task/main.yml again:

`

---

<div class="post-metadata">

**Author:** ![Mike\_Biancaniello](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@Mike\_Biancaniello](https://forum.ansible.com/u/Mike_Biancaniello)\
**Post date:** [March 18, 2016, 8:50pm UTC](https://forum.ansible.com/t/my-first-role-isnt-running/20913/18 "2016-03-18T20:50:08Z")

</div>

Overall, you are better off customizing your plays for each group (maybe in separate playbooks) instead of trying to make a one-size-fits-all playbook. Tasks can be reused by either importing them or putting them in roles.

The other nice thing about this model is that when you run ansible-playbook --list-roles, you get a nice documented output (assuming you named all of your tasks) of what will happen.

However, since we’re already here…

‘testserver’ is a value, not a variable name.

There are a few ways you can do this:

1. Use multiple plays. Have one play run with ‘hosts: testservers’ and another play run on some other set of hosts. Group the tasks in the appropriate plays.
2. Make your ‘when’ statement be: when: “‘testservers’ in group\_names”

Also, what do you mean by “moved it up one level”?

---

<div class="post-metadata">

**Author:** ![Uditha\_Desilva](https://avatars.discourse-cdn.com/v4/letter/u/f475e1/32.png) [@Uditha\_Desilva](https://forum.ansible.com/u/Uditha_Desilva)\
**Post date:** [March 18, 2016, 10:17pm UTC](https://forum.ansible.com/t/my-first-role-isnt-running/20913/19 "2016-03-18T22:17:21Z")

</div>

I agree with Mike’s suggestion regarding changing that conditional to when: “‘testservers’ in group\_names” – that’s exactly how I handle special cases like DMZ hosts.

---

<div class="post-metadata">

**Author:** ![Gilberto\_Valentin](https://avatars.discourse-cdn.com/v4/letter/g/b4bc9f/32.png) [@Gilberto\_Valentin](https://forum.ansible.com/u/Gilberto_Valentin)\
**Post date:** [March 18, 2016, 11:59pm UTC](https://forum.ansible.com/t/my-first-role-isnt-running/20913/20 "2016-03-18T23:59:20Z")

</div>

Hi Mike,

When I say “moved it up one level”, I meant I took my hosts file out of the roles directory and moved it up one level out of there.

Now, back to your suggestions. For your first point, I am not sure I follow. For example, you said to run my play (I understand this as task) with ‘hosts: testservers’. How would I do that? I thought your hosts: were defined in the main.yml outside of tasks folder. For example, here is what I have:

In app\_install\_main.yml:

`

[Next page](https://forum.ansible.com/t/my-first-role-isnt-running/20913.md?page=2)
