Sha256: 2d73c805f8b57e7c3d581c16c5d7be788dff15b2fc70ee7e94a9a4ba4a98d70b

Contents?: true

Size: 1.78 KB

Versions: 16

Compression:

Stored size: 1.78 KB

Contents

# frozen_string_literal: true

require 'tempfile'

# Write contents to a file on the given set of targets.
#
# > **Note:** Not available in apply block
Puppet::Functions.create_function(:write_file) do
  # @param targets A pattern identifying zero or more targets. See {get_targets} for accepted patterns.
  # @param content File content to write.
  # @param destination An absolute path on the target(s).
  # @option options [Boolean] _catch_errors Whether to catch raised errors.
  # @option options [String] _run_as User to run as using privilege escalation.
  # @return A list of results, one entry per target.
  # @example Write a file to a target
  #   $content = 'Hello, world!'
  #   write_file($targets, $content, '/Users/me/hello.txt')
  dispatch :write_file do
    required_param 'String', :content
    required_param 'String[1]', :destination
    required_param 'Boltlib::TargetSpec', :targets
    optional_param 'Hash[String[1], Any]', :options
    return_type 'ResultSet'
  end

  def write_file(content, destination, target_spec, options = {})
    unless Puppet[:tasks]
      raise Puppet::ParseErrorWithIssue
        .from_issue_and_stack(Bolt::PAL::Issues::PLAN_OPERATION_NOT_SUPPORTED_WHEN_COMPILING,
                              action: 'write_file')
    end

    executor = Puppet.lookup(:bolt_executor)
    # Send Analytics Report
    executor.report_function_call(self.class.name)

    inventory = Puppet.lookup(:bolt_inventory)
    targets = inventory.get_targets(target_spec)

    executor.log_action("write file #{destination}", targets) do
      executor.without_default_logging do
        Tempfile.create do |tmp|
          call_function('file::write', tmp.path, content)
          call_function('upload_file', tmp.path, destination, targets, options)
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
bolt-2.30.0 bolt-modules/boltlib/lib/puppet/functions/write_file.rb
bolt-2.29.0 bolt-modules/boltlib/lib/puppet/functions/write_file.rb
bolt-2.28.0 bolt-modules/boltlib/lib/puppet/functions/write_file.rb
bolt-2.27.0 bolt-modules/boltlib/lib/puppet/functions/write_file.rb
bolt-2.26.0 bolt-modules/boltlib/lib/puppet/functions/write_file.rb
bolt-2.25.0 bolt-modules/boltlib/lib/puppet/functions/write_file.rb
bolt-2.24.1 bolt-modules/boltlib/lib/puppet/functions/write_file.rb
bolt-2.24.0 bolt-modules/boltlib/lib/puppet/functions/write_file.rb
bolt-2.23.0 bolt-modules/boltlib/lib/puppet/functions/write_file.rb
bolt-2.22.0 bolt-modules/boltlib/lib/puppet/functions/write_file.rb
bolt-2.21.0 bolt-modules/boltlib/lib/puppet/functions/write_file.rb
bolt-2.20.0 bolt-modules/boltlib/lib/puppet/functions/write_file.rb
bolt-2.19.0 bolt-modules/boltlib/lib/puppet/functions/write_file.rb
bolt-2.18.0 bolt-modules/boltlib/lib/puppet/functions/write_file.rb
bolt-2.17.0 bolt-modules/boltlib/lib/puppet/functions/write_file.rb
bolt-2.16.0 bolt-modules/boltlib/lib/puppet/functions/write_file.rb