Gather facts fails regardless if conditional is met for roles

47 views
Skip to first unread message

phil toohill

unread,
Dec 10, 2014, 10:13:55 AM12/10/14
to ansible...@googlegroups.com
##### 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?

Matt Martz

unread,
Dec 10, 2014, 10:43:19 AM12/10/14
to ansible...@googlegroups.com
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]






--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/c7c00708-ffbb-45b0-ae7c-57ab35b8632a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Matt Martz
@sivel
sivel.net

phil toohill

unread,
Dec 10, 2014, 12:32:18 PM12/10/14
to ansible...@googlegroups.com
This is a great tip! Seems to be working as expected now,

Thank you
Reply all
Reply to author
Forward
0 new messages