{"id":391,"date":"2023-05-10T12:02:14","date_gmt":"2023-05-10T16:02:14","guid":{"rendered":"https:\/\/blog.signalsguru.net\/?p=391"},"modified":"2023-05-10T12:02:14","modified_gmt":"2023-05-10T16:02:14","slug":"ansible-verbosity-and-tags","status":"publish","type":"post","link":"http:\/\/blog.signalsguru.net\/archives\/391","title":{"rendered":"Ansible Verbosity and Tags"},"content":{"rendered":"\n
A question that sometimes comes up on the forums about Ansible is how to cut down on its verbose output, especially how to suppress output for skipped tasks. One solution to this problem is to use tags <\/mark>instead of when<\/mark>. Tags allow you to bypass a task completely, as if it weren’t even in the file. However, there are some caveats. The following snippet demonstrates how this works.<\/p>\n\n\n\n If you run this, defining skip at the command line, you see that both the whens are skipped as expected, but they still appear in the output. By contrast, the first skip does not appear at all, which is what we want. Notice, though, that even though skip2 is defined, the second tags debug runs. The tags are evaluated before the file is run, so you need to take this into account when deciding whether you want to use tags to skip a task.<\/p>\n\n\n\n If you run this without defining skip, you get the output below, which does run the first tags. So it’s clear that you can use tags to get a similar behavior to when but without the undesirable verbosity. There is one more caveat. It appears that you can only use the fact that a variable is bound or not. If you try to use the value of the variable to control the tags, it won’t work.<\/p>\n\n\n\n In some situations, the fact that tags are evaluated before the file runs may be desirable behavior. In the example below, I’m including a file recursively twice. The first time through, the include is run because first has not yet be defined when the tags are evaluated. The second time through, it skips the include because first was defined before the second recursive call. If you tried to do this in the exact same way using when, it wouldn’t work since first would be defined by the time the when is evaluated. In this case, you could probably come up with a slightly different way to make this work with when, but the point is that you can achieve different behavior by using tags instead.<\/p>\n\n\n\n A question that sometimes comes up on the forums about Ansible is how to cut down on its verbose output, especially how to suppress output for skipped tasks. One solution to this problem is to use tags instead of when. Tags allow you to bypass a task completely, as if it weren’t even in the …<\/p>\n- set_fact:\n skip2: false\n\n- tags: '{{\"always\" if skip is undefined else \"never\"}}'\n debug:\n msg: 'Tag skip not skipped ({{skip is undefined}})'\n\n- tags: '{{\"always\" if skip2 is undefined else \"never\"}}'\n debug:\n msg: 'Tag skip2 not skipped ({{skip2 is undefined}})'\n\n- when: skip is undefined\n debug:\n msg: 'When skip not skipped ({{skip is undefined}})'\n\n- when: skip2 is undefined\n debug:\n msg: 'When skip2 not skipped ({{skip2 is undefined}})'<\/mark><\/code><\/pre>\n\n\n\n
> ansible-playbook skip.yml -e \"skip=\"<\/mark>\n\nTASK [set_fact] ********************************************************************************************************************************************************************\r\nok: [localhost]\r\n\r\nTASK [debug] ***********************************************************************************************************************************************************************\r\nok: [localhost] => {\r\n \"msg\": \"Tag skip2 not skipped (False)\"\r\n}\r\n\r\nTASK [debug] ***********************************************************************************************************************************************************************\r\nskipping<\/mark><\/mark>: [localhost]\r\n\r\nTASK [debug] ***********************************************************************************************************************************************************************\r\nskipping<\/mark><\/mark>: [localhost]<\/code><\/pre>\n\n\n\n
> ansible-playbook skip.yml<\/mark>\n\nTASK [set_fact] ********************************************************************************************************************************************************************\nok: [localhost]\n\nTASK [debug] ***********************************************************************************************************************************************************************\nok: [localhost] => {\n \"msg\": \"Tag skip not skipped (True)\"\n}\n\nTASK [debug] ***********************************************************************************************************************************************************************\nok: [localhost] => {\n \"msg\": \"Tag skip2 not skipped (False)\"\n}\n\nTASK [debug] ***********************************************************************************************************************************************************************\nok: [localhost] => {\n \"msg\": \"When skip not skipped (True)\"\n}\n\nTASK [debug] ***********************************************************************************************************************************************************************\nskipping<\/mark><\/mark>: [localhost]<\/code><\/pre>\n\n\n\n
- debug:\n msg: 'First attempt ({{first is undefined}})'\n\n- vars:\n first: false\n tags: '{{\"always\" if first is undefined else \"never\"}}'\n include_tasks: try_something.yml<\/mark><\/code><\/pre>\n\n\n\n
TASK [include_tasks] ***************************************************************************************************************************************************************\r\nincluded: \/home\/user\/try_something.yml for localhost\r\n\r\nTASK [debug] ***********************************************************************************************************************************************************************\r\nok: [localhost] => {\r\n \"msg\": \"First attempt (True)<\/mark>\"\r\n}\r\n\r\nTASK [include_tasks] ***************************************************************************************************************************************************************\r\nincluded: \/home\/user\/try_something.yml for localhost\r\n\r\nTASK [debug] ***********************************************************************************************************************************************************************\r\nok: [localhost] => {\r\n \"msg\": \"First attempt (False)<\/mark>\"\r\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"