Questions tagged [hostvars]
16 questions
1
vote
1 answer
How can I check how many Play Hosts have variable value X in Ansible?
I have the following Ansible problem.
Each host a variable, let's call it is_master, which can either be "0" or "1".
I now want to check if the number of hosts having is_master == "1" is grather than 1.
So I want to do something like
- set_fact:…

Bl Jay
- 11
- 2
1
vote
2 answers
'gather_facts' seems to break 'set_fact' and 'hostvars'
I am using set_fact and hostvars to pass variables between plays within a playbook. My code looks something like this:
- name: Staging play
hosts: localhost
gather_facts: no
vars_prompt:
- name: hostname
prompt: "Enter hostname or…

SaintRough
- 61
- 3
1
vote
1 answer
Extract hostvar list
I'm trying to get secondary ip addresses from hosts in a group and set them to a fact.
my hostvars contain
{
"network_interfaces": [
{
"private_ip_address": "10.224.1.48",
"private_ip_addresses": [
{
"primary":…

barry
- 17
- 1
- 2
- 6
1
vote
2 answers
Ansible: Merge variable values into new variable
Let's say I have an inventory like this:
database:
hosts:
database1:
ansible_host: 192.168.0.125
database2:
ansible_host: 192.168.0.126
database3:
ansible_host: 192.168.0.127
Now I'll have to replace a string in a…

Bert
- 347
- 7
- 25
1
vote
1 answer
Loop Ansible hostvars from inventory in jinjar2 template
I'm new to Ansible and just having a problem with variables in inventory. In the inventory I have different variables which contain MAC or IP addresses. I want to loop through them and, if they are present, process them in a jinjar2 template.
My…

nifu
- 47
- 1
- 6
1
vote
1 answer
Ansible - dynamically access hostvars with current hosts in play during runtime
Using Ansible 2.5.0.
Question: How do I dynamically access the host variables of the current hosts in play, during runtime execution.
Inventory file:
[group_one]
host-1 ansible_host=1.2.3.4
host-2 ansible_host=5.6.7.8
[group_two]
host-3…

Kevin C
- 4,851
- 8
- 30
- 64
0
votes
1 answer
Ansible - pass fact between plays when one host is a variable
I have a playbook that contains two plays.
The first play does some data gathering, creates some facts and finally a dynamic inventory.
The second play uses the dynamic inventory from the first and, tries (but fails) to use some facts from the…

piercjs
- 133
- 8
0
votes
1 answer
how to call a dictionary variable from ansible host_var to ansible playbook
I have an ansible playbook, which has a variable defined in it like this:
- hosts: dev-web
become: yes
vars:
- web_dir: /opt/abc/example.com/xyz
i want the string inside the variable "/opt/abc/example.com/xyz" dynamically get from the…

maheshs
- 3
- 2
0
votes
1 answer
Ansible playbooks to remove multiple servers from multiple nagios monitoring via api keys
I need to remove multiple servers from multiple nagios monitoring via api keys with ansible.
so I tried below solution but not able to get api key section in command and get the success output
example-
hosts file -->
[nagios_server]
10.0.0.1…

G_developer
- 13
- 1
- 4
0
votes
1 answer
Ansible - hostvars from single host
i have in the inventory host with own variables:
children:
webserver:
vars:
hosts:
web01.local:
function: live
location: lca
web02.local:
function: test
…

Kr3el
- 3
- 1
0
votes
0 answers
How to use variables from another play inside a new play in Ansible
following situation. I have 3 plays in total. In the first play I filter a routername and register it in a variable, let's call this variable pe. pe is used in the second play as a host, like this
- hosts: "{{ hostvars['firsthost']['pe']['stdout']…

ibliscious
- 1
- 1
0
votes
0 answers
hostvars on a temp group in a ansible playbook
I am trying to preserve a command output from one dynamic host group to another one but get a variable is undefined error in my ansible playbook.
My playbook is dynamically getting the primary and secondary hosts by running a command. I want to use…

user3164720
- 73
- 1
- 7
0
votes
0 answers
hostvars 'list object' has no attribute
I am trying to get some result from the hostvars , but fails.
playbook:
- debug:
msg: "{{hostvars[item]['a']}}"
run_once: true
delegate_to: localhost
with_items: "{{ groups['all'] }}"
results:
ok: [ansible8] => (item=ansible8) => {
…

sloweriang
- 308
- 4
- 19
0
votes
0 answers
Dynamically setting ansible_network_os from playbook
I'm working on an ansible project for network troubleshooting, and I'm trying to set the ansible_network_os variable automatically. I have an OS-agnostic python script that uses paramiko to run a show ver command on routers, then returns a variable…

Seth R.
- 15
- 7
0
votes
0 answers
Ansible Tower/Awx - Conditionally choose instance group based on host_var
I have 3 instance groups (eg: AA, BB, CC) and one single inventory of hosts called INVENTORY_A. There is a host_var set for all hosts called HOST_GROUP and the value can be AA, BB or CC.
I want to make sure the job is executed on the respective…

Vijesh Kk
- 141
- 2
- 11