Difference between revisions of "Ansible variables"

From wikieduonline
Jump to navigation Jump to search
 
(24 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
* https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html
 
* https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html
  
Creating a variable:
+
Creating a [[variable]]:
  your_new_variable: your_new_var_value
+
  [[vars:]]
 +
    your_new_variable: your_new_var_value
 +
 
 +
[[Using variables]], just reference it using <code><nowiki>{{ }}</nowiki></code>: <ref>https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#referencing-simple-variables</ref>
 +
<nowiki>{{ your_new_var_value }}</nowiki>
 +
 +
 
 +
 
 +
* <code>[[/group_vars/]]</code> <ref>https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#organizing-host-and-group-variables</ref>
 +
* <code>[[/host_vars/]]</code>
  
  
 
  - name: Execute a command
 
  - name: Execute a command
 
   ansible.builtin.command: "false"
 
   ansible.builtin.command: "false"
   debugger: on_failed
+
   [[debugger:]] [[on_failed]]
 +
 
 +
== Variable attributes ==
 +
<code>add, append, as_integer_ratio, bit_length, capitalize, center, clear, conjugate, copy, count, decode, denominator, difference, difference_update, discard, encode, endswith, expandtabs, extend, find, format, fromhex, fromkeys, get, has_key, hex, imag, index, insert, intersection, intersection_update, isalnum, isalpha, isdecimal, isdigit, isdisjoint, is_integer, islower, isnumeric, isspace, issubset, issuperset, istitle, isupper, items, iteritems, iterkeys, itervalues, join, keys, ljust, lower, lstrip, numerator, partition, pop, popitem, real, remove, replace, reverse, rfind, rindex, rjust, rpartition, rsplit, rstrip, setdefault, sort, split, splitlines, startswith, strip, swapcase, symmetric_difference, symmetric_difference_update, title, translate, union, update, upper, values, viewitems, viewkeys, viewvalues, zfill</code>
 +
 
 +
== Forcing the data type ==
 +
*  https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_filters.html#forcing-the-data-type
 +
- ansible.builtin.debug:
 +
      msg: test
 +
  [[when:]] some_string_value | [[bool]]
 +
 
 +
 
 +
- name: YOUR-VAR
 +
  [[value:]] "{{ [[Ansible lookup plugins|lookup]]('env','YOUR-ENV-NAME') | [[string]] }}"
  
 
== Related ==
 
== Related ==
Line 14: Line 36:
 
* [[Msg: The task includes an option with an undefined variable.]]
 
* [[Msg: The task includes an option with an undefined variable.]]
 
* [[DEBUG]]
 
* [[DEBUG]]
 +
* <code>[[--extra-vars]]</code>
 +
* <code>[[Ansible lookup plugins|lookup]]</code>
 +
* [[Jinja]]
  
 
== See also ==
 
== See also ==
* {{Ansible}}
+
* {{Ansible variables}}
  
 
[[Category:Ansible]]
 
[[Category:Ansible]]

Latest revision as of 12:40, 12 December 2022

Creating a variable:

vars:
   your_new_variable: your_new_var_value

Using variables, just reference it using {{ }}: [1]

{{ your_new_var_value }}



- name: Execute a command
  ansible.builtin.command: "false"
  debugger: on_failed

Variable attributes[edit]

add, append, as_integer_ratio, bit_length, capitalize, center, clear, conjugate, copy, count, decode, denominator, difference, difference_update, discard, encode, endswith, expandtabs, extend, find, format, fromhex, fromkeys, get, has_key, hex, imag, index, insert, intersection, intersection_update, isalnum, isalpha, isdecimal, isdigit, isdisjoint, is_integer, islower, isnumeric, isspace, issubset, issuperset, istitle, isupper, items, iteritems, iterkeys, itervalues, join, keys, ljust, lower, lstrip, numerator, partition, pop, popitem, real, remove, replace, reverse, rfind, rindex, rjust, rpartition, rsplit, rstrip, setdefault, sort, split, splitlines, startswith, strip, swapcase, symmetric_difference, symmetric_difference_update, title, translate, union, update, upper, values, viewitems, viewkeys, viewvalues, zfill

Forcing the data type[edit]

- ansible.builtin.debug:
     msg: test
  when: some_string_value | bool


- name: YOUR-VAR
  value: "{{ lookup('env','YOUR-ENV-NAME') | string }}"

Related[edit]

See also[edit]

  • https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#referencing-simple-variables
  • https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#organizing-host-and-group-variables
  • Advertising: