Class: QB::Ansible::Cmd::Playbook

Inherits:
Cmds
  • Object
show all
Defined in:
lib/qb/ansible/cmd/playbook.rb

Overview

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

Constant Summary collapse

DEFAULT_PLAYBOOK_PATH =

Constants

'.qb-playbook.yml'
DEFAULT_EXE =

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

Returns:

  • (String)
'ansible-playbook'
TEMPLATE =
<<-END
  <%== exe %>
  
  <%= cmd_options %>
  
  <% if verbose %>
    -<%= 'v' * verbose %>
  <% end %>
  
  <%= playbook_path %>
END

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#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) ⇒ Playbook

Instantiate a new QB::Ansible::Playbook.

Parameters:

  • extra_vars: (Hash) (defaults to: {})

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

    Available as the #extra_vars attribute.



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
# 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

Instance Attribute Details

#exeString (readonly)

Whatever to use for the ansible-playbook executable.

Returns:

  • (String)


71
72
73
# File 'lib/qb/ansible/cmd/playbook.rb', line 71

def exe
  @exe
end

#extra_varsHash (readonly)

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

Returns:

  • (Hash)


102
103
104
# File 'lib/qb/ansible/cmd/playbook.rb', line 102

def extra_vars
  @extra_vars
end

#playbooknil, Hash (readonly)

Optional playbook object to write and run.

Returns:

  • (nil)

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

  • (Hash)

    If we will be writing a YAML dump of this object to #playbook_path before runnning.



83
84
85
# File 'lib/qb/ansible/cmd/playbook.rb', line 83

def playbook
  @playbook
end

#playbook_pathString, Pathname (readonly)

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

Returns:

  • (String, Pathname)


64
65
66
# File 'lib/qb/ansible/cmd/playbook.rb', line 64

def playbook_path
  @playbook_path
end

#role_optionsnil, QB::Options (readonly)

Optional role options if running a role.

Returns:

  • (nil)

    If we're not running a role.

  • (QB::Options)

    If we are running a role.



94
95
96
# File 'lib/qb/ansible/cmd/playbook.rb', line 94

def role_options
  @role_options
end

Instance Method Details

#before_spawnnil

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

Returns:

  • (nil)


206
207
208
209
210
211
212
213
# 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_optionsHash<String => Object>

Returns Hash of CLI options for ansible-playbook based off #role_options and #playbook.

Returns:

  • (Hash<String => Object>)

    Hash of CLI options for ansible-playbook based off #role_options and #playbook.



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# 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

#envObject

Override so we can call #to_h in case env is Env.



196
197
198
# File 'lib/qb/ansible/cmd/playbook.rb', line 196

def env
  @env.to_h
end

#kwdsHash{Symbol => Object}

Dynamically form the keywords from instance variables.

Returns:

  • (Hash{Symbol => Object})


185
186
187
188
189
190
191
192
# 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) ⇒ Object

TODO:

Move up to Cmds

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



221
222
223
224
# File 'lib/qb/ansible/cmd/playbook.rb', line 221

def prepare *args, &block
  prepared = super *args, &block
  prepared.gsub /[\s\n\\]+\z/, ''
end