ansible-facts

ansible ssh to json_query response values in loop

跟風遠走 提交于 2019-12-11 16:58:22
问题 Team, I have response from json_query which is a dict key:value and i would like to iterate over all values and run ssh command for each value Below gets me list of all nodes - name: "Fetch all nodes from clusters using K8s facts" k8s_facts: kubeconfig: $WORKSPACE kind: Node verify_ssl: no register: node_list - debug: var: node_list | json_query(query) vars: query: 'resources[].{node_name: metadata.name, nodeType: metadata.labels.nodeType}' TASK [3_validations_on_ssh : debug] ok: [target1] =>

How retrieve the name of specific dictionary - Ansible

对着背影说爱祢 提交于 2019-12-11 05:34:27
问题 I need help to retrieve only the name of the Virtual Machines of my ESXI Host. - name: VM-FACTS vmware_vm_facts: hostname: "{{ ansible_hostname }}" username: "{{ ansible_user }}" password: "{{ ansible_password }}" validate_certs: False delegate_to: localhost register: vmfacts - name: Debug debug: var: vmfacts.virtual_machines Output of the variable: ok: [xxxxx] => { "vmfacts.virtual_machines": { "001-MV-Test": { "cluster": null, "esxi_hostname": "xxxxx", "guest_fullname": "Red Hat Enterprise

Use Ansible control master's IP address in Jinja template

心已入冬 提交于 2019-12-10 17:55:24
问题 I would like to insert an IP address in to a J2 template which is used by an Ansible playbook. That IP adress is not the address of the host which is being provisioned, but the IP of the host from which the provisioning is done. Everything I have found so far covers using variables/facts related to the hosts being provisioned. In other words: the IP I’d like to insert is the one in ['ansible_default_ipv4']['address'] when executing ansible -m setup 127.0.0.1 . I think that I could use a local

How to switch Ansible playbook to another host when calling second playbook

ⅰ亾dé卋堺 提交于 2019-12-08 08:20:26
问题 I have two playbooks - my first playbook iterates on the list of ESXi servers getting list of all VMs, and then passes that list to the second playbook, that should iterates on the IPs of the VMs. Instead it is still trying to execute on the last ESXi server. I have to switch host to that VM IP that I'm currently passing to the second playbook. Don't know how to switch... Anybody? First playbook: - name: get VM list from ESXi hosts: all tasks: - name: get facts vmware_vm_facts: hostname: "{{

Best way to get the IP address of the Ansible control machine

寵の児 提交于 2019-12-07 02:50:04
问题 I am using Ansible and ufw to setup a firewall on my servers. As part of the ufw rules I would like to allow SSH from the Ansible control machine, but not from anywhere else. My question is - what is the best way to get the IP address of the control machine itself so I can put it into the rule? I'm aware that I can use facts to get the IP address of the machine I am running the playbook on, but I don't see any easy way to get it automatically for the machine that is running ansible. I'd like

Ansible iterate over hosts in inventory group set by variable

怎甘沉沦 提交于 2019-12-04 06:48:57
问题 I have the next snippet in my role template: upstream portal { {% set nodes = groups["my_dev_cluster"] %} {% for node in nodes %} ...do something with nodes... {% endfor %} } And it works well. But when I try to parametrize inventory group name like this: upstream portal { {% set nodes = groups["{{cluster_name}}"] %} {% for node in nodes %} ...do something with nodes... {% endfor %} } I get an exception like: FAILED! => {"changed": false, "failed": true, "msg": "AnsibleUndefinedVariable:

Ansible - actions BEFORE gathering facts

时间秒杀一切 提交于 2019-12-03 04:04:46
问题 Does anyone know how to do something (like wait for port / boot of the managed node) BEFORE gathering facts? I know I can turn gathering facts off gather_facts: no and THEN wait for port but what if I need the facts while also still need to wait until the node boots up? 回答1: Gathering facts is equivalent to running the setup module. You can manually gather facts by running it. It's not documented, but simply add a task like this: - name: Gathering facts setup: In combination with gather_facts

Ansible: get current target host's IP address

我的未来我决定 提交于 2019-12-02 17:02:46
How do you get the current host's IP address in a role? I know you can get the list of groups the host is a member of and the hostname of the host but I am unable to find a solution to getting the IP address. You can get the hostname by using {{inventory_hostname}} and the group by using {{group_names}} I have tried things like {{ hostvars[{{ inventory_hostname }}]['ansible_ssh_host'] }} and ip="{{ hostvars.{{ inventory_hostname }}.ansible_ssh_host }}" A list of all addresses is stored in a fact ansible_all_ipv4_addresses , a default address in ansible_default_ipv4.address . --- - hosts:

Ansible iterate over hosts in inventory group set by variable

风格不统一 提交于 2019-12-02 10:34:06
I have the next snippet in my role template: upstream portal { {% set nodes = groups["my_dev_cluster"] %} {% for node in nodes %} ...do something with nodes... {% endfor %} } And it works well. But when I try to parametrize inventory group name like this: upstream portal { {% set nodes = groups["{{cluster_name}}"] %} {% for node in nodes %} ...do something with nodes... {% endfor %} } I get an exception like: FAILED! => {"changed": false, "failed": true, "msg": "AnsibleUndefinedVariable: 'dict object' has no attribute '{{cluster_name}}'"} Here, cluster_name - is a simple string variable

Ansible - use fact from local host in remote host template

与世无争的帅哥 提交于 2019-12-01 03:37:23
问题 I have a playbook that contains roles for localhost and roles for remote hosts. In one of the localhost roles I set a fact called git_tag . I want to use this fact in a template for the remote hosts. I tried: - name: Read Version set_fact: git_tag: "{{ package_json.stdout | from_json | json_query('version')}}" delegate_to: "test-server" But when Ansible reaches the role that reads the template that has {{ git_tag }} it says that git_tag is undefined . I'm sure I'm doing something wrong. How