Last active
April 4, 2024 02:35
-
-
Save Tcarters/f51358738628208bb738e6428fc05da7 to your computer and use it in GitHub Desktop.
A demo to group data column in a csv file passing through python and using ansible to display it.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import yaml | |
# Path to your CSV file | |
csv_file_path = 'sample_ip.csv' | |
# Initialize an empty dictionary to store the groups and hostnames | |
groups = {} | |
# Open and read the CSV file | |
with open(csv_file_path, mode='r') as file: | |
# Create a CSV reader object | |
csv_reader = csv.DictReader(file) | |
# Iterate over each row in the CSV file | |
for row in csv_reader: | |
group = row['Group'] | |
hostname = row['Hostname'] | |
# Check if the group already exists in the dictionary | |
if group in groups: | |
# Append the hostname to the existing group's list if not already present | |
if hostname not in groups[group]: | |
groups[group].append(hostname) | |
else: | |
# Create a new entry in the dictionary with the group as the key | |
groups[group] = [hostname] | |
# Convert the dictionary to the desired list format for YAML | |
yaml_list = [{'groupname': group, 'members': members} for group, members in groups.items()] | |
# Define the path for the YAML file | |
yaml_file_path = 'groups.yaml' | |
# Write to the YAML file | |
with open(yaml_file_path, 'w') as file: | |
yaml.dump(yaml_list, file, default_flow_style=False) | |
print(f"Groups have been successfully written to {yaml_file_path}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- name: Reading list | |
hosts: lab | |
gather_facts: no | |
tasks: | |
- name: Read content from the group file | |
set_fact: | |
# items: "{{ lookup('file', './groups.yaml') }}" | |
group_data: "{{ lookup('file', './groups.yaml') | from_yaml }}" | |
- name: Print group raw | |
debug: | |
var: "{{ group_data }}" | |
- name: Print each group name and its members | |
debug: | |
msg: "Group name: {{ item.groupname }} | Members: {{ item.members | join(', ') }}" | |
loop: "{{ group_data }}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- groupname: web | |
members: | |
- frontend | |
- server1 | |
- groupname: data | |
members: | |
- backend | |
- server5 | |
- groupname: db | |
members: | |
- server3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hostname | Ip | Group | |
---|---|---|---|
frontend | 192.168.100.21 | web | |
server1 | 192.168.100.22 | web | |
backend | 10.10.168.31\24 | data | |
server3 | 192.168.100.21\20 | db | |
server5 | 10.10.168.21\24 | data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment