How can I access ansible facts of another server? - ansible

I am trying to access Ansible facts for a different host than my target host. Following is my sample playbook. But facts are not collected for util server when I run the below play. Can someone help me, how can I access facts of a different server?
---
- hosts: build
gather_facts: yes
vars:
dist: "{{ hostvars['util']['ansible_facts']['distribution'] }}"
tasks:
- name: Demo Magic variables
debug:
var: "{{ dist }}"

Simply gather the needed facts from your desired server prior to using them. The simplest solution:
---
- name: Gather facts from util server
hosts: util
- name: Do our job
hosts: build
vars:
dist: "{{ hostvars['util']['ansible_facts']['distribution'] }}"
tasks:
- name: Demo Magic variables
debug:
var: "{{ dist }}"

Related

Access variables at play level

I use localhost and set_fact to store variables and access them in different playbooks.
---
- hosts: localhost
connection: local
gather_facts: False
tasks:
- name: set_variables
set_fact:
cloudinit_fqdn: 'server1.example.com'
additional_container_config_values:
security.nesting: 'false'
security.privileged: 'false'
cloudinit_network_raw:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: False
addresses: [192.168.178.35/24]
gateway4: 192.168.178.1
nameservers:
addresses: [192.168.178.13]
Now I want to use the cloudinit_fqdn at import_playbook:
- name: system configuration
import_playbook: "{{ hostvars['localhost']['cloudinit_fqdn'] }}_server_config.yml"
I tried different ways to get that variable, but I get errors like:
'ERROR! 'hostvars' is undefined'
I am not able to get access to that variable by:
- debug:
msg: '{{ vars }}'
ERROR! 'debug' is not a valid attribute for a Play
How can I use a variable at play-level?
Regarding your use case I've setup a short test to come around the syntax errors of the variable, as well the debug task.
---
- hosts: localhost
become: false
gather_facts: false
tasks:
- name: Set variables
set_fact:
example_fqdn: 'test.example.com'
- name: Show variables
debug:
msg: "{{ hostvars['localhost'].example_fqdn }}"
While the example is working, adding
- name: Import playbook
import_playbook: "{{ hostvars['localhost'].example_fqdn }}.yml"
or even a simple
- name: Import playbook
import_playbook: "{{ example_fqdn }}.yml"
let the playbook run fail with
ERROR! 'hostvars' is undefined
ERROR! 'example_fqdn' is undefined
since the import is done during compile time, whereby the variable will be defined during runtime. Even not possible is
- name: Import playbook
import_playbook: "{{ to_import }}.yml"
vars:
to_import: "{{ example_fqdn }}"
as the import is static, not dynamic. Importing playbooks and Re-using playbooks seems not working in that way.
What is actually working is
- name: Import playbook
import_playbook: test.example.com.yml
Furher Questions and Answers
Ansible: import_playbook fails with variable undefined error
Ansible: Skip import_playbook with variable definition
What's the difference between include_tasks and import_tasks

How to run a task only once during entire Ansible Playbook?

I have a Ansible playbook which does multiple things as below -
Download artifacts fron nexus into local server (Ansible Master).
Copy those artifacts onto multiple remote machines let's say server1/2/3 etc..
And I have used roles in my playbook and the role (repodownload) which downloads the artifacts I want to run it only once because why would i want to download the same thing again. I have tried to use run_once: true but i guess that won't work because that only works for one playbook run but my playbook is running multiple times for multiple hosts.
---
- name: Deploy my Application to tomcat nodes
hosts: '{{ target_env }}'
serial: 1
roles:
- role: repodownload
tags:
- repodownload
- role: copyrepo
tags:
- copyrepo
- role: stoptomcat
tags:
- stoptomcat
- role: deploy
tags:
- deploy
Here target_env is being passed from the command line and it's the remote host group.
Any help is appreciated.
Below is the code from main.yml from repodownload role -
- connection: local
name: Downloading files from Nexus to local server
get_url: url="{{ nexus_url }}/{{item}}/{{ myvm_release_version }}/{{item}}-{{ release_ver }}.war" dest={{ local_server_location }}
with_items:
- "{{ temps }}"
This is a really simple one that I battled with too.
Try this:
- connection: local
name: Downloading files from Nexus to local server
get_url:
url: "{{ nexus_url }}/{{item}}/{{ myvm_release_version }}/{{item}}-{{ release_ver }}.war"
dest: "{{ local_server_location }}"
with_items:
- "{{ temps }}"
run_once: true
Just something else, unrelated to your main question;
When you run a module that has really long args, like in your example above, rather break the params into their own lines nested under the module. It makes for easier reading, and it makes it easier to spot any potential typos or syntax errors early.
Okay extending from your converstation with Zeitounator. The following workaround will work without changing your vars files. Just remember that this is a workaround, might not be the most efficient way to do the job.
---
- name: Download my repo to localhost
# Executes only for first host in target_env and has visibility to group vars of target_env
hosts: '{{ target_env }}[0]'
serial: 1
roles:
- role: repodownload
tags:
- repodownload
- name: Deploy my Application to tomcat nodes
# Executes for all hosts in target_env
hosts: '{{ target_env }}'
serial: 1
roles:
- role: copyrepo
tags:
- copyrepo
- role: stoptomcat
tags:
- stoptomcat
- role: deploy
tags:
- deploy

Copy file from one remote server to another remote server using nested delegate_to

As a user I want to copy file from node1 to node2. Is it possible with copy module + delegate_to
Below is what I was trying to do. Playbook is running from node3.
Playbook Sample
---
- name: Gather Facts for all hosts
hosts: all
gather_facts: true
any_errors_fatal: true
become: true
- name: Test
hosts: all
gather_facts: false
any_errors_fatal: true
become: true
roles:
- role: test
Role Sample
---
- block:
- include_tasks: test.yml
any_errors_fatal: true
run_once: true
Task Sample
---
- name: Test
block:
- name: Transfer files from node1 to node2
copy:
src: /tmp/a
dest: /tmp/
delegate_to: node2
delegate_to: node1
You can use synchronize module only when rsync is enabled either in source server (kube master in your case) or in the kube nodes.
Method 1: to push from master, need rsync enabled in master
Synchronize use push mode by default
- hosts: nodes
tasks:
- name: Transfer file from master to nodes
synchronize:
src: /src/path/to/file
dest: /dest/path/to/file
delegate_to: "{{ master }}"
Method 2: to use fetch and copy modules
- hosts: all
tasks:
- name: Fetch the file from the master to ansible
run_once: yes
fetch: src=/src/path/to/file dest=temp/ flat=yes
when: "{{ ansible_hostname == 'master' }}"
- name: Copy the file from the ansible to nodes
copy: src=temp/file dest=/dest/path/to/file
when: "{{ ansible_hostname != 'master' }}"
Hope this helps.
The short answer is no: you will not be able to do this with the copy module.
You might however want to have a look at the synchronize module
Quoting the doc
The “local host” can be changed to a different host by using delegate_to. This enables copying between two remote hosts or entirely on one remote machine.
You would basically end up with something like:
---
- name: Rsync some files
hosts: my_target_host
tasks:
- name: copy my file
synchronize:
src: path/on/source/host
dest: path/on/dest/host
delegate_to: my_source_host
Edit I just found this article referencing synchronize as well as the fetch/copy method that you might want to look at.

Ansible Dynamic Inventory

I'm running a playbook which houses multiple roles targets multiple hosts
The goal is to deploy a VM and use it's IP to deploy an app.
My playbook, has two roles, using "build_vm" role I'm able to display IP address via debug, yet when passing ipaddr variable to second role, Ansible complains that the variable is not defined
- hosts: linux
become: true
roles:
- build_vm
- tasks:
- debug: msg="{{ ipaddr }}"
- hosts: "{{ ipaddr }}"
roles:
- deploy_app
I have used set_fact with and ran into same issue, I wonder what I should be using here? dynamic inventory? I have searched sparse docs online and I'm unable to find an intuitive example to follow.
There are many ways to using add_host. In this example, I am adding the new host to a group and using it in a later play.
- hosts: linux
become: true
roles:
- build_vm
- tasks:
- debug: msg="{{ ipaddr }}"
- name: Add ipaddr to host inventory
add_host: name="{{ ipaddr }}" group=NewHostGroup
- hosts: NewHostGroup
roles:
- deploy_app

Ansible - Get Facts from Remote Windows Hosts

I am using Ansible / Ansible Tower and would like to determine what facts are available on my Windows host. The documentation states that I can run the following:
ansible hostname -m setup
How would I incorporate this into a playbook I run from Tower so I can gather the information from hosts?
Here is the current Playbook per the assistance given:
# This play outputs the facts of the Windows targets in scope
- name: Gather Windows Facts
hosts: "{{ target }}"
gather_facts: yes
tasks:
- setup:
register: ansible_facts
- debug: item
with_dict: ansible_facts
However, running this produces the following error:
ERROR! this task 'debug' has extra params, which is only allowed in
the following modules: command, shell, script, include, include_vars,
add_host, group_by, set_fact, raw, meta
Use gather_facts which is true by default. It is equivalent to running setup module.
- hosts: ....
gather_facts: yes
The facts are saved in ansible variables to be used in playbooks. See System Facts
There are many ways to display the ansible facts. For you to understand how it works, try the following:
- hosts: 127.0.0.1
gather_facts: true
tasks:
- setup:
register: ansible_facts
- debug: item
with_dict: ansible_facts
Testing and working through it, this is working for me:
- name: Gather Windows Facts
hosts: "{{ target }}"
tasks:
- debug: var=vars
- debug: var=hostvars[inventory_hostname]

Resources