A play uses a variable that is defined only for some hosts. How do you handle the rest?
BeginnerScenarioVariables, Facts and Templates
Answer
Situation read → Optional variable.
What I'd do →
- ansible.builtin.debug:
msg: "{{ proxy_url | default(omit) }}"
- ansible.builtin.apt: { name: curl }
environment:
http_proxy: "{{ proxy_url | default('') }}"
when: proxy_url is defined
Why → default(omit) drops a module parameter entirely; is defined guards a task.
Interview tip → default('') with empty string does not trigger for variables set to empty; use default(x, true) for that.
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