Registers
Capture the output of the play inside the variable
- name: Register Playbook
hosts: proxy
tasks:
- name: Run a command
command: uptime
register: server_uptime
- name: Inspect the server_uptime variable
debug:
var: server_uptime
- name: Show the server uptime
debug:
msg: "{{ server_uptime.stdout }}"
Ansible special variables
The two most important ones are
- ansible playhostall
- ansible playhost This is holds what nodes where successful
Filtering the nodes
-
Shorter one
-
Longer one
{% for host in ansible_play_host_all %} {% if host not in ansible_play_hosts%} {{host}} {% endif %} {% endfor %}
Ansible variables
Varialbe prcencece
The most important is the --extra-vars
Including external variables
Just use var files to import them from the another yml file Keep in mind that vars files loads the variables right at the start of the playbook**.
Dynamic Varaibles
You can also use the include vars module to dynamically load your variables in your playbook.
- name: Working with variables
hosts: node1
tasks:
- name: Load the variables
include_vars: myvars.yml
- name: Show 2nd item in port_nums
debug:
msg: SSH port is {{ port_nums[1] }}
-
Creating a list dictionaries
The variables can be listed in the array