Method: Bolt::Config.load_bolt_defaults_yaml

Defined in:
lib/bolt/config.rb

.load_bolt_defaults_yaml(dir) ⇒ Object

Loads a ‘bolt-defaults.yaml’ file, which contains default configuration that applies to all projects. This file does not allow project-specific configuration such as ‘hiera-config’ and nests all default inventory configuration under an ‘inventory-config’ key.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/bolt/config.rb', line 82

def self.load_bolt_defaults_yaml(dir)
  filepath     = dir + DEFAULTS_NAME
  data         = Bolt::Util.read_yaml_hash(filepath, 'config')

  Bolt::Logger.debug("Loaded configuration from #{filepath}")

  # Validate the config against the schema. This will raise a single error
  # with all validation errors.
  Bolt::Validator.new.tap do |validator|
    validator.validate(data, defaults_schema, filepath)
    validator.warnings.each { |warning| Bolt::Logger.warn(warning[:id], warning[:msg]) }
    validator.deprecations.each { |dep| Bolt::Logger.deprecate(dep[:id], dep[:msg]) }
  end

  # Remove project-specific config such as hiera-config, etc.
  project_config = data.slice(*(PROJECT_OPTIONS - DEFAULTS_OPTIONS))

  if project_config.any?
    data.reject! { |key, _| project_config.include?(key) }

    Bolt::Logger.warn(
      "unsupported_project_config",
      "Unsupported project configuration detected in '#{filepath}': #{project_config.keys}. "\
      "Project configuration should be set in 'bolt-project.yaml'."
    )
  end

  # Remove top-level transport config such as transport, ssh, etc.
  transport_config = data.slice(*INVENTORY_OPTIONS.keys)

  if transport_config.any?
    data.reject! { |key, _| transport_config.include?(key) }

    Bolt::Logger.warn(
      "unsupported_inventory_config",
      "Unsupported inventory configuration detected in '#{filepath}': #{transport_config.keys}. "\
      "Transport configuration should be set under the 'inventory-config' option or "\
      "in 'inventory.yaml'."
    )
  end

  # Move data under inventory-config to top-level so it can be easily merged with
  # config from other sources. Error early if inventory-config is not a hash or
  # has a plugin reference.
  if data.key?('inventory-config')
    unless data['inventory-config'].is_a?(Hash)
      raise Bolt::ValidationError,
            "Option 'inventory-config' must be of type Hash, received #{data['inventory-config']} "\
            "#{data['inventory-config']} (file: #{filepath})"
    end

    if data['inventory-config'].key?('_plugin')
      raise Bolt::ValidationError,
            "Found unsupported key '_plugin' for option 'inventory-config'; supported keys are "\
            "'#{INVENTORY_OPTIONS.keys.join("', '")}' (file: #{filepath})"
    end

    data = data.merge(data.delete('inventory-config'))
  end

  { filepath: filepath, data: data }
end