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
Whatever to use for the `ansible-playbook` executable.
@return [String]
Hash of extra variables that will be JSON encoded and passed to `ansible-playbook` via the `–extra-vars` CLI
option.
@return [Hash]
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.
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]
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
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.
# 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
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
@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
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
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
HACK To fix test fails on linux… seems you can't end a command there
with a `\`
@todo Move up to Cmds
# 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
@return [Fixnum]
# 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