Path: | lib/automateit/tag_manager.rb |
Last Update: | Fri Mar 20 04:47:12 -0700 2009 |
The TagManager provides a way of querying tags. Tags are keywords associated with a specific hostname or group. These are useful for grouping together hosts and defining common behavior for them.
The tags are typically stored in a Project‘s config/tags.yml file.
For example, consider this config/tags.yml file:
desktops: - satori - sunyata - michiru notebooks: - rheya - avijja
With the above file, if we‘re on the host called "satori", we can query the fields like this:
tags # => ["satori", "desktops", "localhost", ...] tagged?("desktops") # => true tagged?("notebooks") # => false tagged?(:satori) # => true tagged?("satori") # => true tagged?("satori || desktops") # => true tagged?("(satori || desktops) && !notebooks") # => true
Your system may also automatically add tags that describe your system‘s traits, such as the name of the operating system, distribution release, hardware architecture, hostnames, IP addresses, etc.
For example, here is a full set of tags for a system:
ai> pp tags.sort # Pretty print the tags in sorted order ["10.0.0.6", # IPv4 addresses "127.0.0.1", # ... "192.168.130.1", # ... "::1/128", # IPv6 addresses "fe80::250:56ff:fec0:8/64", # ... "fe80::250:8dff:fe95:8fe9/64", # ... "i686", # Hardware architecture "linux", # OS "linux_i686", # OS and architecture "localhost", # Variants of hostname "localhost.localdomain", # ... "michiru", # ... "michiru.koshevoy", # ... "michiru.koshevoy.net", # ... "myapp_servers", # User defined tags "rails_servers", # ... "ubuntu", # OS distribution name "ubuntu_6.06"] # OS distribution name and release version
To execute code only on an Ubuntu system:
if tagged?("ubuntu") # Code will only be run on Ubuntu systems end
These additional tags are retrieved from the PlatformManager and AddressManager. If your platform does not provide drivers for these, you will not get these tags. If you‘re on an unsupported platform and do not want to write drivers, you can work around this by manually declaring the missing tags in config/tags.yml on a host-by-host basis.
You can include and negate tags declaratively by giving "@" and "!" prefixes to arguments.
For example, consider this config/tags.yml file:
apache_servers: - kurou - shirou apache_servers_except_kurou: - @apache_servers - !kurou
This will produce the following results:
ai> hosts_tagged_with("apache_servers") => ["kurou", "shirou"] ai> hosts_tagged_with("apache_servers_except_kurou") => ["shirou"]