{"id":17982,"date":"2022-08-03T18:51:34","date_gmt":"2022-08-03T18:51:34","guid":{"rendered":"https:\/\/linuxways.net\/?p=17982"},"modified":"2022-08-17T06:26:51","modified_gmt":"2022-08-17T06:26:51","slug":"using-tags-ansible-playbook","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/centos\/using-tags-ansible-playbook\/","title":{"rendered":"Using Tags in Ansible Playbook"},"content":{"rendered":"<p>Tags are basically metadata that are attached to the tasks, roles, plays, etc. Using tags has a big advantage in saving time on and simplifying the running and debugging jobs of playbooks. It also makes the playbook more organized and robust.<\/p>\n<h2><strong>What Will We Cover?<\/strong><\/h2>\n<p>In this guide, we will see how to use the tags with Ansible playbooks. We also see some basic examples to demonstrate the tags in action wherever necessary.<\/p>\n<h2><strong>What Will You Need?<\/strong><\/h2>\n<p>As our previous labs, we need to have the following requirements to perform the examples shown in this tutorial:<\/p>\n<p>1. You should have an installed Ansible on the controller node (Ubuntu 20.04 in our case).<\/p>\n<p>2. You should have a basic knowledge about what the purpose of Ansible is and how to write a playbook (and of course you need to know what a playbook is).<\/p>\n<p>If you are just starting to learn Ansible, we suggest that you first learn Vagrant and how to set up a basic local testing environment using Vagrant. Once you set up a basic lab consisting of one controller node and two target nodes, you are all set to perform these examples.<\/p>\n<h2><strong>Need of Tags in Ansible<\/strong><\/h2>\n<p>In many cases, you may want to run a specific part of a playbook instead of executing the entire playbook. This is very handy in cases where the playbook is very large. In such cases, tags are used to run or skip a task in a playbook. This is done in a two-step approach:<\/p>\n<p>1. Add the desired tags to the target tasks.<\/p>\n<p>2. Run or skip the tasks on the basis of the tags options.<\/p>\n<p>A tag can be defined for a task, block, role, or for an entire play. The \u201ctags\u201d keyword is used for defining a tag as we will soon see in the subsequent examples.<\/p>\n<h2><strong>Working with Tags<\/strong><\/h2>\n<h3><strong>1. Starting with the Basic Example<\/strong><\/h3>\n<p>In its most basic form, a single task can have one or more tags. Similarly, a common tag can be linked to more than one task. For example, consider the following playbook:<\/p>\n<pre>\r\n---\r\n\r\n- hosts: all\r\n\r\ngather_facts: yes\r\n\r\nbecome: true\r\n\r\ntasks:\r\n\r\n- name: Update the system repository information (\u2018apt-get update\u2019)\r\n\r\nansible.builtin.apt:\r\n\r\nupdate_cache: yes\r\n\r\ntags: ubuntu\r\n\r\n- name: Run the equivalent of \u2018apt-get dist-upgrade\u2019 to upgrade the system as a separate step\r\n\r\nansible.builtin.apt:\r\n\r\nupgrade: dist\r\n\r\ntags: [ ubuntu, distup ]\r\n\r\n- name: Install the apache web server and ntp program\r\n\r\nansible.builtin.apt:\r\n\r\nname:\r\n\r\n- apache2\r\n\r\n- ntp\r\n\r\nstate: present\r\n\r\ntags:\r\n\r\n- ntp\r\n\r\n- webservers\r\n\r\n- ubuntu\r\n\r\n- name: Restart apache\r\n\r\nansible.builtin.service:\r\n\r\nname: apache2\r\n\r\nstate: restarted\r\n\r\ntags:\r\n\r\n- ubuntu\r\n\r\n- RestartApache\r\n<\/pre>\n<p>There are four tasks in the given playbook:<\/p>\n<p>1. The first task has only one tag, while the others have more than one.<\/p>\n<p>2. The \u201cubuntu\u201d tag is a common one and is linked with all the four tasks.<\/p>\n<p>Using a tag for multiple tasks (tag reuse) executes all the tasks linked with that tag. For this, the \u201c\u2014tags\u201d flag is specified at the command line.<\/p>\n<p>Suppose we have already run the previous playbook once and when we want to only restart the apache service and do nothing with the other tasks. In this case, we have to run the playbook (our my-playbook.yml) in the following manner:<\/p>\n<pre>\r\n$ ansible-playbook myplaybook.yml -i \/path\/to\/inventory\/file --tags RestartApache\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1261\" height=\"492\" class=\"wp-image-18006\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17982-1.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17982-1.png 1261w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17982-1-300x117.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17982-1-1024x400.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17982-1-768x300.png 768w\" sizes=\"auto, (max-width: 1261px) 100vw, 1261px\" \/><\/p>\n<h2><strong>2. Skipping a Tag in a Playbook<\/strong><\/h2>\n<p>In cases where you want to skip a task from execution, Ansible provides you with the \u201c&#8211;skip-tags\u201d parameter. The rest of the playbook normally runs. In the previous example, let us ignore the task of upgrading the system (task:2). For this, the \u201cansible-playbook\u201d command is modified as:<\/p>\n<pre>\r\n$ ansible-playbook myplaybook.yml -i \/path\/to\/inventory\/file --skip-tags distup\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1262\" height=\"559\" class=\"wp-image-18017\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17982-2.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17982-2.png 1262w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17982-2-300x133.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17982-2-1024x454.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17982-2-768x340.png 768w\" sizes=\"auto, (max-width: 1262px) 100vw, 1262px\" \/><\/p>\n<h2><strong>3. The<\/strong> \u2018always\u2019<strong> and \u2018never\u2019 Tags<\/strong><\/h2>\n<p>Ansible has two reserved tags for two specific purposes. These are the \u2018never\u2019 and \u2018always&#8217; tags. The purpose of the \u2018always\u2019 tag is to always run the task it is linked with. The \u2018never\u2019 tag is opposite of the \u2018always\u2019 tag which skips the task it is linked with. The following example demonstrates this:<\/p>\n<pre>\r\n---\r\n\r\n- hosts: all\r\n\r\ngather_facts: yes\r\n\r\nbecome: true\r\n\r\ntasks:\r\n\r\n- name: The Ansible 'always' tag example\r\n\r\ndebug:\r\n\r\nmsg: \"I will be always shown here\"\r\n\r\ntags:\r\n\r\n- always\r\n\r\n- name: The Ansible 'never' tag example\r\n\r\ndebug:\r\n\r\nmsg: \"I will never shown up until you specifically allow me\"\r\n\r\ntags:\r\n\r\n- never\r\n<\/pre>\n<p>In the previous playbook, the first task is set to always run while the second is set to never run.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1067\" height=\"475\" class=\"wp-image-18020\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17982-3.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17982-3.png 1067w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17982-3-300x134.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17982-3-1024x456.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17982-3-768x342.png 768w\" sizes=\"auto, (max-width: 1067px) 100vw, 1067px\" \/><\/p>\n<p>In case we want to skip the task tagged with \u201calways\u201d, we have to explicitly specify this behavior on the command line as \u201c&#8211;skip-tags always\u201d. In the previous playbook, we can skip the first task as:<\/p>\n<pre>\r\n$ ansible-playbook myplaybook.yml -i \/path\/to\/inventory\/file --skip-tags always\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1176\" height=\"175\" class=\"wp-image-18023\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17982-4.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17982-4.png 1176w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17982-4-300x45.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17982-4-1024x152.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17982-4-768x114.png 768w\" sizes=\"auto, (max-width: 1176px) 100vw, 1176px\" \/><\/p>\n<p>Similarly, a task with the \u201cnever\u201d tag will never run unless it is tagged as \u201c&#8211;tags never\u201d. The second task can be executed as:<\/p>\n<pre>\r\n$ ansible-playbook myplaybook.yml -i \/path\/to\/inventory\/file --tags never\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1129\" height=\"644\" class=\"wp-image-18028\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17982-5.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17982-5.png 1129w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17982-5-300x171.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17982-5-1024x584.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17982-5-768x438.png 768w\" sizes=\"auto, (max-width: 1129px) 100vw, 1129px\" \/><\/p>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p>It is very critical to develop a well-structured playbook for imparting flexibility in the automation process. Like many other ways, Ansible provides tags to achieve this feature.<\/p>","protected":false},"excerpt":{"rendered":"<p>Practical guide on how to use the tags with Ansible playbooks for imparting flexibility in the automation process through some basic examples.<\/p>","protected":false},"author":102,"featured_media":18096,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-17982","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-centos"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/17982","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/102"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=17982"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/17982\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/18096"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=17982"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=17982"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=17982"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}