My intention is to generate a list containing numbers from 1 to 22 and for that I wrote below Ansible script. hosts: localhost gatherfacts: no tasks: - name: Generate sequance set. We have people using ansible in a heterogeneous environment, so trying to rely on either password or passwordlookup simply won't work for us since I either have to run some string of LOCAL commands (problem because of people running ansible on different platforms), or they refer to a file (bad practice to pass around password files). Toyaml and toniceyaml filters use PyYAML library which has a default 80 symbol string length limit. That causes unexpected line break after 80th symbol (if there is a space after 80th symbol) To avoid such behaviour and generate long lines it is possible to use width option. . fixup 'historical' versionadded, skip plugins/loader.py. Fix pluginsbysupport ref link to new plugins/./ location. use:ref: for commonreturnvalues, allow empty versionadded. warnings on missing doc info. add a prefix to randomchoice It was colliding with the target for randomchoice plugin.
Need a random number for your Ansible playbook? But want to be idempotent on subsequent runs? There is an answer!
Let's say you want to register
cron
jobs on a bunch of servers and don't want it to start on the same time. You can use:but this will generate random number during each playbook execution, giving you unnecessary
changed
state for tasks.Update for Ansible 2.3:
As of Ansible version 2.3, it’s also possible to initialize the random number generator from a seed. This way, you can create random-but-idempotent numbers:
'{{ 59 |random(seed=inventory_hostname) }} * * * * root /script/from/cron'
For previous Ansible versions:
But you can craft a pseudo-random number based on any variable/fact you want. For example, you can choose
inventory_hostname
to make this number different between servers but the same on subsequent playbook runs:Pm fastrack pmp v8 keygen. Magic explained:
- we take
inventory_hostname
string (e.g.'myserver'
) - make a
hash
from it ('c3a7a35a28dcce27daad3a7a90caad99b967a904'
) - split it into array of characters (
['c','3','a',..]
)
where every character is a hexadecimal digit - apply
int
filter withbase=16
to every character to convert it to number0.15
([12,3,10,..]
) sum
all numbers (334
)- limit our pseudo-random number by taking the remainder of division
% 60
(34
)
So your cron task may look like:
This will start
myscript.sh
at some random time between 6:00
and 7:59
and this time will be idempotent on subsequent playbook runs.Ansible User Password
pkilldemo output
[will@fedora pkilldemo]$ ansible-playbook pkilldemo.yml |
PLAY [localhost] ************************************************************** |
GATHERING FACTS *************************************************************** |
ok: [127.0.0.1] |
TASK: [generate random process string] **************************************** |
changed: [127.0.0.1] |
TASK: [warn user] ************************************************************* |
[127.0.0.1] |
About to kill processes matching lkSm/8UMTb9bR2ZharCU with signal SIGIO. Hit Return to continue or Ctrl-C then a to abort: |
ok: [127.0.0.1] |
TASK: [show bad effects of pkill -f] ****************************************** |
changed: [127.0.0.1] |
TASK: [works ok without -f] *************************************************** |
changed: [127.0.0.1] |
TASK: [works ok with pgrep -f] ************************************************ |
changed: [127.0.0.1] |
TASK: [this is supposed to fail as we don't handle the error with true] ******* |
failed: [127.0.0.1] => {'changed': true, 'cmd': 'pgrep -f lkSm/8UMTb9bR2ZharCU ', 'delta': '0:00:00.023586', 'end': '2014-05-28 21:00:54.400289', 'rc': 1, 'start': '2014-05-28 21:00:54.376703'} |
..ignoring |
pkilldemo.yml
Ansible Generate Random String In Javascript
- hosts: localhost |
connection: local |
tasks: |
- name: generate random process string |
action: shell openssl rand 15 | base64 |
register: process_string |
- name: warn user |
pause: prompt='About to kill processes matching {{process_string.stdout}} with signal SIGIO. Hit Return to continue or Ctrl-C then a to abort' |
- name: show bad effects of pkill -f |
action: shell pkill -IO -f {{process_string.stdout}} || true |
ignore_errors: True |
- name: works ok without -f |
action: shell pkill -IO {{process_string.stdout}} || true |
- name: works ok with pgrep -f |
action: shell pgrep -f {{process_string.stdout}} || true |
- name: this is supposed to fail as we don't handle the error with true |
action: shell pgrep -f {{process_string.stdout}} |
ignore_errors: True |
Ansible Generate Random Strings
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment