Skip to content

Commit

Permalink
Merge branch 'release/0.7.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
b08x committed Aug 7, 2024
2 parents b189b4c + 090f1af commit 3788a3d
Show file tree
Hide file tree
Showing 1,079 changed files with 28,158 additions and 19,411 deletions.
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ docs/build/
hostvars.json
*.zip
*.qcow2
*.iso
*.iso
file/closet/
roles/utils/files/dotfiles/
roles/llmops/files/*.agi.json
file.log
/.venv/
454 changes: 311 additions & 143 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion ansible.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ connect_timeout = 30
highlight = bright green
debug = dark gray
ok = blue
changed = bright yellow
changed = yellow

[diff]
always = True
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
166 changes: 166 additions & 0 deletions bin/cfgmgmtv4.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'cli/ui'
require 'yaml'
require 'json'
require 'csv'
require 'open3'
require 'tty-prompt'

# Configuration
class Configuration
attr_reader :ansible_home, :playbooks, :group_vars, :host_vars, :roles_home, :tags, :groups, :hosts,
:roles
attr_accessor :tags, :groups, :hosts, :roles, :inventory

def initialize
@ansible_home = ENV['ANSIBLE_HOME'] || File.expand_path('..', __dir__)
@inventory = File.join(@ansible_home, 'inventory.ini')
@playbooks = File.join(@ansible_home, 'playbooks')
@group_vars = File.join(@ansible_home, 'group_vars')
@host_vars = File.join(@ansible_home, 'host_vars')
@roles_home = File.join(@ansible_home, 'roles')
@tags = [] # Dynamically generated later
@groups = [] # Dynamically generated later
@hosts = [] # Dynamically generated later
@roles = [] # Dynamically generated later
end
end

# Ansible Project Class
class AnsibleProject
attr_reader :playbooks, :group_vars, :host_vars, :tags

def initialize(config)
@config = config
@playbooks = Dir.children(@config.playbooks).map { |file| File.join(@config.playbooks, file) }
@group_vars = @config.group_vars
@host_vars = @config.host_vars
end

def tags(playbook)
`ansible-playbook -i #{@config.inventory} #{playbook} --list-tags | awk -F 'TASK TAGS:' '{print $2}'`.chomp.strip.gsub(
/\[|\]|,/, ''
)
end

end

# UI Class
class UI
def initialize(config)
@config = config
@prompt = TTY::Prompt.new
end

def select_playbook(ansible_project)
@prompt.select('Select Playbook', ansible_project.playbooks)
end

def select_type
@prompt.select('Select Type', ['n/a', 'tags', 'roles'])
end

def select_tags(playbook, ansible_project)
# Get tags dynamically from the playbook
tags = ansible_project.tags(playbook)
@prompt.multi_select('Select Tags', tags.split(" "))
end

def select_roles
@prompt.multi_select('Select Roles', @config.roles)
end

def select_limit
@prompt.select('Select Limit', %w[all groups hosts localhost])
end

def select_group
@prompt.multi_select('Select Group', @config.groups)
end

def select_host
@prompt.multi_select('Select Host', @config.hosts)
end

end

# Main Script
config = Configuration.new

# Dynamically generate lists from Ansible project
# config.tags = `ansible-playbook -i #{config.inventory} #{config.playbooks} --list-tags | awk -F 'TASK TAGS:' '{print $2}'`.chomp.strip.gsub(
# /\[|\]|,/, ''
# ).split(' ')

config.groups = Dir.children(config.group_vars).map { |group| group.gsub('.yml', '') }
config.hosts = Dir.children(config.host_vars).map { |host| host.gsub('.yml', '') }
config.roles = Dir.children(config.roles_home)

ansible_project = AnsibleProject.new(config)
ui = UI.new(config)

playbook = ui.select_playbook(ansible_project)
type = ui.select_type

tags = case type
when 'tags'
ui.select_tags(playbook, ansible_project)
when 'roles'
ui.select_roles
else
[]
end

limit = ui.select_limit

group = case limit
when 'groups'
ui.select_group
when 'hosts'
ui.select_host
when 'localhost'
localhost = true
config.inventory = File.join(config.ansible_home, 'hosts')
else
[]
end

command_parts = ['ansible-playbook', '-i', config.inventory, playbook]
unless localhost
command_parts << '--limit' << group.join(',') unless group.empty?
end
command_parts << '--tags' << tags.join(',') unless tags.empty?

CLI::UI.frame_style = :bracket

CLI::UI::StdoutRouter.enable

CLI::UI::Frame.open('syncopatedOS') do

CLI::UI::Frame.open('Info') do
puts command_parts
end
# Run Ansible Playbook
CLI::UI::Frame.open('Output') do
Dir.chdir(config.ansible_home) do
Open3.popen3(command_parts.join(' ')) do |_stdin, stdout, stderr, _thread|
# Capture stdout and stderr
stdout_output = stdout.read
stderr_output = stderr.read

# Print stdout
puts stdout_output

# Check for errors
if stderr_output.empty?
puts 'Playbook executed successfully!'
else
puts 'Ansible Playbook encountered errors:'
puts stderr_output
end
end
end
end
end
Loading

0 comments on commit 3788a3d

Please sign in to comment.