Class: QB::Ansible::Cmd::Playbook
- Inherits:
-
Cmds
- Object
- Cmds
- QB::Ansible::Cmd::Playbook
- 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. 'ansible-playbook'
- TEMPLATE =
<<-END <%== exe %> <%= cmd_options %> <% if verbose %> -<%= 'v' * verbose %> <% end %> <%= playbook_path %> END
Instance Attribute Summary collapse
-
#exe ⇒ String
readonly
Whatever to use for the
ansible-playbook
executable. -
#extra_vars ⇒ Hash
readonly
Hash of extra variables that will be JSON encoded and passed to
ansible-playbook
via the--extra-vars
CLI option. -
#playbook ⇒ nil, Hash
readonly
Optional playbook object to write and run.
-
#playbook_path ⇒ String, Pathname
readonly
Path to the playbook.
-
#role_options ⇒ nil, QB::Options
readonly
Optional role options if running a role.
Instance Method Summary collapse
- #before_spawn ⇒ nil
-
#cmd_options ⇒ Hash<String => Object>
Hash of CLI options for
ansible-playbook
based off #role_options and #playbook. -
#env ⇒ Object
Override so we can call
#to_h
in caseenv
is Env. -
#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
constructor
Instantiate a new
QB::Ansible::Playbook
. -
#kwds ⇒ Hash{Symbol => Object}
Dynamically form the keywords from instance variables.
-
#prepare(*args, &block) ⇒ Object
HACK To fix test fails on linux...
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
.
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 = @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
#exe ⇒ String (readonly)
Whatever to use for the ansible-playbook
executable.
71 72 73 |
# File 'lib/qb/ansible/cmd/playbook.rb', line 71 def exe @exe end |
#extra_vars ⇒ Hash (readonly)
Hash of extra variables that will be JSON encoded and passed to
ansible-playbook
via the --extra-vars
CLI option.
102 103 104 |
# File 'lib/qb/ansible/cmd/playbook.rb', line 102 def extra_vars @extra_vars end |
#playbook ⇒ nil, Hash (readonly)
Optional playbook object to write and run.
83 84 85 |
# File 'lib/qb/ansible/cmd/playbook.rb', line 83 def playbook @playbook end |
#playbook_path ⇒ String, 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
64 65 66 |
# File 'lib/qb/ansible/cmd/playbook.rb', line 64 def playbook_path @playbook_path end |
#role_options ⇒ nil, QB::Options (readonly)
Optional role options if running a role.
94 95 96 |
# File 'lib/qb/ansible/cmd/playbook.rb', line 94 def @role_options end |
Instance Method Details
#before_spawn ⇒ 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_options ⇒ Hash<String => Object>
Returns 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 = {} if # Merge in any Ansible options collected. .merge! .ansible # Add tags if we have them if .qb['tags'] ['tags'] = .qb['tags'] end end # Add inventory file if we have it in QB options for the role. if && .qb['inventory'] ['inventory-file'] = .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 ['inventory-file'] = play['hosts'] end # Add extra vars if we have any. unless @extra_vars.empty? ['extra-vars'] = JSON.dump @extra_vars end end |
#env ⇒ Object
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 |
#kwds ⇒ Hash{Symbol => Object}
Dynamically form the keywords from instance variables.
185 186 187 188 189 190 191 192 |
# File 'lib/qb/ansible/cmd/playbook.rb', line 185 def kwds { exe: exe, verbose: ( && .qb['verbose']), playbook_path: playbook_path.to_s, cmd_options: , } end |
#prepare(*args, &block) ⇒ Object
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 |