'create unique list from multi servers
I want to create an inventory of my park. form this exemple, I'm getting the serveur name and linux's version
in my inventory, there is a group with my servers. (listeServers)
I want to execute this task and finaly, show one complet liste with all of my servers (in only one var)
- hosts: listeServers
gather_facts: no
vars:
bilan: ['inventory']
tasks:
- name: "Unix version"
shell: "echo Server : `uname -n` - `lsb_release -d -s`"
register: unix_version
- name: "add to bilan"
set_fact:
bilan: "{{ bilan + [ unix_version.stdout ] }}"
- debug: var=bilan
at the moment, each server has its own variable. and it gives:
10:15:06 TASK [debug] *******************************************************************
10:15:06 ok: [serveur3_apache] => {
10:15:06 "bilan": [
10:15:06 "inventory",
10:15:06 "Server : server03 - Ubuntu 18.04.1 LTS"
10:15:06 ]
10:15:06 }
10:15:06 ok: [serveur4_middle] => {
10:15:06 "bilan": [
10:15:06 "inventory",
10:15:06 "Server : server04 - Ubuntu 18.04.1 LTS"
10:15:06 ]
10:15:06 }
10:15:06 ok: [serveur5_middle] => {
10:15:06 "bilan": [
10:15:06 "inventory",
10:15:06 "Server : server05 - Ubuntu 18.04.1 LTS"
10:15:06 ]
10:15:06 }
10:15:06 ok: [serveur6_postgres] => {
10:15:06 "bilan": [
10:15:06 "inventory",
10:15:06 "Server : server06 - Ubuntu 18.04.1 LTS"
10:15:06 ]
10:15:06 }
I would like them all to write to the same variable. and the content of this variable is:
"inventoryM",
"Server : server03 - Ubuntu 18.04.1 LTS"
"Server : server04 - Ubuntu 18.04.1 LTS"
"Server : server05 - Ubuntu 18.04.1 LTS"
"Server : server06 - Ubuntu 18.04.1 LTS"
have you an idea ?
Solution 1:[1]
extract the variables from the hostvars. Use ansible_play_hosts
- name: Create list bilan
set_fact:
bilan: "{{ ansible_play_hosts|
map('extract', hostvars, ['unix_version', 'stdout'] }}"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Vladimir Botka |
