# Gather facts fails regardless if conditional is met for roles

**URL:** <https://forum.ansible.com/t/gather-facts-fails-regardless-if-conditional-is-met-for-roles/17009>\
**Category:** Ansible Project\
**Tags:** ubuntu\
**Created:** [December 10, 2014, 3:13pm UTC](https://forum.ansible.com/t/gather-facts-fails-regardless-if-conditional-is-met-for-roles/17009 "2014-12-10T15:13:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![phil\_toohill](https://avatars.discourse-cdn.com/v4/letter/p/dbc845/32.png) [@phil\_toohill](https://forum.ansible.com/u/phil_toohill)\
**Post date:** [December 10, 2014, 3:13pm UTC](https://forum.ansible.com/t/gather-facts-fails-regardless-if-conditional-is-met-for-roles/17009/1 "2014-12-10T15:13:55Z")

</div>

##### Issue Type:

Bug Report

##### Ansible Version:

1.8.2

##### Environment:

OSX 10.8.5

##### Summary:

Fact gathering fails when cloud module deletes server and roles play is called with a when conditional.

##### Steps To Reproduce:

Assuming utilization of a cloud module such as ‘rax’, you can set the count to 0 to remove the servers and then call out to roles, as such:

```auto
- name: Build an exact count of cloud servers with incremented names
hosts: local
gather_facts: False
tasks:
- name: Server build requests
local_action:
module: rax
credentials: ~/.raxpub
name: test%03d.example.org
flavor: performance1-1
image: ubuntu-1204-lts-precise-pangolin
state: present
count: 0
exact_count: yes
group: webservers
wait: yes
register: rax

- name: Setup nginx
user: root
hosts: webservers
vars:
operation: "{{ wsoperation }}"
roles:
- { role: nginx, when: operation == 'create' }

```

##### Expected Results:

It would expected that the when clause should trigger prior/during the gather facts call thus adhering to the conditional and passing tests.

I call this a bug because I assume no operations should happen for a play unless the conditional is met, this includes fact gathering.

Besides having a separate playbook with duplicated server logic, which defeats the purpose of this modules benefits, I do not see a proper work around.

##### Actual Results:

Gather facts is triggered before the when operation is analyzed causing failure.

```auto
fatal: [webser-backend01] => SSH encountered an unknown error during the connection

```

This was closed as ‘not a bug’, anyone have suggested work around?

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [December 10, 2014, 3:42pm UTC](https://forum.ansible.com/t/gather-facts-fails-regardless-if-conditional-is-met-for-roles/17009/2 "2014-12-10T15:42:33Z")

</div>

So roles are evaluated after the top level portions of the play.

I would do something such as using add\_host to add deleted servers to an in memory group that you could use for exclusion later. Such as something like:

- hosts: local  
gather\_facts: False  
tasks:

- name: Create or delete servers  
rax:  
[module stuff here that deletes servers]  
group: webservers  
register: rax\_results

- name: Add created servers to webservers group  
add\_host:  
hostname: “{{ [item.name](http://item.name) }}”  
ansible\_ssh\_host: “{{ item.rax\_accessipv4 }}”  
ansible\_ssh\_pass: “{{ item.rax\_adminpass }}”  
groups: webservers  
with\_items: rax\_results.success  
when: rax\_results.action == ‘create’

- name: Add deleted servers to deleted group for later exclusions  
add\_host:  
hostname: “{{ [item.name](http://item.name) }}”  
groups: deleted  
with\_items: rax\_results.success  
when: rax\_results.action == ‘delete’

- hosts: webservers!deleted  
[other stuff here]

---

<div class="post-metadata">

**Author:** ![phil\_toohill](https://avatars.discourse-cdn.com/v4/letter/p/dbc845/32.png) [@phil\_toohill](https://forum.ansible.com/u/phil_toohill)\
**Post date:** [December 10, 2014, 5:32pm UTC](https://forum.ansible.com/t/gather-facts-fails-regardless-if-conditional-is-met-for-roles/17009/3 "2014-12-10T17:32:18Z")

</div>

This is a great tip! Seems to be working as expected now,

Thank you
