summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrian Coca <bcoca@users.noreply.github.com>2024-11-12 22:05:37 +0100
committerGitHub <noreply@github.com>2024-11-12 22:05:37 +0100
commitc734ac2125f502dcbef2af46b1aba201f850ad05 (patch)
tree6d4b5148b86325d788efe2cecc8fe0ae559a7958
parentDeprecated libvirt_lxc_noseclabel config (#84287) (diff)
downloadansible-c734ac2125f502dcbef2af46b1aba201f850ad05.tar.xz
ansible-c734ac2125f502dcbef2af46b1aba201f850ad05.zip
vars/varnames more examles more varied (#84300)
also ref each other in seealso as they will commonly be used toghether
-rw-r--r--lib/ansible/plugins/lookup/varnames.py14
-rw-r--r--lib/ansible/plugins/lookup/vars.py13
2 files changed, 20 insertions, 7 deletions
diff --git a/lib/ansible/plugins/lookup/varnames.py b/lib/ansible/plugins/lookup/varnames.py
index 2163ce7acf..ef6159f390 100644
--- a/lib/ansible/plugins/lookup/varnames.py
+++ b/lib/ansible/plugins/lookup/varnames.py
@@ -13,11 +13,14 @@ DOCUMENTATION = """
_terms:
description: List of Python regex patterns to search for in variable names.
required: True
+ seealso:
+ - plugin_type: lookup
+ plugin: ansible.builtin.vars
"""
EXAMPLES = """
- name: List variables that start with qz_
- ansible.builtin.debug: msg="{{ lookup('ansible.builtin.varnames', '^qz_.+')}}"
+ ansible.builtin.debug: msg="{{ lookup('ansible.builtin.varnames', '^qz_.+') }}"
vars:
qz_1: hello
qz_2: world
@@ -25,13 +28,16 @@ EXAMPLES = """
qz_: "I won't show either"
- name: Show all variables
- ansible.builtin.debug: msg="{{ lookup('ansible.builtin.varnames', '.+')}}"
+ ansible.builtin.debug: msg="{{ lookup('ansible.builtin.varnames', '.+') }}"
- name: Show variables with 'hosts' in their names
- ansible.builtin.debug: msg="{{ lookup('ansible.builtin.varnames', 'hosts')}}"
+ ansible.builtin.debug: msg="{{ q('varnames', 'hosts') }}"
- name: Find several related variables that end specific way
- ansible.builtin.debug: msg="{{ lookup('ansible.builtin.varnames', '.+_zone$', '.+_location$') }}"
+ ansible.builtin.debug: msg="{{ query('ansible.builtin.varnames', '.+_zone$', '.+_location$') }}"
+
+- name: display values from variables found via varnames (note "*" is used to dereference the list to a 'list of arguments')
+ debug: msg="{{ lookup('vars', *lookup('varnames', 'ansible_play_.+')) }}"
"""
diff --git a/lib/ansible/plugins/lookup/vars.py b/lib/ansible/plugins/lookup/vars.py
index 14cac99c36..5e9247c6ae 100644
--- a/lib/ansible/plugins/lookup/vars.py
+++ b/lib/ansible/plugins/lookup/vars.py
@@ -17,6 +17,10 @@ DOCUMENTATION = """
description:
- What to return if a variable is undefined.
- If no default is set, it will result in an error if any of the variables is undefined.
+ seealso:
+ - plugin_type: lookup
+ plugin: ansible.builtin.varnames
+
"""
EXAMPLES = """
@@ -27,20 +31,23 @@ EXAMPLES = """
myvar: ename
- name: Show default empty since i dont have 'variablnotename'
- ansible.builtin.debug: msg="{{ lookup('ansible.builtin.vars', 'variabl' + myvar, default='')}}"
+ ansible.builtin.debug: msg="{{ lookup('ansible.builtin.vars', 'variabl' + myvar, default='') }}"
vars:
variablename: hello
myvar: notename
- name: Produce an error since i dont have 'variablnotename'
- ansible.builtin.debug: msg="{{ lookup('ansible.builtin.vars', 'variabl' + myvar)}}"
+ ansible.builtin.debug: msg="{{ q('vars', 'variabl' + myvar) }}"
ignore_errors: True
vars:
variablename: hello
myvar: notename
- name: find several related variables
- ansible.builtin.debug: msg="{{ lookup('ansible.builtin.vars', 'ansible_play_hosts', 'ansible_play_batch', 'ansible_play_hosts_all') }}"
+ ansible.builtin.debug: msg="{{ query('ansible.builtin.vars', 'ansible_play_hosts', 'ansible_play_batch', 'ansible_play_hosts_all') }}"
+
+- name: show values from variables found via varnames (note "*" is used to dereference the list to a 'list of arguments')
+ debug: msg="{{ q('vars', *q('varnames', 'ansible_play_.+')) }}"
- name: Access nested variables
ansible.builtin.debug: msg="{{ lookup('ansible.builtin.vars', 'variabl' + myvar).sub_var }}"