Gather facts fails regardless if conditional is met for roles

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:

- 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.

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

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

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 }}”
    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 }}”
    groups: deleted
    with_items: rax_results.success
    when: rax_results.action == ‘delete’

  • hosts: webservers!deleted
    [other stuff here]

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

Thank you