Module: Ansible::PlaybookMethods

Included in:
Ansible, Playbook
Defined in:
lib/ansible/playbook.rb

Overview

Ansible Playbook methods

Constant Summary collapse

BIN =

executable that runs Ansible Playbooks

'ansible-playbook'

Instance Method Summary collapse

Instance Method Details

#playbook(pb) ⇒ String Also known as: <<

Run playbook, returning output

Parameters:

  • pb (String)

    path to playbook

Returns:

  • (String)

    output



13
14
15
16
# File 'lib/ansible/playbook.rb', line 13

def playbook(pb)
  # TODO if debug then puts w/ colour
  `#{config.to_s "#{BIN} #{pb}"}`
end

#stream(pb, raise_on_failure: false) ⇒ Integer

Stream execution of a playbook using PTY because otherwise output is buffered

Parameters:

  • pb (String)

    path to playbook

  • raise_on_failure (Symbol) (defaults to: false)

    Specifies if streaming should raise an exception when a Playbook failure occurs. Defaults to :false, can also be :during to raise as soon as an error occurs or :after to allow all output to stream first.

Returns:

  • (Integer)

    exit status

Raises:



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ansible/playbook.rb', line 25

def stream(pb, raise_on_failure: false)
  cmd = config.to_s("#{BIN} #{pb}")
  error_at_line = {}

  pid = SafePty.spawn cmd do |r,_,_| # add -vvvv here for verbose
    line_num = 0

    until r.eof? do
      line = r.gets
      line_num += 1

      block_given? ? yield(line) : puts(line)

      # track errors in output by line
      if raise_on_failure
        case line
          when /fatal: \[/ then error_at_line[line_num] ||= "FAILED: #{line}"
          when /ERROR!/, /FAILED!/ then error_at_line[line_num] ||= "ERROR: #{line}"
          # allow errors on previous line to be ignored
          when /...ignoring/ then error_at_line.delete(line_num-1)
        end

        if raise_on_failure == :during
          # trigger failure unless it was ignored
          fatal_unskipped_error = error_at_line[line_num-1]
          raise Playbook::Exception.new(fatal_unskipped_error) if fatal_unskipped_error
        end
      end
    end
  end

  if raise_on_failure
    # at this point, all output has been streamed
    fatal_unskipped_error = error_at_line.first
    raise Playbook::Exception.new(fatal_unskipped_error.last) if fatal_unskipped_error
  end

  pid
end