{"id":1608,"date":"2023-01-24T02:36:18","date_gmt":"2023-01-23T21:06:18","guid":{"rendered":"https:\/\/trysiteprice.com\/blog\/?p=1608"},"modified":"2023-01-24T02:36:18","modified_gmt":"2023-01-23T21:06:18","slug":"how-to-define-multiple-when-conditions-in-ansible","status":"publish","type":"post","link":"https:\/\/trysiteprice.com\/blog\/how-to-define-multiple-when-conditions-in-ansible\/","title":{"rendered":"How to define multiple when conditions in Ansible"},"content":{"rendered":"<p>In Ansible, the <code>when<\/code> statement is used to conditionally execute a task based on the outcome of a test. You can use multiple <code>when<\/code> statements in a task by using the <code>and<\/code> and <code>or<\/code> operators to join multiple tests.<\/p>\n<p>To use multiple <code>when<\/code> statements with the <code>and<\/code> operator, you can use the <code>and<\/code> keyword followed by a test. For example, the following task will only run when both the variable <code>var1<\/code> is equal to <code>value1<\/code> and the variable <code>var2<\/code> is equal to <code>value2<\/code>:<\/p>\n<div class=\"bg-black mb-4 rounded-md\">\n<div class=\"flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans\"><\/div>\n<div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre-wrap hljs language-bash\">- name: Example task<br \/>\n  <span class=\"hljs-built_in\">command<\/span>: <span class=\"hljs-built_in\">echo<\/span> <span class=\"hljs-string\">\"This task runs only when var1 is value1 and var2 is value2\"<\/span><br \/>\n  when: var1 == <span class=\"hljs-string\">'value1'<\/span> and var2 == <span class=\"hljs-string\">'value2'<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<p>To use multiple <code>when<\/code> statements with the <code>or<\/code> operator, you can use the <code>or<\/code> keyword followed by a test. For example, the following task will run when either the variable <code>var1<\/code> is equal to <code>value1<\/code> or the variable <code>var2<\/code> is equal to <code>value2<\/code>:<\/p>\n<div class=\"bg-black mb-4 rounded-md\">\n<div class=\"flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans\"><\/div>\n<div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre-wrap hljs language-bash\">- name: Example task<br \/>\n  <span class=\"hljs-built_in\">command<\/span>: <span class=\"hljs-built_in\">echo<\/span> <span class=\"hljs-string\">\"This task runs only when var1 is value1 or var2 is value2\"<\/span><br \/>\n  when: var1 == <span class=\"hljs-string\">'value1'<\/span> or var2 == <span class=\"hljs-string\">'value2'<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<p>You can also use parenthesis to make the conditions more readable. For example<\/p>\n<div class=\"bg-black mb-4 rounded-md\">\n<div class=\"flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans\"><\/div>\n<div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre-wrap hljs language-bash\">- name: Example task<br \/>\n  <span class=\"hljs-built_in\">command<\/span>: <span class=\"hljs-built_in\">echo<\/span> <span class=\"hljs-string\">\"This task runs only when var1 is value1 or (var2 is value2 and var3 is value3)\"<\/span><br \/>\n  when: var1 == <span class=\"hljs-string\">'value1'<\/span> or (var2 == <span class=\"hljs-string\">'value2'<\/span> and var3 == <span class=\"hljs-string\">'value3'<\/span>)<br \/>\n<\/code><\/div>\n<\/div>\n<p>You can also use complex conditions with <code>jq<\/code> and <code>register<\/code><\/p>\n<div class=\"bg-black mb-4 rounded-md\">\n<div class=\"flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans\"><\/div>\n<div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre-wrap hljs language-bash\">- name: Example task<br \/>\n  shell: <span class=\"hljs-built_in\">echo<\/span> <span class=\"hljs-string\">'{\"key1\":\"value1\",\"key2\":\"value2\"}'<\/span><br \/>\n  register: json_output<br \/>\n- name: Another task<br \/>\n  <span class=\"hljs-built_in\">command<\/span>: <span class=\"hljs-built_in\">echo<\/span> <span class=\"hljs-string\">\"This task runs only when key1 is value1 and key2 is value2\"<\/span><br \/>\n  when: json_output.stdout | from_json | json_query(<span class=\"hljs-string\">'key1'<\/span>) == <span class=\"hljs-string\">'value1'<\/span> and json_output.stdout | from_json | json_query(<span class=\"hljs-string\">'key2'<\/span>) == <span class=\"hljs-string\">'value2'<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<p>You can use <code>when<\/code> statement in many modules like <code>command<\/code>, <code>shell<\/code>, <code>copy<\/code>, <code>template<\/code> etc.<\/p>\n<p>It&#8217;s important to remember that, when using multiple <code>when<\/code> statements, the tests are evaluated from left to right and the order of the tests can affect the outcome of the task.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Ansible, the when statement is used to conditionally execute a task based on the outcome of a test. You can use multiple when statements in a task by using the and and or operators to join multiple tests. To use multiple when statements with the and operator, you can use the and keyword followed &#8230; <a title=\"How to define multiple when conditions in Ansible\" class=\"read-more\" href=\"https:\/\/trysiteprice.com\/blog\/how-to-define-multiple-when-conditions-in-ansible\/\" aria-label=\"Read more about How to define multiple when conditions in Ansible\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1608","post","type-post","status-publish","format-standard","hentry","category-best-tutorial"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v23.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to define multiple when conditions in Ansible - TrySitePrice<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/trysiteprice.com\/blog\/how-to-define-multiple-when-conditions-in-ansible\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to define multiple when conditions in Ansible - TrySitePrice\" \/>\n<meta property=\"og:description\" content=\"In Ansible, the when statement is used to conditionally execute a task based on the outcome of a test. You can use multiple when statements in a task by using the and and or operators to join multiple tests. To use multiple when statements with the and operator, you can use the and keyword followed ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/trysiteprice.com\/blog\/how-to-define-multiple-when-conditions-in-ansible\/\" \/>\n<meta property=\"og:site_name\" content=\"TrySitePrice\" \/>\n<meta property=\"article:published_time\" content=\"2023-01-23T21:06:18+00:00\" \/>\n<meta name=\"author\" content=\"Rahul Sahu\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/trysiteprice.com\/blog\/how-to-define-multiple-when-conditions-in-ansible\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/trysiteprice.com\/blog\/how-to-define-multiple-when-conditions-in-ansible\/\"},\"author\":{\"name\":\"Rahul Sahu\",\"@id\":\"https:\/\/trysiteprice.com\/blog\/#\/schema\/person\/358e04eeea4281deacad2f30c58e67f4\"},\"headline\":\"How to define multiple when conditions in Ansible\",\"datePublished\":\"2023-01-23T21:06:18+00:00\",\"dateModified\":\"2023-01-23T21:06:18+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/trysiteprice.com\/blog\/how-to-define-multiple-when-conditions-in-ansible\/\"},\"wordCount\":176,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/trysiteprice.com\/blog\/#organization\"},\"articleSection\":[\"Best\/Tutorial\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/trysiteprice.com\/blog\/how-to-define-multiple-when-conditions-in-ansible\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/trysiteprice.com\/blog\/how-to-define-multiple-when-conditions-in-ansible\/\",\"url\":\"https:\/\/trysiteprice.com\/blog\/how-to-define-multiple-when-conditions-in-ansible\/\",\"name\":\"How to define multiple when conditions in Ansible - TrySitePrice\",\"isPartOf\":{\"@id\":\"https:\/\/trysiteprice.com\/blog\/#website\"},\"datePublished\":\"2023-01-23T21:06:18+00:00\",\"dateModified\":\"2023-01-23T21:06:18+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/trysiteprice.com\/blog\/how-to-define-multiple-when-conditions-in-ansible\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/trysiteprice.com\/blog\/how-to-define-multiple-when-conditions-in-ansible\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/trysiteprice.com\/blog\/how-to-define-multiple-when-conditions-in-ansible\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/trysiteprice.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to define multiple when conditions in Ansible\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/trysiteprice.com\/blog\/#website\",\"url\":\"https:\/\/trysiteprice.com\/blog\/\",\"name\":\"TrySitePrice\",\"description\":\"Free Website Value Calculator Tool\",\"publisher\":{\"@id\":\"https:\/\/trysiteprice.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/trysiteprice.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/trysiteprice.com\/blog\/#organization\",\"name\":\"TrySitePrice\",\"url\":\"https:\/\/trysiteprice.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/trysiteprice.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/trysiteprice.com\/blog\/wp-content\/uploads\/2021\/12\/cropped-trysiteprice-logo.png\",\"contentUrl\":\"https:\/\/trysiteprice.com\/blog\/wp-content\/uploads\/2021\/12\/cropped-trysiteprice-logo.png\",\"width\":395,\"height\":268,\"caption\":\"TrySitePrice\"},\"image\":{\"@id\":\"https:\/\/trysiteprice.com\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/trysiteprice.com\/blog\/#\/schema\/person\/358e04eeea4281deacad2f30c58e67f4\",\"name\":\"Rahul Sahu\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/trysiteprice.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/51f0f95f7b95665f62baed2211572165?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/51f0f95f7b95665f62baed2211572165?s=96&d=mm&r=g\",\"caption\":\"Rahul Sahu\"},\"sameAs\":[\"https:\/\/trysiteprice.com\/blog\"],\"url\":\"https:\/\/trysiteprice.com\/blog\/author\/rsahu4242_trysiteprice\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to define multiple when conditions in Ansible - TrySitePrice","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/trysiteprice.com\/blog\/how-to-define-multiple-when-conditions-in-ansible\/","og_locale":"en_US","og_type":"article","og_title":"How to define multiple when conditions in Ansible - TrySitePrice","og_description":"In Ansible, the when statement is used to conditionally execute a task based on the outcome of a test. You can use multiple when statements in a task by using the and and or operators to join multiple tests. To use multiple when statements with the and operator, you can use the and keyword followed ... Read more","og_url":"https:\/\/trysiteprice.com\/blog\/how-to-define-multiple-when-conditions-in-ansible\/","og_site_name":"TrySitePrice","article_published_time":"2023-01-23T21:06:18+00:00","author":"Rahul Sahu","twitter_card":"summary_large_image","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/trysiteprice.com\/blog\/how-to-define-multiple-when-conditions-in-ansible\/#article","isPartOf":{"@id":"https:\/\/trysiteprice.com\/blog\/how-to-define-multiple-when-conditions-in-ansible\/"},"author":{"name":"Rahul Sahu","@id":"https:\/\/trysiteprice.com\/blog\/#\/schema\/person\/358e04eeea4281deacad2f30c58e67f4"},"headline":"How to define multiple when conditions in Ansible","datePublished":"2023-01-23T21:06:18+00:00","dateModified":"2023-01-23T21:06:18+00:00","mainEntityOfPage":{"@id":"https:\/\/trysiteprice.com\/blog\/how-to-define-multiple-when-conditions-in-ansible\/"},"wordCount":176,"commentCount":0,"publisher":{"@id":"https:\/\/trysiteprice.com\/blog\/#organization"},"articleSection":["Best\/Tutorial"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/trysiteprice.com\/blog\/how-to-define-multiple-when-conditions-in-ansible\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/trysiteprice.com\/blog\/how-to-define-multiple-when-conditions-in-ansible\/","url":"https:\/\/trysiteprice.com\/blog\/how-to-define-multiple-when-conditions-in-ansible\/","name":"How to define multiple when conditions in Ansible - TrySitePrice","isPartOf":{"@id":"https:\/\/trysiteprice.com\/blog\/#website"},"datePublished":"2023-01-23T21:06:18+00:00","dateModified":"2023-01-23T21:06:18+00:00","breadcrumb":{"@id":"https:\/\/trysiteprice.com\/blog\/how-to-define-multiple-when-conditions-in-ansible\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/trysiteprice.com\/blog\/how-to-define-multiple-when-conditions-in-ansible\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/trysiteprice.com\/blog\/how-to-define-multiple-when-conditions-in-ansible\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/trysiteprice.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to define multiple when conditions in Ansible"}]},{"@type":"WebSite","@id":"https:\/\/trysiteprice.com\/blog\/#website","url":"https:\/\/trysiteprice.com\/blog\/","name":"TrySitePrice","description":"Free Website Value Calculator Tool","publisher":{"@id":"https:\/\/trysiteprice.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/trysiteprice.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/trysiteprice.com\/blog\/#organization","name":"TrySitePrice","url":"https:\/\/trysiteprice.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/trysiteprice.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/trysiteprice.com\/blog\/wp-content\/uploads\/2021\/12\/cropped-trysiteprice-logo.png","contentUrl":"https:\/\/trysiteprice.com\/blog\/wp-content\/uploads\/2021\/12\/cropped-trysiteprice-logo.png","width":395,"height":268,"caption":"TrySitePrice"},"image":{"@id":"https:\/\/trysiteprice.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/trysiteprice.com\/blog\/#\/schema\/person\/358e04eeea4281deacad2f30c58e67f4","name":"Rahul Sahu","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/trysiteprice.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/51f0f95f7b95665f62baed2211572165?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/51f0f95f7b95665f62baed2211572165?s=96&d=mm&r=g","caption":"Rahul Sahu"},"sameAs":["https:\/\/trysiteprice.com\/blog"],"url":"https:\/\/trysiteprice.com\/blog\/author\/rsahu4242_trysiteprice\/"}]}},"_links":{"self":[{"href":"https:\/\/trysiteprice.com\/blog\/wp-json\/wp\/v2\/posts\/1608","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/trysiteprice.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/trysiteprice.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/trysiteprice.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/trysiteprice.com\/blog\/wp-json\/wp\/v2\/comments?post=1608"}],"version-history":[{"count":1,"href":"https:\/\/trysiteprice.com\/blog\/wp-json\/wp\/v2\/posts\/1608\/revisions"}],"predecessor-version":[{"id":1612,"href":"https:\/\/trysiteprice.com\/blog\/wp-json\/wp\/v2\/posts\/1608\/revisions\/1612"}],"wp:attachment":[{"href":"https:\/\/trysiteprice.com\/blog\/wp-json\/wp\/v2\/media?parent=1608"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/trysiteprice.com\/blog\/wp-json\/wp\/v2\/categories?post=1608"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/trysiteprice.com\/blog\/wp-json\/wp\/v2\/tags?post=1608"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}