Repository: ansible/ansible

Issue:
gather_facts does not execute when using tags
##### Issue Type:

Bug Report
##### Ansible Version:

```
ansible 2.1.0 (devel 49c15c8137) last updated 2016/01/31 08:09:48 (GMT +000)
  lib/ansible/modules/core: (detached HEAD 93d02189f6) last updated 2016/01/31 08:32:26 (GMT +000)
  lib/ansible/modules/extras: (detached HEAD fff5ae6994) last updated 2016/01/31 08:32:46 (GMT +000)
  config file =
  configured module search path = Default w/o overrides
```
##### Ansible Configuration:

N/A
##### Environment:

Ubuntu 14.04 LTS
##### Summary:

The destructive integration test for apt_repository fails as of commit  5587b08335f223fff64f54ced6f2790b3d6ee6f0.
The test passes using commit ceef202024423dd35ecef93f947d661f71c33e75 and earlier.
##### Steps To Reproduce:

Run the destructive integration test on test_apt_repository:

```
ansible-playbook destructive.yml -i inventory -e @integration_config.yml -v --tags test_apt_repository
```
##### Expected Results:

```
TASK [test_apt_repository : set_fact] ******************************************
ok: [testhost] => {"ansible_facts": {"test_ppa_filename": "menulibre-dev", "test_ppa_key": "A7AD98A1", "test_ppa_name": "ppa:menulibre-dev/devel", "test_ppa_spec": "deb http://ppa.launchpad.net/menulibre-dev/devel/ubuntu trusty main"}, "changed": false}
```
##### Actual Results:

```
TASK [test_apt_repository : set_fact] ******************************************
fatal: [testhost]: FAILED! => {"failed": true, "msg": "The conditional check 'ansible_distribution in ('Ubuntu', 'Debian')' failed. The error was: error while evaluating conditional (ansible_distribution in ('Ubuntu', 'Debian')): 'ansible_distribution' is undefined\n\nThe error appears to have been in '/root/ansible/test/integration/roles/test_apt_repository/tasks/main.yml': line 19, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- include: 'apt.yml'\n  ^ here\n"}
        to retry, use: --limit @destructive.retry
```



Discussion:
This problem only manifests if tags are specified. Running tests without tags works as expected.

This is expected, when specifying a tag, fact gathering is not happening. Still the tests depend on facts so they should always be gathered.

@mattclay The above PR fixes this for me, can you confirm?

Shouldn't setup be tagged `always` internally? Otherwise it'll never run when someone's running with tags, which is almost certainly a breaking change from 1.9.

@bcoca The above PR does resolve the issue. However, shouldn't setup run automatically for the selected tags, rather than having to make changes to existing playbooks to adapt to the new behavior?

This breaks all our playbooks which we run using tags, since we expect that facts will be gathered.


Resolving patch:
diff --git a/lib/ansible/executor/play_iterator.py b/lib/ansible/executor/play_iterator.py
--- a/lib/ansible/executor/play_iterator.py
+++ b/lib/ansible/executor/play_iterator.py
@@ -137,6 +137,7 @@ def __init__(self, inventory, play, play_context, variable_manager, all_vars, st
         setup_block = Block(play=self._play)
         setup_task = Task(block=setup_block)
         setup_task.action = 'setup'
+        setup_task.tags   = ['always']
         setup_task.args   = {}
         setup_task.set_loader(self._play._loader)
         setup_block.block = [setup_task]


Test patch: