Reputation: 11
I am trying to generate random value with in the range of 1-255 and should not be in available value. that is Stand by group id of cisco switch.
- name: Filter avilable Group ID
shell: cat "standby_precheck.txt" | awk '{print $2}' | sed -e '1,3d'
register: avil_grp_ids
- name: Pick free standby group id
set_fact:
standby_grp_id: "{{ 255 | random }}"
until: "standby_grp_id not in {{ avil_grp_ids.stdout_lines }}"
retries: 30
some times picking value not in list some times not, I have tried by increasing retries value up to 1000 still it is not working some times. used delegate_to: local also please have a look how my output as fallows. please help me to resolve this issue guys, please suggest me if any other methods also... Thanks in Advance :)
TASK [Pick free standby group id] *************************************************************************************************************************** ok: [localhost] => {"ansible_facts": {"retries": 1000, "standby_grp_id": "176", "until": "standby_grp_id not in ['', 'Grp', '12', '1', '81', '82', '98', '181', '212', '128', '130', '132', '134', '135', '152', '154', '156', '158', '176', '178', '184', '185', '186', '189', '190', '198', '200', '204', '137', '217', '215', '26', '4', '5', '7', '79', '241', '17', '83', '29', '30', '32', '34', '36', '38', '40', '42', '43', '44', '48', '50', '51', '53', '64', '66', '68', '70', '72', '74', '76', '193', '94', '96', '197', '99', '120', '230', '43', '33', '224', '244', '246', '248', '249', '231', '23', '25', '56', '58', '60', '65', '63', '146', '14', '16', '252', '2', '6', '10', '62', '28', '233', '35', '235', '236', '237', '46', '41', '143', '52', '71', '201', '73', '75', '77', '78', '18', '28', '86', '88', '92', '93', '194', '170', '91', '19', '242', '221', '202', '192', '112', '113', '114', '214', '115', '216', '116', '117', '118', '218', '119', '195', '196', '122', '123', '206', '208', '213', '220', '22', '129', '254', '167', '163', '162', '169', '219', '140', '142', '144', '121', '148', '21', '232', '234', '31', '55']"}, "changed": false}
Upvotes: 0
Views: 648
Reputation: 11
I got solution for this thank you.
- name: Filter avilable Group ID
shell: cat "standby_precheck.txt" | awk '{print $2}' | sed -e '1,/Grp/ d'
register: avil_grp_ids
- name: run shell to get random number
shell: echo `shuf -i 1-255 -n 1`
register: standby_grp_id
failed_when: standby_grp_id.rc > 255
until: standby_grp_id.stdout not in avil_grp_ids.stdout_lines
retries: 255
delay: 1
This is working perfectly.. Thank you :)
Upvotes: 0
Reputation: 2298
Picking a random number multiple times does not guarantee that all values will be tried.
Instead, you want to difference between the set of possible ids and the set of available ones:
range(1,255) | difference(avil_grp_ids.stdout_lines)
Keep in mind you might have to convert some values to make sure the types match
Upvotes: 3