You want a variable value computed once from a command and reused through the play.
IntermediateScenarioVariables, Facts and Templates
Answer
Situation read → Store results for reuse.
What I'd do →
- ansible.builtin.command: git -C /opt/app rev-parse --short HEAD
register: git_rev
changed_when: false
- ansible.builtin.set_fact:
release: "{{ git_rev.stdout }}"
cacheable: true
Why → register lives for the play on that host; set_fact with cacheable can persist into the fact cache.
Interview tip → set_fact has high precedence and can shadow inventory variables.
More on Variables, Facts and Templates
A variable you set in `group_vars` is being overridden and you do not know where. How do you find the source?IntermediateYou need different values per environment without copying playbooks.BeginnerYou must render an Nginx config with one `server` block per site from a list.IntermediateA template needs the IPs of all hosts in the `db` group, for example for a replication config.IntermediateNothing should be rendered unless a mandatory variable is supplied.BeginnerPer-host values, like `server_id` for MySQL, must be unique and stable.Intermediate