Class: Ansible::Ruby::DslBuilders::Task

Inherits:
Unit
  • Object
show all
Defined in:
lib/ansible/ruby/dsl_builders/task.rb

Class Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Unit

#ansible_when, #become, #become_user, #ignore_errors, #vars

Methods inherited from Base

#jinja, #method_missing

Constructor Details

#initialize(name, context) ⇒ Task

Returns a new instance of Task.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ansible/ruby/dsl_builders/task.rb', line 20

def initialize(name, context)
  super()
  @name = name
  @context = context
  @module = nil
  @inclusion = nil
  # Until the variable is utilized, we don't know if 'register' should be set, the supplied lambda
  name_fetcher = lambda do
    self.class.counter_variable += 1
    name = "result_#{self.class.counter_variable}"
    @task_args[:register] = name
    name
  end
  @register = Result.new name_fetcher
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Ansible::Ruby::DslBuilders::Base

Class Attribute Details

.counter_variableObject

Returns the value of attribute counter_variable.



16
17
18
# File 'lib/ansible/ruby/dsl_builders/task.rb', line 16

def counter_variable
  @counter_variable
end

Instance Method Details

#_registerObject



96
97
98
# File 'lib/ansible/ruby/dsl_builders/task.rb', line 96

def _register
  @register
end

#_resultObject

allow for other attributes besides the module in any order



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/ansible/ruby/dsl_builders/task.rb', line 101

def _result
  args = {
    name: @name
  }.merge @task_args
  args[:module] = @module if @module
  args[:inclusion] = @inclusion if @inclusion
  args[:block] = @block if @block
  args[:rescue] = @rescue if @rescue
  task = @context.new args
  # Quick feedback if the type is wrong, etc.
  task.validate! if validate?
  task
end

#ansible_block(&block) ⇒ Object



119
120
121
122
123
124
# File 'lib/ansible/ruby/dsl_builders/task.rb', line 119

def ansible_block(&block)
  block_builder = Block.new
  block_builder.instance_eval(&block)
  @block = block_builder._result
  nil
end

#ansible_rescue(&block) ⇒ Object



126
127
128
129
130
131
# File 'lib/ansible/ruby/dsl_builders/task.rb', line 126

def ansible_rescue(&block)
  block_builder = Block.new
  block_builder.instance_eval(&block)
  @rescue = block_builder._result
  nil
end

#async(value) ⇒ Object



67
68
69
# File 'lib/ansible/ruby/dsl_builders/task.rb', line 67

def async(value)
  @task_args[:async] = value
end

#changed_when(clause) ⇒ Object



44
45
46
# File 'lib/ansible/ruby/dsl_builders/task.rb', line 44

def changed_when(clause)
  @task_args[:changed_when] = clause
end

#connection(value) ⇒ Object



40
41
42
# File 'lib/ansible/ruby/dsl_builders/task.rb', line 40

def connection(value)
  @task_args[:connection] = value
end

#delegate_to(value) ⇒ Object



84
85
86
# File 'lib/ansible/ruby/dsl_builders/task.rb', line 84

def delegate_to(value)
  @task_args[:delegate_to] = value
end

#failed_when(clause) ⇒ Object



48
49
50
# File 'lib/ansible/ruby/dsl_builders/task.rb', line 48

def failed_when(clause)
  @task_args[:failed_when] = clause
end

#local_actionObject



79
80
81
82
# File 'lib/ansible/ruby/dsl_builders/task.rb', line 79

def local_action
  @task_args[:local_action] = true
  yield
end

#no_log(value) ⇒ Object



36
37
38
# File 'lib/ansible/ruby/dsl_builders/task.rb', line 36

def no_log(value)
  @task_args[:no_log] = value
end

#notify(value) ⇒ Object



75
76
77
# File 'lib/ansible/ruby/dsl_builders/task.rb', line 75

def notify(value)
  @task_args[:notify] = value
end

#poll(value) ⇒ Object



71
72
73
# File 'lib/ansible/ruby/dsl_builders/task.rb', line 71

def poll(value)
  @task_args[:poll] = value
end

#remote_user(value) ⇒ Object



88
89
90
# File 'lib/ansible/ruby/dsl_builders/task.rb', line 88

def remote_user(value)
  @task_args[:remote_user] = value
end

#respond_to_missing?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/ansible/ruby/dsl_builders/task.rb', line 92

def respond_to_missing?(*)
  !@module || super
end

#validate?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/ansible/ruby/dsl_builders/task.rb', line 115

def validate?
  true
end

#with_dict(clause) {|[hash_key, hash_value]| ... } ⇒ Object

Yields:

  • ([hash_key, hash_value])


52
53
54
55
56
57
58
59
# File 'lib/ansible/ruby/dsl_builders/task.rb', line 52

def with_dict(clause)
  @task_args[:with_dict] = clause
  return unless block_given?

  hash_key = JinjaItemNode.new('item.key')
  hash_value = JinjaItemNode.new('item.value')
  yield [hash_key, hash_value]
end

#with_items(*clause) {|JinjaItemNode.new| ... } ⇒ Object

Yields:



61
62
63
64
65
# File 'lib/ansible/ruby/dsl_builders/task.rb', line 61

def with_items(*clause)
  # 1 arg is probably a variable reference, so don't use an array
  @task_args[:with_items] = clause.length == 1 ? clause[0] : clause
  yield JinjaItemNode.new if block_given?
end