Skip to content

Commit cd4dcb2

Browse files
Add example to use host parameters via Ansible (#4037)
Example error if host parameter is unset: fatal: [foreman.example.com]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'ansible.vars.hostvars.HostVarsVars object' has no attribute '_My_Host_Parameter_'. 'ansible.vars.hostvars.HostVarsVars object' has no attribute '_My_Host_Parameter_'\n\nThe error appears to be in '/tmp/d20250922-101358-j1ipdv/project/playbook.yml': line 5, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - name: \"View a host parameter\"\n ^ here\n"} Example REX output: TASK [View a host parameter] *************************************************** ok: [foreman.example.com] => { "msg": "_Value_of_My_Host_Parameter_" } (cherry picked from commit 335131b)
1 parent 6797979 commit cd4dcb2

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

guides/common/modules/proc_running-an-ansible-playbook.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,4 @@ For more information, see xref:executing-a-remote-job_{context}[].
3636
For more information, see the following resources:
3737
** xref:importing-an-ansible-playbook-by-name_{context}[]
3838
** xref:importing-all-available-ansible-playbooks_{context}[]
39+
* xref:host-parameters-in-ansible-playbooks[]
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
:_mod-docs-content-type: REFERENCE
2+
3+
[id="host-parameters-in-ansible-playbooks"]
4+
= Host parameters in Ansible Playbooks
5+
6+
You can use host parameters from {Project} in Ansible Playbooks.
7+
8+
.Example Ansible Playbook to access host parameters
9+
[source, yaml, options="nowrap", subs="+quotes,verbatim,attributes"]
10+
----
11+
---
12+
- name: "Ansible Playbook to access host parameters"
13+
hosts: all
14+
tasks:
15+
- name: "View a host parameter"
16+
ansible.builtin.debug:
17+
msg: "{{ hostvars[inventory_hostname]['_My_Host_Parameter_'] }}"
18+
...
19+
----
20+
21+
Ansible will access the host parameter and show its value.
22+
If you set the host parameter to an ERB template, for example, `<%= @host.capabilities.include?(:snapshots) %>`, {Project} will render it and provide the result to Ansible.
23+
Ansible receives only the rendered value, not the ERB template itself.
24+
25+
.Additional resources
26+
* xref:Host_Parameter_Hierarchy_{context}[]

guides/doc-Managing_Configurations_Ansible/master.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ include::common/assembly_using-ansible-roles.adoc[leveloffset=+1]
1515

1616
include::common/modules/proc_running-an-ansible-playbook.adoc[leveloffset=+1]
1717

18+
include::common/modules/ref_host-parameters-in-ansible-playbooks.adoc[leveloffset=+2]
19+
1820
include::common/assembly_configuring-and-setting-up-remote-jobs.adoc[leveloffset=+1]
1921

2022
include::common/assembly_integrating-awx.adoc[leveloffset=+1]

0 commit comments

Comments
 (0)