-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore_host.yaml
57 lines (45 loc) · 1.77 KB
/
store_host.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
---
- name: find Hosts
hosts: localhost
gather_facts: no
vars:
hosts_info: {}
new_hosts_info: {}
tasks:
- name: Read store to host mapping info from CSV file
read_csv:
path: "./roles/version_check/files/store_host.csv"
fieldnames: env,id,store_num,host
delimiter: ','
register: store_host
- set_fact:
store_ids_list: "{{ store_ids.split(',') }}"
- name: create host_info dict store_no as key and hostname as value
set_fact:
hosts_info: "{{ hosts_info | combine({ item : store_host.list | selectattr('env','==',ENV) | selectattr('store_num','==',item) | map(attribute='host')|list|first|default() }) }}"
loop: "{{ store_ids_list }}"
- debug: var=hosts_info
- name: Find store_ids which don't have any matching hosts
set_fact:
emptyhost_store_ids: "{{ hosts_info | dict2items | selectattr('value', 'equalto', '') | map(attribute='key') | list }}"
- debug: var=emptyhost_store_ids
- fail:
msg: "the store ids {{ emptyhost_store_ids }} doesn't have matching host"
when: emptyhost_store_ids | length > 0
- name: Transform dictionary host as key and store_ids as values
set_fact:
new_hosts_info: "{{ new_hosts_info | combine({item.value: (new_hosts_info[item.value] + ',' + item.key) if new_hosts_info[item.value] | default('') else item.key}) }}"
loop: "{{ hosts_info | dict2items }}"
- name: create temp group
add_host:
name: '{{ item.key }}'
storeids: '{{ item.value }}'
groups: store_group
with_dict: "{{ new_hosts_info }}"
- hosts: "store_group"
gather_facts: no
tasks:
- debug: var=storeids
- debug:
msg: "{{ item }}"
loop: "{{ storeids.split(',') }}"