class MU::Groomer::Ansible::Inventory
Simple interface for an Ansible
inventory file.
Public Class Methods
new(deploy)
click to toggle source
@param deploy [MU::MommaCat]
# File modules/mu/groomers/ansible.rb, line 679 def initialize(deploy) @deploy = deploy @ansible_path = @deploy.deploy_dir+"/ansible" if !Dir.exist?(@ansible_path) Dir.mkdir(@ansible_path, 0755) end @lockfile = File.open(@ansible_path+"/.hosts.lock", File::CREAT|File::RDWR, 0600) end
Public Instance Methods
add(group, name)
click to toggle source
Add a node to our Ansible
inventory @param group [String]: The host group to which the node belongs @param name [String]: The hostname or IP of the node
# File modules/mu/groomers/ansible.rb, line 706 def add(group, name) if group.nil? or group.empty? or name.nil? or name.empty? raise MuError, "Ansible::Inventory.add requires both a host group string and a name" end lock read @inv[group] ||= [] @inv[group] << name @inv[group].uniq! save! unlock end
haveNode?(name)
click to toggle source
See if we have a particular node in our inventory.
# File modules/mu/groomers/ansible.rb, line 690 def haveNode?(name) lock read @inv.values.each { |nodes| if nodes.include?(name) unlock return true end } unlock false end
remove(name)
click to toggle source
Remove a node from our Ansible
inventory @param name [String]: The hostname or IP of the node
# File modules/mu/groomers/ansible.rb, line 721 def remove(name) lock read @inv.each_pair { |_group, nodes| nodes.delete(name) } save! unlock end
Private Instance Methods
lock()
click to toggle source
# File modules/mu/groomers/ansible.rb, line 733 def lock @lockfile.flock(File::LOCK_EX) end
read()
click to toggle source
# File modules/mu/groomers/ansible.rb, line 753 def read @inv = {} if File.exist?(@ansible_path+"/hosts") section = nil File.readlines(@ansible_path+"/hosts").each { |l| l.chomp! l.sub!(/#.*/, "") next if l.empty? if l.match(/\[(.+?)\]/) section = Regexp.last_match[1] @inv[section] ||= [] else @inv[section] << l end } end @inv end
save!()
click to toggle source
# File modules/mu/groomers/ansible.rb, line 741 def save! @inv ||= {} File.open(@ansible_path+"/hosts", File::CREAT|File::RDWR|File::TRUNC, 0600) { |f| @inv.each_pair { |group, hosts| next if hosts.size == 0 # don't write empty groups f.puts "["+group+"]" f.puts hosts.join("\n") } } end
unlock()
click to toggle source
# File modules/mu/groomers/ansible.rb, line 737 def unlock @lockfile.flock(File::LOCK_UN) end