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












0














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 '18 at 12:03










  • Check this: stackoverflow.com/a/43487756/2795592
    – Konstantin Suvorov
    Nov 19 '18 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 '18 at 12:47
















0














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 '18 at 12:03










  • Check this: stackoverflow.com/a/43487756/2795592
    – Konstantin Suvorov
    Nov 19 '18 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 '18 at 12:47














0












0








0







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 '18 at 12:50

























asked Nov 19 '18 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 '18 at 12:03










  • Check this: stackoverflow.com/a/43487756/2795592
    – Konstantin Suvorov
    Nov 19 '18 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 '18 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 '18 at 12:03










  • Check this: stackoverflow.com/a/43487756/2795592
    – Konstantin Suvorov
    Nov 19 '18 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 '18 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 '18 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 '18 at 12:03












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




Check this: stackoverflow.com/a/43487756/2795592
– Konstantin Suvorov
Nov 19 '18 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 '18 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 '18 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',
autoActivateHeartbeat: false,
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




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




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

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

SQL update select statement

'app-layout' is not a known element: how to share Component with different Modules