{"id":6218,"date":"2025-03-15T12:27:48","date_gmt":"2025-03-15T12:27:48","guid":{"rendered":"https:\/\/linuxbuz.com\/?p=6218"},"modified":"2025-07-11T04:31:41","modified_gmt":"2025-07-11T04:31:41","slug":"ansible-changed_when-and-failed_when","status":"publish","type":"post","link":"https:\/\/linuxbuz.com\/devops\/ansible-changed_when-and-failed_when","title":{"rendered":"Ansible changed_when and failed_when with Examples"},"content":{"rendered":"<p class=\"\" style=\"text-align: justify;\" data-start=\"376\" data-end=\"533\">Have you ever had an Ansible task report <strong data-start=\"408\" data-end=\"421\">\u201cchanged\u201d<\/strong> even though it didn\u2019t really change anything? Or worse, mark a task as <strong data-start=\"492\" data-end=\"504\">\u201cfailed\u201d<\/strong> when the error was harmless?<\/p>\n<p class=\"\" style=\"text-align: justify;\" data-start=\"535\" data-end=\"596\">That\u2019s where <a href=\"https:\/\/docs.ansible.com\/ansible\/latest\/playbook_guide\/playbooks_error_handling.html\" target=\"_blank\" rel=\"noopener\"><strong>changed_when and failed_when<\/strong><\/a> come into play. These directives give you full control over how Ansible interprets task results\u2014so your automation is accurate, clean, and smart.<\/p>\n<p style=\"text-align: justify;\"><em>This guide explores how to use changed_when and failed_when with practical examples.<\/em><\/p>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#001a66;border-radius:3px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#004d99;color:#FFFFFF;border-top-left-radius:1px;border-top-right-radius:1px\">\ud83d\udca1 Recommended Reading<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:1px;border-bottom-right-radius:1px\">\n\ud83d\udc49 <a href=\"https:\/\/linuxbuz.com\/linuxhowto\/how-to-get-free-cloud-vps-server-step-by-step-guide\" target=\"_blank\" rel=\"noopener\">How to Get a Free Cloud Server for Testing<\/a><br \/>\n<\/div><\/div>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/linuxbuz.com\/devops\/ansible-changed_when-and-failed_when\/#What_Are_changed_when_and_failed_when\" >What Are changed_when and failed_when?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/linuxbuz.com\/devops\/ansible-changed_when-and-failed_when\/#Example_1_Avoid_False_Positives_When_Creating_Files\" >Example 1: Avoid False Positives When Creating Files<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/linuxbuz.com\/devops\/ansible-changed_when-and-failed_when\/#Example_2_Fail_When_Disk_Usage_Hits_Threshold\" >Example 2: Fail When Disk Usage Hits Threshold<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/linuxbuz.com\/devops\/ansible-changed_when-and-failed_when\/#Example_3_Start_a_Service_If_Inactive_Fail_If_Restart_Fails\" >Example 3: Start a Service If Inactive, Fail If Restart Fails<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/linuxbuz.com\/devops\/ansible-changed_when-and-failed_when\/#Example_4_Fail_if_No_Process_is_Listening_on_Port_80\" >Example 4: Fail if No Process is Listening on Port 80<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/linuxbuz.com\/devops\/ansible-changed_when-and-failed_when\/#Example_5_Report_Change_Only_on_Apache_Restart\" >Example 5: Report Change Only on Apache Restart<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/linuxbuz.com\/devops\/ansible-changed_when-and-failed_when\/#Example_6_Use_changed_when_for_Idempotent_Config_Changes\" >Example 6: Use changed_when for Idempotent Config Changes<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/linuxbuz.com\/devops\/ansible-changed_when-and-failed_when\/#_Troubleshooting_Tips\" >\u00a0Troubleshooting Tips<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/linuxbuz.com\/devops\/ansible-changed_when-and-failed_when\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/linuxbuz.com\/devops\/ansible-changed_when-and-failed_when\/#FAQs\" >FAQs<\/a><\/li><\/ul><\/nav><\/div>\n\n<h2 class=\"\" data-start=\"896\" data-end=\"944\"><span class=\"ez-toc-section\" id=\"What_Are_changed_when_and_failed_when\"><\/span>What Are <code data-start=\"911\" data-end=\"925\">changed_when<\/code> and <code data-start=\"930\" data-end=\"943\">failed_when<\/code>?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<div class=\"pointer-events-none relative left-[50%]! flex w-[100cqw] translate-x-[-50%] justify-center *:pointer-events-auto\">\n<div class=\"tableContainer horzScrollShadows\">\n<table class=\"min-w-full\" data-start=\"946\" data-end=\"1191\">\n<thead data-start=\"946\" data-end=\"969\">\n<tr data-start=\"946\" data-end=\"969\">\n<th data-start=\"946\" data-end=\"958\">Directive<\/th>\n<th data-start=\"958\" data-end=\"969\">Purpose<\/th>\n<\/tr>\n<\/thead>\n<tbody data-start=\"993\" data-end=\"1191\">\n<tr data-start=\"993\" data-end=\"1094\">\n<td class=\"max-w-[calc(var(--thread-content-max-width)*2\/3)]\" data-start=\"993\" data-end=\"1010\"><code data-start=\"995\" data-end=\"1009\">changed_when<\/code><\/td>\n<td class=\"max-w-[calc(var(--thread-content-max-width)*2\/3)] min-w-[calc(var(--thread-content-max-width)\/3)]\" data-start=\"1010\" data-end=\"1094\">Controls whether a task is marked as <strong data-start=\"1049\" data-end=\"1060\">changed<\/strong>, regardless of module behavior.<\/td>\n<\/tr>\n<tr data-start=\"1095\" data-end=\"1191\">\n<td class=\"max-w-[calc(var(--thread-content-max-width)*2\/3)]\" data-start=\"1095\" data-end=\"1112\"><code data-start=\"1097\" data-end=\"1110\">failed_when<\/code><\/td>\n<td class=\"max-w-[calc(var(--thread-content-max-width)*2\/3)] min-w-[calc(var(--thread-content-max-width)\/3)]\" data-start=\"1112\" data-end=\"1191\">Controls whether a task is marked as <strong data-start=\"1151\" data-end=\"1161\">failed<\/strong>, even if no error occurred.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p class=\"\" data-start=\"1193\" data-end=\"1204\">By default:<\/p>\n<ul data-start=\"1205\" data-end=\"1327\">\n<li class=\"\" data-start=\"1205\" data-end=\"1264\">\n<p class=\"\" data-start=\"1207\" data-end=\"1264\">A task is considered \u201cchanged\u201d if it performs any action.<\/p>\n<\/li>\n<li class=\"\" data-start=\"1265\" data-end=\"1327\">\n<p class=\"\" data-start=\"1267\" data-end=\"1327\">A task fails if it returns a non-zero exit code or an error.<\/p>\n<\/li>\n<\/ul>\n<p class=\"\" data-start=\"1329\" data-end=\"1416\">These directives override that behavior using conditions based on registered variables.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Example_1_Avoid_False_Positives_When_Creating_Files\"><\/span>Example 1: Avoid False Positives When Creating Files<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p class=\"\" style=\"text-align: justify;\" data-start=\"1565\" data-end=\"1703\">You want to <strong data-start=\"1577\" data-end=\"1619\">create a file only if it doesn&#8217;t exist <\/strong>and prevent Ansible from falsely reporting it as \u201cchanged\u201d when it&#8217;s already there.<\/p>\n<div class=\"contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary\">\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><span style=\"text-align: justify; font-size: 1rem;\">In this <\/span><a style=\"text-align: justify; background-color: white; font-size: 1rem;\" href=\"https:\/\/linuxbuz.com\/devops\/ansible-playbooks-examples\" target=\"_blank\" rel=\"noopener\"><strong>example playbook,<\/strong><\/a><span style=\"text-align: justify; font-size: 1rem;\"> the <\/span><strong style=\"text-align: justify; font-size: 1rem;\">changed_when<\/strong><span style=\"text-align: justify; font-size: 1rem;\"> directive ensures that the task is only marked as changed <\/span><a style=\"text-align: justify; background-color: white; font-size: 1rem;\" href=\"https:\/\/linuxbuz.com\/devops\/ansible-check-if-files-or-directories-exist\" target=\"_blank\" rel=\"noopener\"><strong>if the file didn&#8217;t exist<\/strong><\/a><span style=\"text-align: justify; font-size: 1rem;\"> beforehand.<\/span><\/div>\n<\/div>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>- name: Check and create file if not exists\r\n  hosts: localhost\r\n  tasks:\r\n    - name: Check if file exists\r\n      stat:\r\n        path: \/tmp\/example_file.txt\r\n      register: file_stat\r\n\r\n    - name: Create file if it does not exist\r\n      file:\r\n        path: \/tmp\/example_file.txt\r\n        state: touch\r\n      changed_when: not file_stat.stat.exists<\/code><\/pre>\n<p><strong>In this example:<\/strong><\/p>\n<ul>\n<li style=\"text-align: justify;\"><strong>stat module<\/strong> checks if the file <strong>\/tmp\/example_file.txt<\/strong> exists, storing the result in <strong>file_stat.<\/strong><\/li>\n<li style=\"text-align: justify;\">The <a href=\"https:\/\/linuxbuz.com\/devops\/ansible-file-module-examples\" target=\"_blank\" rel=\"noopener\"><strong>file module<\/strong><\/a> creates the file only if it doesn&#8217;t exist. The <strong>changed_when<\/strong> directive uses the condition not <strong>file_stat.stat.exists<\/strong> to ensure that Ansible reports a change only if the file was created.<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Example_2_Fail_When_Disk_Usage_Hits_Threshold\"><\/span>Example 2: Fail When Disk Usage Hits Threshold<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">You want to <strong data-start=\"2286\" data-end=\"2342\">fail a task if disk usage on <code data-start=\"2317\" data-end=\"2328\">\/dev\/sda1<\/code> exceeds 80%<\/strong>, but not always treat it as a failure.<\/p>\n<p style=\"text-align: justify;\">This playbook checks the <strong>disk usage<\/strong> of <strong>\/var<\/strong> and fails if <strong>\/dev\/sda1<\/strong> has reached <strong>80%<\/strong> capacity.<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>- name: Check disk usage\r\n  hosts: localhost\r\n  tasks:\r\n    - name: Get disk usage of \/var\r\n      command: df -h \/var\r\n      register: disk_usage\r\n      changed_when: false\r\n      failed_when: \"'\/dev\/sda1' in disk_usage.stdout and '80%' in disk_usage.stdout\"<\/code><\/pre>\n<p><strong>In this example:<\/strong><\/p>\n<ul>\n<li>Prevents the task from reporting as <strong>\u201cchanged\u201d.<\/strong><\/li>\n<li>Triggers failure only when \/dev\/sda1 hits <strong>80%<\/strong> usage.<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Example_3_Start_a_Service_If_Inactive_Fail_If_Restart_Fails\"><\/span>Example 3: Start a Service If Inactive, Fail If Restart Fails<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">Imagine a task that <a href=\"https:\/\/linuxbuz.com\/devops\/ansible-service_facts-module\" target=\"_blank\" rel=\"noopener\"><strong>checks if a service is running<\/strong><\/a> and starts it if it is not. Additionally, we want to fail the task if the service fails to start.<\/p>\n<p style=\"text-align: justify;\">This playbook checks if <strong>my_service<\/strong> is running and attempts to start it if it is not, marking the task as changed and failing the task if starting the service fails.<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>- name: Ensure my_service is running\r\n  hosts: localhost\r\n  tasks:\r\n    - name: Check if my_service is running\r\n      shell: systemctl is-active my_service\r\n      register: service_status\r\n      ignore_errors: yes\r\n\r\n    - name: Start my_service if not running\r\n      shell: systemctl start my_service\r\n      when: service_status.stdout != 'active'\r\n      changed_when: service_status.stdout != 'active'\r\n      failed_when: service_status.stdout != 'active' and service_status.rc != 0<\/code><\/pre>\n<p><strong>In this example:<\/strong><\/p>\n<ul>\n<li>Service starts only if it was not active.<\/li>\n<li>Reports as \u201cchanged\u201d only when needed.<\/li>\n<li>Fails if systemctl returns an error during the start process.<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Example_4_Fail_if_No_Process_is_Listening_on_Port_80\"><\/span>Example 4: Fail if No Process is Listening on Port 80<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p class=\"\" data-start=\"3657\" data-end=\"3742\">You want to ensure a service is <strong data-start=\"3689\" data-end=\"3713\">listening on port 80<\/strong>, and fail explicitly if not.<\/p>\n<p style=\"text-align: justify;\">This playbook checks if a process is running on <strong>port 80<\/strong> and fails with a specific message if no process is found.<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>- name: Check if process is running on port 80\r\n  hosts: localhost\r\n  tasks:\r\n    - name: Get process ID using port 80\r\n      shell: netstat -tuln | grep ':80'\r\n      register: process_check\r\n      ignore_errors: yes\r\n\r\n    - name: Fail if no process is found on port 80\r\n      fail:\r\n        msg: \"No process found running on port 80.\"\r\n      failed_when: process_check.stdout == ''<\/code><\/pre>\n<p><strong>In this example:<\/strong><\/p>\n<ul>\n<li style=\"text-align: justify;\"><strong>netstat command<\/strong> checks for any process listening on port <strong>80,<\/strong> storing the output in process_check.<\/li>\n<li style=\"text-align: justify;\"><strong><a href=\"https:\/\/linuxbuz.com\/devops\/ansible-ignore_errors-example\" target=\"_blank\" rel=\"noopener\">ignore_errors<\/a>: yes<\/strong> allows the playbook to continue even if the command fails (e.g., if the port is not in use).<\/li>\n<li style=\"text-align: justify;\"><strong>failed_when<\/strong> directive triggers a failure if <strong>process_check.stdout<\/strong> is empty, indicating no process was found on port <strong>80.<\/strong><\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Example_5_Report_Change_Only_on_Apache_Restart\"><\/span>Example 5: Report Change Only on Apache Restart<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">Imagine we have a playbook that checks the status of a web server and wants to report a change only if the server was not previously running and is started by the playbook.<\/p>\n<p style=\"text-align: justify;\">This playbook checks the status of the Apache web server and starts it if it is not active, marking the task as changed if the server was either inactive or failed.<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>- name: Check and start web server\r\n  hosts: localhost\r\n  tasks:\r\n    - name: Check web server status\r\n      command: systemctl is-active apache2\r\n      register: webserver_status\r\n      ignore_errors: yes\r\n\r\n    - name: Start web server\r\n      command: systemctl start apache2\r\n      when: webserver_status.stdout != 'active'\r\n      changed_when: \"'inactive' in webserver_status.stdout or 'failed' in webserver_status.stdout\"<\/code><\/pre>\n<p><strong>In this example:<\/strong><\/p>\n<ul>\n<li style=\"text-align: justify;\"><strong>systemctl is-active apache2<\/strong> command checks if the Apache web server is running, with the result stored in webserver_status.<\/li>\n<li style=\"text-align: justify;\"><strong>systemctl start apache2<\/strong> command starts the web server only if it is not active.<\/li>\n<li style=\"text-align: justify;\"><strong>changed_when<\/strong> directive reports a change if the web server was either &#8216;inactive&#8217; or <strong>&#8216;failed&#8217;<\/strong> before the playbook ran.<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Example_6_Use_changed_when_for_Idempotent_Config_Changes\"><\/span>Example 6: Use changed_when for Idempotent Config Changes<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p class=\"\" data-start=\"4921\" data-end=\"5028\">You only want to change a config <strong data-start=\"4954\" data-end=\"4990\">if a specific setting is missing<\/strong>\u2014and not every time the playbook runs.<\/p>\n<p style=\"text-align: justify;\">This playbook checks the current configuration in <strong>\/etc\/myapp\/config<\/strong> and sets the correct configuration if the setting <strong>correct_setting=true<\/strong> is not found, marking the task as changed if the configuration needs to be updated.<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>- name: Ensure correct configuration is set\r\n  hosts: localhost\r\n  tasks:\r\n    - name: Check current configuration\r\n      command: cat \/etc\/myapp\/config\r\n      register: current_config\r\n\r\n    - name: Set correct configuration\r\n      command: echo \"correct_setting=true\" &gt; \/etc\/myapp\/config\r\n      when: \"current_config.stdout.find('correct_setting=true') == -1\"\r\n      changed_when: \"current_config.stdout.find('correct_setting=true') == -1\"<\/code><\/pre>\n<p><strong>In this example:<\/strong><\/p>\n<ul>\n<li style=\"text-align: justify;\"><strong>cat command<\/strong> checks the current configuration, with the output stored in current_config.<\/li>\n<li style=\"text-align: justify;\">command <strong>echo &#8220;correct_setting=true&#8221;<\/strong> &gt; <strong>\/etc\/myapp\/config<\/strong> sets the configuration only if the correct setting is not already present.<\/li>\n<li style=\"text-align: justify;\"><strong>changed_when<\/strong> directive reports a change only if the setting was not already present.<\/li>\n<\/ul>\n<h2 class=\"\" data-start=\"5596\" data-end=\"5622\"><span class=\"ez-toc-section\" id=\"_Troubleshooting_Tips\"><\/span>\u00a0Troubleshooting Tips<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<div class=\"pointer-events-none relative left-[50%]! flex w-[100cqw] translate-x-[-50%] justify-center *:pointer-events-auto\">\n<div class=\"tableContainer horzScrollShadows\">\n<table class=\"min-w-full\" data-start=\"5624\" data-end=\"6176\">\n<thead data-start=\"5624\" data-end=\"5715\">\n<tr data-start=\"5624\" data-end=\"5715\">\n<th data-start=\"5624\" data-end=\"5659\">Problem<\/th>\n<th data-start=\"5659\" data-end=\"5715\">Fix<\/th>\n<\/tr>\n<\/thead>\n<tbody data-start=\"5809\" data-end=\"6176\">\n<tr data-start=\"5809\" data-end=\"5900\">\n<td class=\"max-w-[calc(var(--thread-content-max-width)*2\/3)]\" data-start=\"5809\" data-end=\"5844\">Task always marked as changed<\/td>\n<td class=\"max-w-[calc(var(--thread-content-max-width)*2\/3)] min-w-[calc(var(--thread-content-max-width)\/3)]\" data-start=\"5844\" data-end=\"5900\">Add <code data-start=\"5850\" data-end=\"5871\">changed_when: false<\/code> or use conditionally<\/td>\n<\/tr>\n<tr data-start=\"5901\" data-end=\"5992\">\n<td class=\"max-w-[calc(var(--thread-content-max-width)*2\/3)]\" data-start=\"5901\" data-end=\"5936\">Task fails with harmless errors<\/td>\n<td class=\"max-w-[calc(var(--thread-content-max-width)*2\/3)] min-w-[calc(var(--thread-content-max-width)\/3)]\" data-start=\"5936\" data-end=\"5992\">Use <code data-start=\"5942\" data-end=\"5968\">failed_when: &lt;condition&gt;<\/code> or <code data-start=\"5972\" data-end=\"5987\">ignore_errors<\/code><\/td>\n<\/tr>\n<tr data-start=\"5993\" data-end=\"6084\">\n<td class=\"max-w-[calc(var(--thread-content-max-width)*2\/3)]\" data-start=\"5993\" data-end=\"6028\">Want both conditions together<\/td>\n<td class=\"max-w-[calc(var(--thread-content-max-width)*2\/3)] min-w-[calc(var(--thread-content-max-width)\/3)]\" data-start=\"6028\" data-end=\"6084\">You can combine <code data-start=\"6046\" data-end=\"6060\">changed_when<\/code> + <code data-start=\"6063\" data-end=\"6076\">failed_when<\/code><\/td>\n<\/tr>\n<tr data-start=\"6085\" data-end=\"6176\">\n<td class=\"max-w-[calc(var(--thread-content-max-width)*2\/3)]\" data-start=\"6085\" data-end=\"6120\">Need debug info<\/td>\n<td class=\"max-w-[calc(var(--thread-content-max-width)*2\/3)]\" data-start=\"6120\" data-end=\"6176\">Use <code data-start=\"6126\" data-end=\"6134\">debug:<\/code> to print <code data-start=\"6144\" data-end=\"6154\">register<\/code> outputs<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Ansible&#8217;s changed_when and failed_when give you powerful control over task outcomes, helping you:<\/p>\n<ul>\n<li>Avoid false change reports<\/li>\n<li>Prevent unnecessary failures<\/li>\n<li>Build idempotent, intelligent automation workflows<\/li>\n<\/ul>\n<p>Use them wisely to improve playbook clarity, accuracy, and error handling.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"FAQs\"><\/span>FAQs<span class=\"ez-toc-section-end\"><\/span><\/h2>\n        <section class=\"sc_fs_faq sc_card \">\n            <div>\n\t\t\t\t<h6>1. Can I use changed_when with registered variables?<\/h6>                <div>\n\t\t\t\t\t                    <p>\n\t\t\t\t\t\tYes, changed_when can be used with <strong>registered<\/strong> variables to conditionally mark a task as changed based on the output of previous tasks.                    <\/p>\n                <\/div>\n            <\/div>\n        <\/section>\n\t\t        <section class=\"sc_fs_faq sc_card \">\n            <div>\n\t\t\t\t<h6>2. How can failed_when help in handling errors?<\/h6>                <div>\n\t\t\t\t\t                    <p>\n\t\t\t\t\t\tfailed_when allows you to define specific failure conditions, preventing unnecessary task failures due to non-critical errors or warnings.                    <\/p>\n                <\/div>\n            <\/div>\n        <\/section>\n\t\t        <section class=\"sc_fs_faq sc_card \">\n            <div>\n\t\t\t\t<h6>3. Can changed_when and failed_when be used together in the same task?<\/h6>                <div>\n\t\t\t\t\t                    <p>\n\t\t\t\t\t\tYes, both changed_when and failed_when can be used in a single task to control how a task's result is interpreted and whether it should be marked as changed or failed.                    <\/p>\n                <\/div>\n            <\/div>\n        <\/section>\n\t\t\n<script type=\"application\/ld+json\">\n    {\n\t\t\"@context\": \"https:\/\/schema.org\",\n\t\t\"@type\": \"FAQPage\",\n\t\t\"mainEntity\": [\n\t\t\t\t{\n\t\t\t\t\"@type\": \"Question\",\n\t\t\t\t\"name\": \"1. Can I use changed_when with registered variables?\",\n\t\t\t\t\"acceptedAnswer\": {\n\t\t\t\t\t\"@type\": \"Answer\",\n\t\t\t\t\t\"text\": \"Yes, changed_when can be used with <strong>registered<\/strong> variables to conditionally mark a task as changed based on the output of previous tasks.\"\n\t\t\t\t\t\t\t\t\t}\n\t\t\t}\n\t\t\t,\t\t\t\t{\n\t\t\t\t\"@type\": \"Question\",\n\t\t\t\t\"name\": \"2. How can failed_when help in handling errors?\",\n\t\t\t\t\"acceptedAnswer\": {\n\t\t\t\t\t\"@type\": \"Answer\",\n\t\t\t\t\t\"text\": \"failed_when allows you to define specific failure conditions, preventing unnecessary task failures due to non-critical errors or warnings.\"\n\t\t\t\t\t\t\t\t\t}\n\t\t\t}\n\t\t\t,\t\t\t\t{\n\t\t\t\t\"@type\": \"Question\",\n\t\t\t\t\"name\": \"3. Can changed_when and failed_when be used together in the same task?\",\n\t\t\t\t\"acceptedAnswer\": {\n\t\t\t\t\t\"@type\": \"Answer\",\n\t\t\t\t\t\"text\": \"Yes, both changed_when and failed_when can be used in a single task to control how a task's result is interpreted and whether it should be marked as changed or failed.\"\n\t\t\t\t\t\t\t\t\t}\n\t\t\t}\n\t\t\t\t    ]\n}\n<\/script>\n\n","protected":false},"excerpt":{"rendered":"<p>Have you ever had an Ansible task report \u201cchanged\u201d even though it didn\u2019t really change anything? Or worse, mark a task as \u201cfailed\u201d when the error was harmless? That\u2019s where &hellip; <\/p>\n","protected":false},"author":1,"featured_media":6219,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"two_page_speed":[],"footnotes":""},"categories":[828],"tags":[],"class_list":["post-6218","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/posts\/6218","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/comments?post=6218"}],"version-history":[{"count":0,"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/posts\/6218\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/media\/6219"}],"wp:attachment":[{"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/media?parent=6218"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/categories?post=6218"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/tags?post=6218"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}