Repository: ansible/ansible

Issue:
Ansible should expose current verbosity level to a variable accessible from playbook

##### ISSUE TYPE
<!--- Pick one below and delete the rest -->
 - Feature Idea

##### COMPONENT NAME
core

##### ANSIBLE VERSION
<!--- Paste verbatim output from "ansible --version" between quotes below -->
```
2.4.3.0
```

##### CONFIGURATION

##### OS / ENVIRONMENT
N/A

##### SUMMARY
Currently is impossible to check current verbosity level from inside the playbook and users may want to use this in order to modify behaviour, like enabling some tasks based on its value.

If ansible would allow us to access verbosity level we could do something like:
```
    shell: echo "yeah!"
    when: "{{ verbosity > 0 }}"
```




Discussion:
Unless a way is also provided to mute skipped tasks (which AFAIK is still missing), this would not  make  much good - at verbosity==0 you would not see "yeah", but you'd still see the "skipped" notice.
@MonsieurCellophane http://docs.ansible.com/ansible/devel/plugins/callback/skippy.html
@MonsieurCellophane yes, while for 'yeah' use case it may not be that much good without skippy,

but imagine there is not just one word, but whole log or output of get-status-overview commands etc, 
spanning few dozen+ lines ... in which case one skip is better price to pay,

... instead of having to choose between '_always use increased verbosity_'  vs '_double printing in case of increased verbosity_'
e.g. just `- shell` or any task with verbosity driven *hidden* output  vs `- shell: register;  - debug: var:`

while with this issue addressed, second approach could be taken while eliminating double printing by having condition for more verbose run to just do skip as it was likely already printed above
Seconded! I already check `ansible_check_mode` to add the `--dry-run` (or some equivalent) to various commands executed by tasks. I'd like to _also_ be able to add `-v` (or equivalent), if the entire ansible is running with increased verbosity.

For example:
```yaml
- name: Populate directories
  command: >-
    rsync
    {% if ansible_check_mode %}--dry-run{% endif %}
    {% if ansible_verbosity > 0 %}-v{% endif %}
    {% if ansible_verbosity > 1 %}-v{% endif %}
    --archive
    --delay-updates
    ...
  check_mode: ansible_check_mode
```
I agree.
Other use cases include when your validation or debug code requires inspection tasks unnecessary for normal playbook execution.  For example calling a REST API to gather facts.

Resolving patch:
diff --git a/lib/ansible/utils/vars.py b/lib/ansible/utils/vars.py
--- a/lib/ansible/utils/vars.py
+++ b/lib/ansible/utils/vars.py
@@ -146,16 +146,19 @@ def load_extra_vars(loader, options):
 def load_options_vars(options, version):
 
     options_vars = {'ansible_version': version}
-    aliases = {'check': 'check_mode',
-               'diff': 'diff_mode',
-               'inventory': 'inventory_sources',
-               'subset': 'limit',
-               'tags': 'run_tags'}
-
-    for attr in ('check', 'diff', 'forks', 'inventory', 'skip_tags', 'subset', 'tags'):
+    attrs = {'check': 'check_mode',
+             'diff': 'diff_mode',
+             'forks': 'forks',
+             'inventory': 'inventory_sources',
+             'skip_tags': 'skip_tags',
+             'subset': 'limit',
+             'tags': 'run_tags',
+             'verbosity': 'verbosity'}
+
+    for attr, alias in attrs.items():
         opt = getattr(options, attr, None)
         if opt is not None:
-            options_vars['ansible_%s' % aliases.get(attr, attr)] = opt
+            options_vars['ansible_%s' % alias] = opt
 
     return options_vars
 


Test patch: