How to find missing number from output in Ansible
My playlist has below contains
- set_fact:
missnu={{out.stdout_lines}}
register: missnu
- debug: msg={{missnu}}
And I am getting below output,
TASK [debug] **********************
ok: [192.168.0.10] => {
"msg": [
"1 2 3 4"
]
}
This is expected result, however I want to find the number which is missing from this.
Meaning if the result is "1 2 4"
OR "2 3 4"
OR "2 4"
how would I get those missing numbers?
ansible
add a comment |
My playlist has below contains
- set_fact:
missnu={{out.stdout_lines}}
register: missnu
- debug: msg={{missnu}}
And I am getting below output,
TASK [debug] **********************
ok: [192.168.0.10] => {
"msg": [
"1 2 3 4"
]
}
This is expected result, however I want to find the number which is missing from this.
Meaning if the result is "1 2 4"
OR "2 3 4"
OR "2 4"
how would I get those missing numbers?
ansible
add a comment |
My playlist has below contains
- set_fact:
missnu={{out.stdout_lines}}
register: missnu
- debug: msg={{missnu}}
And I am getting below output,
TASK [debug] **********************
ok: [192.168.0.10] => {
"msg": [
"1 2 3 4"
]
}
This is expected result, however I want to find the number which is missing from this.
Meaning if the result is "1 2 4"
OR "2 3 4"
OR "2 4"
how would I get those missing numbers?
ansible
My playlist has below contains
- set_fact:
missnu={{out.stdout_lines}}
register: missnu
- debug: msg={{missnu}}
And I am getting below output,
TASK [debug] **********************
ok: [192.168.0.10] => {
"msg": [
"1 2 3 4"
]
}
This is expected result, however I want to find the number which is missing from this.
Meaning if the result is "1 2 4"
OR "2 3 4"
OR "2 4"
how would I get those missing numbers?
ansible
ansible
asked Nov 13 '18 at 4:11
user10373379user10373379
124
124
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Try with below code (it's a simple python code), It worked for me. In below 6 is the limit which I have set to test you can change it as per your requirement.
So by this the numbers which you get from out.stdout.lines
will get removed from limit which you set (6 or any number which you want) and remaining numbers will show.
- name: Getting Missing Numbers
shell: python -c "ma = {{ out.stdout_lines }}; num = [int(i) for i in ma[0].split()]; mn = list(set(range(1, 6 + 1)) - set(num)); print(mn)"
register: mynu
- debug: msg={{mynu.stdout}}
Thanks a Lot, That is exactly what I needed.
– user10373379
Nov 13 '18 at 12:13
add a comment |
You're printing the stdout from some command but I'll assume you know the expected result and have that stored in a variable somewhere. For this I'd use the difference
filter.
- hosts: localhost
vars:
sample_out_ml: [ 1, 2, 3, 4 ]
sample_out_sl: "1 2 3 4"
expected: [ 1, 2, 3 ,4 ,5 ]
tasks:
- name: Show difference when cmd outputs multi-line
debug:
msg: "{{ expected | difference(sample_out_ml) }}"
- name: Show difference when cmd outputs on single line
debug:
msg: "{{ expected | difference(sample_out_sl.split(' ') | map('int') ) }}"
EDIT Based on your comments I provided a more robust example of how you might do this. If you're going all in on Ansible you should try and refrain from using the command
modules. Hope this helps.
It prints"msg": "Hello world!"
not the missing numbers
– user10373379
Nov 13 '18 at 6:00
I may have missed a quote or two. I updated my answer . What version of Ansible are you running?
– Petro026
Nov 13 '18 at 6:05
To Test this I have addedallnu: [ 1, 2, 3, 4, 5 ]
so the missing number should be5
instead it shows"msg": [ 1, 2, 3, 4, 5 ]
Ansible version is2.7.0
– user10373379
Nov 13 '18 at 7:14
It will not work, To verify this I have tried thismsg: "{{ allnu | difference([1, 2, 3, 4]) }}"
And I get exact missing number i.e5
. However my output like"1 2 3 4"
it's not a list to work with your way.
– user10373379
Nov 13 '18 at 7:56
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53273690%2fhow-to-find-missing-number-from-output-in-ansible%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try with below code (it's a simple python code), It worked for me. In below 6 is the limit which I have set to test you can change it as per your requirement.
So by this the numbers which you get from out.stdout.lines
will get removed from limit which you set (6 or any number which you want) and remaining numbers will show.
- name: Getting Missing Numbers
shell: python -c "ma = {{ out.stdout_lines }}; num = [int(i) for i in ma[0].split()]; mn = list(set(range(1, 6 + 1)) - set(num)); print(mn)"
register: mynu
- debug: msg={{mynu.stdout}}
Thanks a Lot, That is exactly what I needed.
– user10373379
Nov 13 '18 at 12:13
add a comment |
Try with below code (it's a simple python code), It worked for me. In below 6 is the limit which I have set to test you can change it as per your requirement.
So by this the numbers which you get from out.stdout.lines
will get removed from limit which you set (6 or any number which you want) and remaining numbers will show.
- name: Getting Missing Numbers
shell: python -c "ma = {{ out.stdout_lines }}; num = [int(i) for i in ma[0].split()]; mn = list(set(range(1, 6 + 1)) - set(num)); print(mn)"
register: mynu
- debug: msg={{mynu.stdout}}
Thanks a Lot, That is exactly what I needed.
– user10373379
Nov 13 '18 at 12:13
add a comment |
Try with below code (it's a simple python code), It worked for me. In below 6 is the limit which I have set to test you can change it as per your requirement.
So by this the numbers which you get from out.stdout.lines
will get removed from limit which you set (6 or any number which you want) and remaining numbers will show.
- name: Getting Missing Numbers
shell: python -c "ma = {{ out.stdout_lines }}; num = [int(i) for i in ma[0].split()]; mn = list(set(range(1, 6 + 1)) - set(num)); print(mn)"
register: mynu
- debug: msg={{mynu.stdout}}
Try with below code (it's a simple python code), It worked for me. In below 6 is the limit which I have set to test you can change it as per your requirement.
So by this the numbers which you get from out.stdout.lines
will get removed from limit which you set (6 or any number which you want) and remaining numbers will show.
- name: Getting Missing Numbers
shell: python -c "ma = {{ out.stdout_lines }}; num = [int(i) for i in ma[0].split()]; mn = list(set(range(1, 6 + 1)) - set(num)); print(mn)"
register: mynu
- debug: msg={{mynu.stdout}}
answered Nov 13 '18 at 11:38
Jaydeep ChaudhariJaydeep Chaudhari
10010
10010
Thanks a Lot, That is exactly what I needed.
– user10373379
Nov 13 '18 at 12:13
add a comment |
Thanks a Lot, That is exactly what I needed.
– user10373379
Nov 13 '18 at 12:13
Thanks a Lot, That is exactly what I needed.
– user10373379
Nov 13 '18 at 12:13
Thanks a Lot, That is exactly what I needed.
– user10373379
Nov 13 '18 at 12:13
add a comment |
You're printing the stdout from some command but I'll assume you know the expected result and have that stored in a variable somewhere. For this I'd use the difference
filter.
- hosts: localhost
vars:
sample_out_ml: [ 1, 2, 3, 4 ]
sample_out_sl: "1 2 3 4"
expected: [ 1, 2, 3 ,4 ,5 ]
tasks:
- name: Show difference when cmd outputs multi-line
debug:
msg: "{{ expected | difference(sample_out_ml) }}"
- name: Show difference when cmd outputs on single line
debug:
msg: "{{ expected | difference(sample_out_sl.split(' ') | map('int') ) }}"
EDIT Based on your comments I provided a more robust example of how you might do this. If you're going all in on Ansible you should try and refrain from using the command
modules. Hope this helps.
It prints"msg": "Hello world!"
not the missing numbers
– user10373379
Nov 13 '18 at 6:00
I may have missed a quote or two. I updated my answer . What version of Ansible are you running?
– Petro026
Nov 13 '18 at 6:05
To Test this I have addedallnu: [ 1, 2, 3, 4, 5 ]
so the missing number should be5
instead it shows"msg": [ 1, 2, 3, 4, 5 ]
Ansible version is2.7.0
– user10373379
Nov 13 '18 at 7:14
It will not work, To verify this I have tried thismsg: "{{ allnu | difference([1, 2, 3, 4]) }}"
And I get exact missing number i.e5
. However my output like"1 2 3 4"
it's not a list to work with your way.
– user10373379
Nov 13 '18 at 7:56
add a comment |
You're printing the stdout from some command but I'll assume you know the expected result and have that stored in a variable somewhere. For this I'd use the difference
filter.
- hosts: localhost
vars:
sample_out_ml: [ 1, 2, 3, 4 ]
sample_out_sl: "1 2 3 4"
expected: [ 1, 2, 3 ,4 ,5 ]
tasks:
- name: Show difference when cmd outputs multi-line
debug:
msg: "{{ expected | difference(sample_out_ml) }}"
- name: Show difference when cmd outputs on single line
debug:
msg: "{{ expected | difference(sample_out_sl.split(' ') | map('int') ) }}"
EDIT Based on your comments I provided a more robust example of how you might do this. If you're going all in on Ansible you should try and refrain from using the command
modules. Hope this helps.
It prints"msg": "Hello world!"
not the missing numbers
– user10373379
Nov 13 '18 at 6:00
I may have missed a quote or two. I updated my answer . What version of Ansible are you running?
– Petro026
Nov 13 '18 at 6:05
To Test this I have addedallnu: [ 1, 2, 3, 4, 5 ]
so the missing number should be5
instead it shows"msg": [ 1, 2, 3, 4, 5 ]
Ansible version is2.7.0
– user10373379
Nov 13 '18 at 7:14
It will not work, To verify this I have tried thismsg: "{{ allnu | difference([1, 2, 3, 4]) }}"
And I get exact missing number i.e5
. However my output like"1 2 3 4"
it's not a list to work with your way.
– user10373379
Nov 13 '18 at 7:56
add a comment |
You're printing the stdout from some command but I'll assume you know the expected result and have that stored in a variable somewhere. For this I'd use the difference
filter.
- hosts: localhost
vars:
sample_out_ml: [ 1, 2, 3, 4 ]
sample_out_sl: "1 2 3 4"
expected: [ 1, 2, 3 ,4 ,5 ]
tasks:
- name: Show difference when cmd outputs multi-line
debug:
msg: "{{ expected | difference(sample_out_ml) }}"
- name: Show difference when cmd outputs on single line
debug:
msg: "{{ expected | difference(sample_out_sl.split(' ') | map('int') ) }}"
EDIT Based on your comments I provided a more robust example of how you might do this. If you're going all in on Ansible you should try and refrain from using the command
modules. Hope this helps.
You're printing the stdout from some command but I'll assume you know the expected result and have that stored in a variable somewhere. For this I'd use the difference
filter.
- hosts: localhost
vars:
sample_out_ml: [ 1, 2, 3, 4 ]
sample_out_sl: "1 2 3 4"
expected: [ 1, 2, 3 ,4 ,5 ]
tasks:
- name: Show difference when cmd outputs multi-line
debug:
msg: "{{ expected | difference(sample_out_ml) }}"
- name: Show difference when cmd outputs on single line
debug:
msg: "{{ expected | difference(sample_out_sl.split(' ') | map('int') ) }}"
EDIT Based on your comments I provided a more robust example of how you might do this. If you're going all in on Ansible you should try and refrain from using the command
modules. Hope this helps.
edited Nov 13 '18 at 16:45
answered Nov 13 '18 at 4:50
Petro026Petro026
92946
92946
It prints"msg": "Hello world!"
not the missing numbers
– user10373379
Nov 13 '18 at 6:00
I may have missed a quote or two. I updated my answer . What version of Ansible are you running?
– Petro026
Nov 13 '18 at 6:05
To Test this I have addedallnu: [ 1, 2, 3, 4, 5 ]
so the missing number should be5
instead it shows"msg": [ 1, 2, 3, 4, 5 ]
Ansible version is2.7.0
– user10373379
Nov 13 '18 at 7:14
It will not work, To verify this I have tried thismsg: "{{ allnu | difference([1, 2, 3, 4]) }}"
And I get exact missing number i.e5
. However my output like"1 2 3 4"
it's not a list to work with your way.
– user10373379
Nov 13 '18 at 7:56
add a comment |
It prints"msg": "Hello world!"
not the missing numbers
– user10373379
Nov 13 '18 at 6:00
I may have missed a quote or two. I updated my answer . What version of Ansible are you running?
– Petro026
Nov 13 '18 at 6:05
To Test this I have addedallnu: [ 1, 2, 3, 4, 5 ]
so the missing number should be5
instead it shows"msg": [ 1, 2, 3, 4, 5 ]
Ansible version is2.7.0
– user10373379
Nov 13 '18 at 7:14
It will not work, To verify this I have tried thismsg: "{{ allnu | difference([1, 2, 3, 4]) }}"
And I get exact missing number i.e5
. However my output like"1 2 3 4"
it's not a list to work with your way.
– user10373379
Nov 13 '18 at 7:56
It prints
"msg": "Hello world!"
not the missing numbers– user10373379
Nov 13 '18 at 6:00
It prints
"msg": "Hello world!"
not the missing numbers– user10373379
Nov 13 '18 at 6:00
I may have missed a quote or two. I updated my answer . What version of Ansible are you running?
– Petro026
Nov 13 '18 at 6:05
I may have missed a quote or two. I updated my answer . What version of Ansible are you running?
– Petro026
Nov 13 '18 at 6:05
To Test this I have added
allnu: [ 1, 2, 3, 4, 5 ]
so the missing number should be 5
instead it shows "msg": [ 1, 2, 3, 4, 5 ]
Ansible version is 2.7.0
– user10373379
Nov 13 '18 at 7:14
To Test this I have added
allnu: [ 1, 2, 3, 4, 5 ]
so the missing number should be 5
instead it shows "msg": [ 1, 2, 3, 4, 5 ]
Ansible version is 2.7.0
– user10373379
Nov 13 '18 at 7:14
It will not work, To verify this I have tried this
msg: "{{ allnu | difference([1, 2, 3, 4]) }}"
And I get exact missing number i.e 5
. However my output like "1 2 3 4"
it's not a list to work with your way.– user10373379
Nov 13 '18 at 7:56
It will not work, To verify this I have tried this
msg: "{{ allnu | difference([1, 2, 3, 4]) }}"
And I get exact missing number i.e 5
. However my output like "1 2 3 4"
it's not a list to work with your way.– user10373379
Nov 13 '18 at 7:56
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53273690%2fhow-to-find-missing-number-from-output-in-ansible%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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