{"id":6106,"date":"2019-07-03T07:31:07","date_gmt":"2019-07-03T07:31:07","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=6106"},"modified":"2019-11-29T06:01:08","modified_gmt":"2019-11-29T06:01:08","slug":"how-to-specifying-multiple-groups-in-a-playbook-hosts-specification","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/how-to-specifying-multiple-groups-in-a-playbook-hosts-specification\/","title":{"rendered":"How to specifying multiple groups in a playbook hosts specification?"},"content":{"rendered":"\n<p>By design, Ansible executes just one play at a time. Your playbook consists of two plays (the two items in the root-level YAML list defined by the playbook file).<\/p>\n\n\n\n<p><strong>Method 1<\/strong><br> The following patterns address one or more groups. Groups separated by a colon indicate an \u201cOR\u201d configuration. This means the host may be in either one group or the other:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">---\n- name: This sets up an httpd webserver\n  <span class=\"hljs-attr\">hosts<\/span>: web:db\n\n  <span class=\"hljs-attr\">tasks<\/span>:\n  - name: Install the httpd apps\n    <span class=\"hljs-attr\">yum<\/span>: name=httpd\n  - name: start the httpd service\n    <span class=\"hljs-attr\">service<\/span>: name=httpd state=started\n  - debug:\n      msg: System {{ myname }} has uuid {{ ansible_product_uuid }}\n  - debug: msg=<span class=\"hljs-string\">\"System {{ myname }} has uuid {{ ansible_product_uuid }}\"<\/span>\n\n\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Method 2<\/strong><br> You can exclude groups as well, for instance, all machines must be in the group webservers but not in the group phoenix:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">---\n- name: This sets up an httpd webserver\n  <span class=\"hljs-attr\">hosts<\/span>: webservers:!phoenix\n\n  <span class=\"hljs-attr\">tasks<\/span>:\n  - name: Install the httpd apps\n    <span class=\"hljs-attr\">yum<\/span>: name=httpd\n  - name: start the httpd service\n    <span class=\"hljs-attr\">service<\/span>: name=httpd state=started\n  - debug:\n      msg: System {{ myname }} has uuid {{ ansible_product_uuid }}\n  - debug: msg=<span class=\"hljs-string\">\"System {{ myname }} has uuid {{ ansible_product_uuid }}\"<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Method 3<\/strong><br> You can also specify the intersection of two groups. This would mean the hosts must be in the group webservers and the host must also be in the group staging:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">---\n- name: This sets up an httpd webserver\n  <span class=\"hljs-attr\">hosts<\/span>: webservers:&amp;staging\n  <span class=\"hljs-attr\">tasks<\/span>:\n  - name: Install the httpd apps\n    <span class=\"hljs-attr\">yum<\/span>: name=httpd\n  - name: start the httpd service\n    <span class=\"hljs-attr\">service<\/span>: name=httpd state=started\n  - debug:\n      msg: System {{ myname }} has uuid {{ ansible_product_uuid }}\n  - debug: msg=<span class=\"hljs-string\">\"System {{ myname }} has uuid {{ ansible_product_uuid }}\"<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Method 4<\/strong><br> You can also specify the intersection of two groups. This would mean the hosts must be in the group webservers and the host must also be in the group staging: The below configuration means \u201call machines in the groups \u2018webservers\u2019 and \u2018dbservers\u2019 are to be managed if they are in the group \u2018staging\u2019 also, but the machines are not to be managed if they are in the group \u2018phoenix\u2019 \u2026 whew!<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">---\n- name: This sets up an httpd webserver\n  <span class=\"hljs-attr\">hosts<\/span>: webservers:dbservers:&amp;staging:!phoenix\n  <span class=\"hljs-attr\">tasks<\/span>:\n  - name: Install the httpd apps\n    <span class=\"hljs-attr\">yum<\/span>: name=httpd\n  - name: start the httpd service\n    <span class=\"hljs-attr\">service<\/span>: name=httpd state=started\n  - debug:\n      msg: System {{ myname }} has uuid {{ ansible_product_uuid }}\n  - debug: msg=<span class=\"hljs-string\">\"System {{ myname }} has uuid {{ ansible_product_uuid }}\"<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Method 5<\/strong><br> You can also use variables if you want to pass some group specifiers via the \u201c-e\u201d argument to ansible-playbook, but this is uncommonly used:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">---\n- name: This sets up an httpd webserver\n  <span class=\"hljs-attr\">hosts<\/span>: webservers:!{{excluded}}:&amp;{{required}}\n  <span class=\"hljs-attr\">tasks<\/span>:\n  - name: Install the httpd apps\n    <span class=\"hljs-attr\">yum<\/span>: name=httpd\n  - name: start the httpd service\n    <span class=\"hljs-attr\">service<\/span>: name=httpd state=started\n  - debug:\n      msg: System {{ myname }} has uuid {{ ansible_product_uuid }}\n  - debug: msg=<span class=\"hljs-string\">\"System {{ myname }} has uuid {{ ansible_product_uuid }}\"<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Reference<\/strong><br> <a href=\"https:\/\/docs.ansible.com\/ansible\/latest\/user_guide\/intro_patterns.html\" target=\"_blank\" rel=\"noopener\">https:\/\/docs.ansible.com\/ansible\/latest\/user_guide\/intro_patterns.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>By design, Ansible executes just one play at a time. Your playbook consists of two plays (the two items in the root-level YAML list defined by the playbook file). Method 1 The following patterns address one or more groups. Groups separated by a colon indicate an \u201cOR\u201d configuration. This means the host may be in&#8230;<\/p>\n","protected":false},"author":1,"featured_media":7898,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_joinchat":[],"footnotes":""},"categories":[5038],"tags":[639,5449,5562,5040,1951],"class_list":["post-6106","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ansible","tag-ansible","tag-devopsschool","tag-groups","tag-playbook","tag-specification"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/6106","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=6106"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/6106\/revisions"}],"predecessor-version":[{"id":6108,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/6106\/revisions\/6108"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/7898"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=6106"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=6106"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=6106"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}