Conditionnal 'changed_when' based on output in a 'with_items' task











up vote
0
down vote

favorite












I have a simple shell task which is feed with_items. I want to have the 'changed' status only when the run actually did something. So for that I have to parse the output and look for a «Done».



The issue is using a register at the same time that the with_items generates a list of results. And I don't find a way to access the stdout of the current loop iteration.



---
- name: Test to set changed status only on when output match with 'Done'
hosts: my-host-01
gather_facts: no

tasks:
- name: Task doing stuff
shell: |
echo {{ item }}
register: do_stuff
with_items:
- "Doing... Done!"
- "Nothing to do"
#changed_when: do_stuff.results[{{ item.id }}].stdout.find('Done')

- name: Print 'do_stuff' var to debug
debug:
var: do_stuff
- name: Try to access to the desired stdout
debug:
var: do_stuff.results[0].stdout


The expected result that I want would be:



TASK [Task doing stuff]
*************************************************************************
changed: [cnode02] => (item=Doing... Done!)
Ok: [cnode02] => (item=Nothing to do)









share|improve this question
























  • The following answer didn't help me as it process it in the next task which is too late to update the task status I assume.
    – xenlo
    Nov 19 at 12:03










  • Check this: stackoverflow.com/a/43487756/2795592
    – Konstantin Suvorov
    Nov 19 at 12:06










  • That's also done in 2 tasks. It will not help me to have the correct status of my initial task.
    – xenlo
    Nov 19 at 12:47















up vote
0
down vote

favorite












I have a simple shell task which is feed with_items. I want to have the 'changed' status only when the run actually did something. So for that I have to parse the output and look for a «Done».



The issue is using a register at the same time that the with_items generates a list of results. And I don't find a way to access the stdout of the current loop iteration.



---
- name: Test to set changed status only on when output match with 'Done'
hosts: my-host-01
gather_facts: no

tasks:
- name: Task doing stuff
shell: |
echo {{ item }}
register: do_stuff
with_items:
- "Doing... Done!"
- "Nothing to do"
#changed_when: do_stuff.results[{{ item.id }}].stdout.find('Done')

- name: Print 'do_stuff' var to debug
debug:
var: do_stuff
- name: Try to access to the desired stdout
debug:
var: do_stuff.results[0].stdout


The expected result that I want would be:



TASK [Task doing stuff]
*************************************************************************
changed: [cnode02] => (item=Doing... Done!)
Ok: [cnode02] => (item=Nothing to do)









share|improve this question
























  • The following answer didn't help me as it process it in the next task which is too late to update the task status I assume.
    – xenlo
    Nov 19 at 12:03










  • Check this: stackoverflow.com/a/43487756/2795592
    – Konstantin Suvorov
    Nov 19 at 12:06










  • That's also done in 2 tasks. It will not help me to have the correct status of my initial task.
    – xenlo
    Nov 19 at 12:47













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a simple shell task which is feed with_items. I want to have the 'changed' status only when the run actually did something. So for that I have to parse the output and look for a «Done».



The issue is using a register at the same time that the with_items generates a list of results. And I don't find a way to access the stdout of the current loop iteration.



---
- name: Test to set changed status only on when output match with 'Done'
hosts: my-host-01
gather_facts: no

tasks:
- name: Task doing stuff
shell: |
echo {{ item }}
register: do_stuff
with_items:
- "Doing... Done!"
- "Nothing to do"
#changed_when: do_stuff.results[{{ item.id }}].stdout.find('Done')

- name: Print 'do_stuff' var to debug
debug:
var: do_stuff
- name: Try to access to the desired stdout
debug:
var: do_stuff.results[0].stdout


The expected result that I want would be:



TASK [Task doing stuff]
*************************************************************************
changed: [cnode02] => (item=Doing... Done!)
Ok: [cnode02] => (item=Nothing to do)









share|improve this question















I have a simple shell task which is feed with_items. I want to have the 'changed' status only when the run actually did something. So for that I have to parse the output and look for a «Done».



The issue is using a register at the same time that the with_items generates a list of results. And I don't find a way to access the stdout of the current loop iteration.



---
- name: Test to set changed status only on when output match with 'Done'
hosts: my-host-01
gather_facts: no

tasks:
- name: Task doing stuff
shell: |
echo {{ item }}
register: do_stuff
with_items:
- "Doing... Done!"
- "Nothing to do"
#changed_when: do_stuff.results[{{ item.id }}].stdout.find('Done')

- name: Print 'do_stuff' var to debug
debug:
var: do_stuff
- name: Try to access to the desired stdout
debug:
var: do_stuff.results[0].stdout


The expected result that I want would be:



TASK [Task doing stuff]
*************************************************************************
changed: [cnode02] => (item=Doing... Done!)
Ok: [cnode02] => (item=Nothing to do)






loops ansible






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 12:50

























asked Nov 19 at 11:59









xenlo

106




106












  • The following answer didn't help me as it process it in the next task which is too late to update the task status I assume.
    – xenlo
    Nov 19 at 12:03










  • Check this: stackoverflow.com/a/43487756/2795592
    – Konstantin Suvorov
    Nov 19 at 12:06










  • That's also done in 2 tasks. It will not help me to have the correct status of my initial task.
    – xenlo
    Nov 19 at 12:47


















  • The following answer didn't help me as it process it in the next task which is too late to update the task status I assume.
    – xenlo
    Nov 19 at 12:03










  • Check this: stackoverflow.com/a/43487756/2795592
    – Konstantin Suvorov
    Nov 19 at 12:06










  • That's also done in 2 tasks. It will not help me to have the correct status of my initial task.
    – xenlo
    Nov 19 at 12:47
















The following answer didn't help me as it process it in the next task which is too late to update the task status I assume.
– xenlo
Nov 19 at 12:03




The following answer didn't help me as it process it in the next task which is too late to update the task status I assume.
– xenlo
Nov 19 at 12:03












Check this: stackoverflow.com/a/43487756/2795592
– Konstantin Suvorov
Nov 19 at 12:06




Check this: stackoverflow.com/a/43487756/2795592
– Konstantin Suvorov
Nov 19 at 12:06












That's also done in 2 tasks. It will not help me to have the correct status of my initial task.
– xenlo
Nov 19 at 12:47




That's also done in 2 tasks. It will not help me to have the correct status of my initial task.
– xenlo
Nov 19 at 12:47

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374175%2fconditionnal-changed-when-based-on-output-in-a-with-items-task%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374175%2fconditionnal-changed-when-based-on-output-in-a-with-items-task%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'