This is an ansible script that I was expecting to print out the same random number three times. Instead, it prints out three random numbers. How do I assign a random number to a variable in ansible so that it is fixed throughout the playbook?
---
- name: Test random filter
hosts: localhost
gather_facts: False
vars:
random_number: "{{ 100 | random }}"
tasks:
- name: Print the random number
debug: var=random_number
- name: Print the random number
debug: var=random_number
- name: Print the random number
debug: var=random_number
Just use the set_fact module as a task first:
- set_fact:
r: "{{ 100 | random }}"
run_once: yes
Subsequently, debug: msg=... has the value of r fixed.
Set facts under task:
---
- name: Test random filter
hosts: localhost
gather_facts: False
tasks:
- name: set fact here
set_fact:
randome_number: "{{ 100 | random }}"
run_once: yes
- name: Print the random number
debug: var=random_number
- name: Print the random number
debug: var=random_number
- name: Print the random number
debug: var=random_number
Related
I'm trying to get the output of my Ansible script using register module but the loop I'm using is probably causing some issue.
Whats a default way of using register module with loop?
Code:
---
- name:
hosts:
gather_facts:
tasks:
- name: Execute file
ansible.builtin.shell:
environment:
"setting environment"
register: output
loop:
"value"
- debug:
vars: output.std_lines
Whats a default way of using register module with loop?
It is just registering the result.
The only difference will be, that a single task will provide you just with an dictionary result (or output in your example) and registering in a loop will provide with a list result.results (or output.results in your example). To access the .stdout_lines you will need to loop over the result set .results too.
You may have a look into the following example playbook which will show some aspects of Registering variables, Loops, data structures, dicts and lists and type debugging.
---
- hosts: localhost
become: false
gather_facts: false
tasks:
- name: Create STDOUT output (single)
command: 'echo "1"'
register: result
- name: Show full result (single)
debug:
var: result
- name: Show '.stdout' (single)
debug:
msg: "The result in '.stdout': {{ result.stdout }} is of type {{ result.stdout | type_debug }}"
- name: Create STDOUT output (loop)
command: 'echo "{{ item }}"'
register: result
loop: [1, 2, 3]
loop_control:
label: "{{ item }}"
- name: Show full result (loop)
debug:
var: result
- name: Show '.stdout' (loop)
debug:
msg: "The result in '.stdout': {{ item.stdout }} is of type {{ item.stdout | type_debug }}"
loop: "{{ result.results }}"
loop_control:
label: "{{ item.item }}"
By running it and going through the output you can get familiar with the differences in your tasks.
Further Q&A
Register Variables in Loop in an Ansible Playbook
Ansible: loop, register, and stdout
Register variables in loop in Ansible playbook
... and many more here on SO.
input:
- tests_to_run
tasks:
modify_user_input:
< Remove the empty elements from tests_to_run>
Have been searching for hours. Does anyone know a way to filter the input, so I can manipulate the tests_to_run list, so it does not have any empty elements.
Sample input and output
Sample tests_to_run = ['test_a','','test_b','test_c','yrls']
Expected variable to be used in later tasks test_to_run_clean = ['test_a','test_b','test_c','yrls']
To remove the empty element from the list:
---
- name: Sample playbook
connection: local
gather_facts: false
hosts: localhost
vars:
mylist:
- abc
- def
-
- jkl
tasks:
- set_fact:
mylist: "{{ mylist|select()|list }}"
I have a set of three numbers: 19, 20 and 21 and I need to get one of them except the one of the input. My approch is to use the jinja-difference filter, which works fine, but I don't know how I can replace the hardcoded number (in my example "19") with an variable (in my example "hostnumber") in the difference-filer.
Example:
ansible-playbook get_other_host.yml -e "hostname=abcd0015"
---
- hosts: all
gather_facts: false
tasks:
- name: Extract the hostnumber
set_fact:
hostnumber: "{{ (hostname[6:] | int) + 4 }}"
# Output is: 19
- name: The hostnumber is
debug:
msg: "{{ hostnumber }}"
# Output is: 20 and 21
- name: The other hosts are
debug:
msg: "{{ [19,20,21] | difference([19]) }}"
# Output is: 20 or 21
- name: One other host is
debug:
msg: "{{ [19,20,21] | difference([19]) | random }}"
Jinja2 has access to all the vars, including the hostnumber that was previously set; it may already be an int, but using the | int on a variable that is already int is harmless
- debug:
msg: "{{ [19,20,21] | difference([hostnumber | int]) }}"
I am new to ansible, trying to achieve following:
Store the output in an array,and iterate over the array " COUNT" times, where count is total number of elements minus one in the array/list.
Below is my example playbook.
---
- name: GETTING INTERFACES
connection: network_cli
cli_command:
command: show interfaces terse | match ge-
register: A
- name: LISTING CONTENTS of LIST
debug:
var: A.stdout_lines
- name: COUNTING ELEMENTS
set_fact:
COUNT: "{{ (A.stdout_lines|length)-1 }}"
- name: DISPLAY
debug:
var: COUNT
- name: ITERATING OVER LIST
debug: var=item
loop: MUST LOOP OVER THE LIST "COUNT" TIMES How can I achieve this?
Thanks and have a good weekend!!
Q: "Iterate over the array COUNT times, where COUNT is the total number of elements in the array minus one."
A: It's possible to use nested. The playbook below
shell> cat pb.yml
- hosts: localhost
vars:
A:
stdout_lines:
- line1
- line2
- line3
tasks:
- debug:
msg: "{{ item.0 }} {{ item.1 }}"
with_nested:
- "{{ query('sequence', params) }}"
- "{{ A.stdout_lines }}"
vars:
count: "{{ A.stdout_lines|length - 2 }}"
params: "{{ 'start=0 end=' ~ count }}"
gives
shell> ansible-playbook pb.yml | grep msg\":
"msg": "0 line1"
"msg": "0 line2"
"msg": "0 line3"
"msg": "1 line1"
"msg": "1 line2"
"msg": "1 line3"
I have a dynamic inventory which returns me my hosts addresses.
But sometimes, I would like to apply some configuration to a limited number of hosts.
A sample with N hosts but only 5 are echoed:
- name: "Run silly shell script"
shell: |
echo {{ item }}
exit 0
with_items: "{{ hosts | only(5) }}"
to get the first X elements from a list, use: list_var[:X].
full example as PB below:
---
- hosts: localhost
gather_facts: false
vars:
list_var:
- 1
- 2
- 3
- 4
- 5
- 6
tasks:
- name: print full list
debug:
var: list_var
- name: print list of first 3 elements
debug:
var: list_var[:3]
hope it helps.