class QB::Ansible::Cmd::Playbook

A command object that runs a playbook with all the QB special-ness.

Constants

DEFAULT_EXE

Default executable to use, just uses a bare `ansible-playbook`, letting shell path resolution do it's thing.

@return [String]

DEFAULT_PLAYBOOK_PATH

Constants

TEMPLATE

Attributes

exe[R]

Whatever to use for the `ansible-playbook` executable.

@return [String]

extra_vars[R]

Hash of extra variables that will be JSON encoded and passed to `ansible-playbook` via the `–extra-vars` CLI option.

@return [Hash]

playbook[R]

Optional playbook object to write and run.

@return [nil]

If we should expect to find the playbook already at {#playbook_path}.

@return [Hash]

If we will be writing a YAML dump of this object to {#playbook_path}
before runnning.
playbook_path[R]

Path to the playbook. If a `playbook:` keyword argument is provided to the constructor, then this is the path it will be written to before

@return [String, Pathname]

role_options[R]

Optional role options if running a role.

@return [nil]

If we're not running a role.

@return [QB::Options]

If we are running a role.

Public Class Methods

new(chdir: nil, env: QB::Ansible::Env.new, exe: DEFAULT_EXE, extra_vars: {}) click to toggle source

Instantiate a new `QB::Ansible::Playbook`.

@param [Hash] extra_vars:

Extra variables that will be JSON encoded and passed to
`ansible-playbook` via the `--extra-vars` option.

Available as the {#extra_vars} attribute.
Calls superclass method
# File lib/qb/ansible/cmd/playbook.rb, line 116
def initialize  chdir: nil,
                env: QB::Ansible::Env.new,
                exe: DEFAULT_EXE,
                extra_vars: {},
                format: :pretty,
                playbook: nil,
                playbook_path: DEFAULT_PLAYBOOK_PATH,
                role_options: nil,
                **other_cmds_opts
  @exe = exe.to_s
  @extra_vars = extra_vars
  @role_options = role_options
  @playbook = playbook
  
  # Resolve whatever path we got to an absolute.
  @playbook_path = QB::Util.resolve playbook_path
  
  super TEMPLATE,
        format: format,
        chdir: chdir,
        **other_cmds_opts
  
  # Overwrite `@env` because `super` freezes it.
  @env = env
end

Public Instance Methods

before_spawn() click to toggle source

Stuff to do before being run, like write {#playbook} to {#path} (unless {#playbook} is `nil`).

@return [nil]

# File lib/qb/ansible/cmd/playbook.rb, line 206
def before_spawn
  # Write the playbook to the path first if one was provided.
  unless playbook.nil?
    playbook_path.open('w') { |f|
      f.write YAML.dump(playbook)
    }
  end
end
cmd_options() click to toggle source

@return [Hash<String => Object>]

Hash of CLI options for `ansible-playbook` based off {#role_options}
and {#playbook}.
# File lib/qb/ansible/cmd/playbook.rb, line 150
def cmd_options
  cmd_options = {}
  
  if role_options
    # Merge in any Ansible options collected.
    cmd_options.merge! role_options.ansible
    
    # Add tags if we have them
    if role_options.qb['tags']
      cmd_options['tags'] = role_options.qb['tags']
    end
  end
  
  # Add inventory file if we have it in QB options for the role.
  if role_options && role_options.qb['inventory']
    cmd_options['inventory-file'] = role_options.qb['inventory']
  elsif playbook && playbook[0]['hosts'] != ['localhost']
    # TODO  I'm not totally sure why this is here, but I copied it over from
    #       `//exe/qb`...? Get overridden below anyways if
    cmd_options['inventory-file'] = play['hosts']
  end
  
  # Add extra vars if we have any.
  unless @extra_vars.empty?
    cmd_options['extra-vars'] = JSON.dump @extra_vars
  end
  
  cmd_options
end
env() click to toggle source

Override so we can call `#to_h` in case `env` is {QB::Ansible::Env}.

# File lib/qb/ansible/cmd/playbook.rb, line 196
def env
  @env.to_h
end
kwds() click to toggle source

Dynamically form the keywords from instance variables.

@return [Hash{Symbol => Object}]

# File lib/qb/ansible/cmd/playbook.rb, line 185
def kwds
  {
    exe: exe,
    verbose: (role_options && role_options.qb['verbose']),
    playbook_path: playbook_path.to_s,
    cmd_options: cmd_options,
  }
end
prepare(*args, &block) click to toggle source

HACK To fix test fails on linux… seems you can't end a command there

with a `\`

@todo Move up to Cmds

Calls superclass method
# File lib/qb/ansible/cmd/playbook.rb, line 221
def prepare *args, &block
  prepared = super *args, &block
  prepared.gsub /[\s\n\\]+\z/, ''
end

Protected Instance Methods

spawn(*args, **kwds, &input_block) click to toggle source

@return [Fixnum]

Calls superclass method
# File lib/qb/ansible/cmd/playbook.rb, line 231
def spawn *args, **kwds, &input_block
  before_spawn
  
  QB::Util::Bundler.with_clean_env do
    # Start the STDIO server
    stdio_server = QB::IPC::STDIO::Server.new.start!

    # Start the RPC server
    rpc_server = QB::IPC::RPC::Server.new.start!
    
    status = QB::IPC::RPC::Server.run_around do
      super *args, **kwds, &input_block
    end

    # ...and stop it
    stdio_server.stop!
    
    # and return the status
    status
  end
  
end