32

I have Ansible role, for example

---
- name: Deploy app1
  include: deploy-app1.yml
  when: 'deploy_project == "{{app1}}"'

- name: Deploy app2
  include: deploy-app2.yml
  when: 'deploy_project == "{{app2}}"'

But I deploy only one app in one role call. When I deploy several apps, I call role several times. But every time there is a lot of skipped tasks output (from tasks which do not pass condition), which I do not want to see. How can I avoid it?

32cupo
  • 850
  • 5
  • 18
  • 36
  • How is it possible not to display skipped tasks for `Deploy app1` but not for `Deploy app2`? I want to skip this display only for a certain amount of tasks. – MUY Belgium Apr 20 '17 at 18:10
  • In comments below (http://stackoverflow.com/questions/39189549/how-can-i-hide-skipped-tasks-output-in-ansible?noredirect=1#comment65746398_39189835) we discussed with @konstantin-suvorov that you can't skip task names, because they are printed before any decision are made. – 32cupo Apr 21 '17 at 06:17

6 Answers6

39

I'm assuming you don't want to see the skipped tasks in the output while running Ansible.

Set this to false in the ansible.cfg file.

display_skipped_hosts = false

Note. It will still output the name of the task although it will not display "skipped" anymore.

UPDATE: by the way you need to make sure ansible.cfg is in the current working directory.

Taken from the ansible.cfg file.

ansible will read ANSIBLE_CONFIG, ansible.cfg in the current working directory, .ansible.cfg in the home directory or /etc/ansible/ansible.cfg, whichever it finds first.

So ensure you are setting display_skipped_hosts = false in the right ansible.cfg file.

Let me know how you go

Omar E
  • 525
  • 1
  • 5
  • 11
  • in comments of previous answer I mentioned, that I tried this ansible.cfg property, although It has the same effect as skippy callback plugin. But thank you for your answer. – 32cupo Aug 30 '16 at 11:26
  • 1
    Setting the following environment variable does the same: `export ANSIBLE_DISPLAY_SKIPPED_HOSTS=false` – daniilyar Apr 20 '20 at 17:50
  • You should not put ansible.cfg in the current working directory just for one setting, because that will override *all* settings, usually a very unexpected result. Ansible does not combine settings from several ansible.cfg files you might expect. – Kevin Keane Jul 13 '20 at 06:37
  • @32cupo you are right. The skippy plugin is deprecated, and the display_skipped_host setting is the new official way of doing the same thing. – Kevin Keane Jul 13 '20 at 06:39
20

Since ansible 2.4, a callback plugin name full_skip was added to suppress the skipping of task names and skipping keyword in the ansible output. You can try the below ansible configuration:

[defaults]
stdout_callback = full_skip
Mozahler
  • 4,958
  • 6
  • 36
  • 56
Leon Xie
  • 201
  • 2
  • 3
16

Ansible allows you to control its output by using custom callbacks.

In this case you can simply use the skippy callback which will not output anything on a skipped task.

That said, skippy is now deprecated and will be removed in ansible v2.11.

wfaulk
  • 1,765
  • 1
  • 17
  • 24
ydaetskcoR
  • 53,225
  • 8
  • 158
  • 177
  • When I copy plugin into my callback plugins directory and run playbook again, I did not notice any changes. I found display_skipped_hosts property in ansible.cfg file ( http://docs.ansible.com/ansible/intro_configuration.html#display-skipped-hosts ) but it still display name of skipped tasks. – 32cupo Aug 28 '16 at 11:45
  • Did you leave it named the same? As the docs mention it loads alphabetically and `default` obviously comes before `skippy` so it wouldn't load. – ydaetskcoR Aug 28 '16 at 11:50
  • I tried to remove all my callback plugins, then copied skippy.py to callback directory, ran playbook and got the same result. So I removed skippy.pyc and renamed skippy.py to akippy.py and it was the same. I ran both options several times, but output was the same. When I tried another callback plugin ( https://github.com/n0ts/ansible-human_log ) it worked. – 32cupo Aug 28 '16 at 12:09
  • I use Ubuntu 16.04.1 LTS and Ansible version is 2.1.1.0 – 32cupo Aug 28 '16 at 12:18
  • @32cupo To change display behavior, you just need to set [stdout_callback](http://docs.ansible.com/ansible/intro_configuration.html#stdout-callback) in config. There's no need to copy/rename files. – Konstantin Suvorov Aug 28 '16 at 21:50
  • 2
    @konstantin-suvorov When I set stdout_callback properties to skippy, display behaviour is the same as copied skippy to callback directory. Ansible still displays name of skipped tasks. – 32cupo Aug 29 '16 at 09:58
  • 5
    @32cupo ah, sorry. I missed the fact that you talk about **name** of skipped task. Names are printed before any decisions are made, so you can't rid of them that simple. It is done this way because you may have task skipped for a subset of hosts. Your only option is a hand-made plugin. – Konstantin Suvorov Aug 29 '16 at 10:10
  • 2
    The correct link to the callback is: https://docs.ansible.com/ansible/latest/plugins/callback/skippy.html Note: it has been deprecated and will be removed in 2.11 – madonius Apr 15 '20 at 08:44
4

If you don't mind losing colours you can elide the skipped tasks by piping the output through sed:

ansible-playbook whatever.yml | sed -nr '/^TASK/{h;n;/^skipping:/{n;b};H;x};p'
Max Murphy
  • 1,701
  • 1
  • 19
  • 29
  • 1
    You can force colors with `force_color = 1` in `ansible.cfg`. – ceving Sep 21 '17 at 11:41
  • Works perfectly, but when I used `force_color` I had to use modified sed - `sed -nr '/^TASK/{h;n;/^^[\[0;36mskipping:/{n;b};H;x};p'` – 32cupo Dec 28 '17 at 20:00
0

The following works for me in version 2.13.3. It's probably not how the tags keyword was intended to be used, but it gets the job done. You won't be able to use this trick if you were already using tags on the same module.

tags: '{{"always" if deploy_project == app1 else "never"}}'

The always and never tags are special tags to force the module to run or not.

Todd
  • 475
  • 4
  • 15
-3

If you are using roles, you can use when to cancel the include in main.yml

# roles/myrole/tasks/main.yml
- include: somefile.yml
  when: somevar is defined


# roles/myrole/tasks/somefile.yml
- name: this task will only run (and be seen in the output) if somevar is defined
  debug:
    msg: "Hello World"
Volker
  • 497
  • 6
  • 15
  • 3
    This will not cancel the include, but just add given when to every task inside include. Please read about static/dynamic includes. – Konstantin Suvorov Sep 07 '17 at 06:58
  • @KonstantinSuvorov you're right, I was wrong. The task line is still displayed. It is a line/time saver for me when I want to skip the complete file. – Volker Sep 08 '17 at 03:16