Which magic variables are most useful?
IntermediateConceptVariables, Facts and Precedence
Answer
inventory_hostname,inventory_hostname_shorthostvars— all variables of every host in the inventorygroups— dict of group name to list of hostsgroup_names— groups the current host is inansible_play_hosts,ansible_play_batch— hosts in the play / current serial batchansible_check_mode,ansible_diff_mode,ansible_versionplaybook_dir,role_path,ansible_run_tags
- debug: msg="{{ groups['db'] | length }} database hosts"
More on Variables, Facts and Precedence
Where can you define variables in Ansible?BeginnerHow do you reference a variable, and when must you quote it?BeginnerGive a summary of variable precedence.IntermediateWhat are the lowest and highest precedence sources, and why does that matter?IntermediateWhat is the difference between a role's `defaults/` and `vars/`?IntermediateWhat are facts and how are they gathered?Beginner