mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-06 13:22:48 +00:00
Allow conditional imports, see examples/playbook3.yml comments for a full explanation. Extensive
refactoring of playbooks now warranted, which we'll do before we move on. This variable assignment system makes nearly all possible magic possible, for we can use these variables however we like, even as module names!
This commit is contained in:
4
test/CentOS.yml
Normal file
4
test/CentOS.yml
Normal file
@@ -0,0 +1,4 @@
|
||||
---
|
||||
# could test something different here but want people running tests on
|
||||
# different OS platforms to still have passing tests
|
||||
testing: default
|
||||
@@ -26,6 +26,15 @@ class TestCallbacks(object):
|
||||
def on_start(self):
|
||||
self.events.append('start')
|
||||
|
||||
def on_setup_primary(self):
|
||||
self.events.append([ 'primary_setup' ])
|
||||
|
||||
def on_setup_secondary(self):
|
||||
self.events.append([ 'secondary_setup' ])
|
||||
|
||||
def on_import_for_host(self, host, filename):
|
||||
self.events.append([ 'import', [ host, filename ]])
|
||||
|
||||
def on_task_start(self, name, is_conditional):
|
||||
self.events.append([ 'task start', [ name, is_conditional ]])
|
||||
|
||||
|
||||
3
test/common_vars.yml
Normal file
3
test/common_vars.yml
Normal file
@@ -0,0 +1,3 @@
|
||||
---
|
||||
duck: quack
|
||||
cow: moo
|
||||
2
test/default_os.yml
Normal file
2
test/default_os.yml
Normal file
@@ -0,0 +1,2 @@
|
||||
---
|
||||
testing: default
|
||||
@@ -7,6 +7,26 @@
|
||||
"all"
|
||||
]
|
||||
],
|
||||
[
|
||||
"primary_setup"
|
||||
],
|
||||
[
|
||||
"secondary_setup"
|
||||
],
|
||||
[
|
||||
"import",
|
||||
[
|
||||
"127.0.0.1",
|
||||
"/home/mdehaan/ansible/test/common_vars.yml"
|
||||
]
|
||||
],
|
||||
[
|
||||
"import",
|
||||
[
|
||||
"127.0.0.1",
|
||||
"/home/mdehaan/ansible/test/CentOS.yml"
|
||||
]
|
||||
],
|
||||
[
|
||||
"task start",
|
||||
[
|
||||
@@ -68,6 +88,25 @@
|
||||
}
|
||||
]
|
||||
],
|
||||
[
|
||||
"task start",
|
||||
[
|
||||
"test vars_files imports",
|
||||
false
|
||||
]
|
||||
],
|
||||
[
|
||||
"ok",
|
||||
[
|
||||
"127.0.0.1",
|
||||
{
|
||||
"cmd": "echo quack moo default ",
|
||||
"rc": 0,
|
||||
"stderr": "",
|
||||
"stdout": "quack moo default"
|
||||
}
|
||||
]
|
||||
],
|
||||
[
|
||||
"task start",
|
||||
[
|
||||
@@ -196,7 +235,8 @@
|
||||
"changed": 2,
|
||||
"dark": 0,
|
||||
"failed": 0,
|
||||
"resources": 8
|
||||
"resources": 9
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,10 @@
|
||||
vars:
|
||||
answer: "Wuh, I think so, Brain, but if we didn't have ears, we'd look like weasels."
|
||||
port: 5150
|
||||
|
||||
vars_files:
|
||||
- common_vars.yml
|
||||
- [ '$facter_operatingsystem.yml', 'default_os.yml' ]
|
||||
|
||||
tasks:
|
||||
|
||||
- name: test basic success command
|
||||
@@ -16,6 +19,9 @@
|
||||
- name: test basic shell, plus two ways to dereference a variable
|
||||
action: shell echo $HOME $port {{ port }}
|
||||
|
||||
- name: test vars_files imports
|
||||
action: shell echo $duck $cow $testing
|
||||
|
||||
# in the command below, the test file should contain a valid template
|
||||
# and trigger the change handler
|
||||
|
||||
|
||||
71
test/playbook2.yml
Normal file
71
test/playbook2.yml
Normal file
@@ -0,0 +1,71 @@
|
||||
---
|
||||
# this is an annotated example of some features available in playbooks
|
||||
# it shows how to make sure packages are updated, how to make sure
|
||||
# services are running, and how to template files. It also demos
|
||||
# change handlers that can restart things (or trigger other actions)
|
||||
# when resources change. For more advanced examples, see example2.yml
|
||||
|
||||
# on all hosts, run as the user root...
|
||||
|
||||
- hosts: all
|
||||
user: root
|
||||
|
||||
# make these variables available inside of templates
|
||||
# for when we use the 'template' action/module later on...
|
||||
|
||||
vars:
|
||||
http_port: 80
|
||||
max_clients: 200
|
||||
|
||||
# define the tasks that are part of this play...
|
||||
|
||||
tasks:
|
||||
|
||||
# task #1 is to run an arbitrary command
|
||||
# we'll simulate a long running task, wait for up to 45 seconds, poll every 5
|
||||
# obviously this does nothing useful but you get the idea
|
||||
|
||||
- name: longrunner
|
||||
action: command /bin/sleep 15
|
||||
async: 45
|
||||
poll: 5
|
||||
|
||||
# let's demo file operations.
|
||||
#
|
||||
# We can 'copy' files or 'template' them instead, using jinja2
|
||||
# as the templating engine. This is done using the variables
|
||||
# from the vars section above mixed in with variables bubbled up
|
||||
# automatically from tools like facter and ohai. 'copy'
|
||||
# works just like 'template' but does not do variable subsitution.
|
||||
#
|
||||
# If and only if the file changes, restart apache at the very
|
||||
# end of the playbook run
|
||||
|
||||
- name: write some_random_foo configuration
|
||||
action: template src=templates/foo.j2 dest=/etc/some_random_foo.conf
|
||||
notify:
|
||||
- restart apache
|
||||
|
||||
# make sure httpd is installed at the latest version
|
||||
|
||||
- name: install httpd
|
||||
action: yum pkg=httpd state=latest
|
||||
|
||||
# make sure httpd is running
|
||||
|
||||
- name: httpd start
|
||||
action: service name=httpd state=running
|
||||
|
||||
# handlers are only run when things change, at the very end of each
|
||||
# play. Let's define some. The names are significant and must
|
||||
# match the 'notify' sections above
|
||||
|
||||
handlers:
|
||||
|
||||
# this particular handler is run when some_random_foo.conf
|
||||
# is changed, and only then
|
||||
|
||||
- name: restart apache
|
||||
action: service name=httpd state=restarted
|
||||
|
||||
|
||||
71
test/playbook3.yml
Normal file
71
test/playbook3.yml
Normal file
@@ -0,0 +1,71 @@
|
||||
---
|
||||
# this is an annotated example of some features available in playbooks
|
||||
# it shows how to make sure packages are updated, how to make sure
|
||||
# services are running, and how to template files. It also demos
|
||||
# change handlers that can restart things (or trigger other actions)
|
||||
# when resources change. For more advanced examples, see example2.yml
|
||||
|
||||
# on all hosts, run as the user root...
|
||||
|
||||
- hosts: all
|
||||
user: root
|
||||
|
||||
# make these variables available inside of templates
|
||||
# for when we use the 'template' action/module later on...
|
||||
|
||||
vars:
|
||||
http_port: 80
|
||||
max_clients: 200
|
||||
|
||||
# define the tasks that are part of this play...
|
||||
|
||||
tasks:
|
||||
|
||||
# task #1 is to run an arbitrary command
|
||||
# we'll simulate a long running task, wait for up to 45 seconds, poll every 5
|
||||
# obviously this does nothing useful but you get the idea
|
||||
|
||||
- name: longrunner
|
||||
action: command /bin/sleep 15
|
||||
async: 45
|
||||
poll: 5
|
||||
|
||||
# let's demo file operations.
|
||||
#
|
||||
# We can 'copy' files or 'template' them instead, using jinja2
|
||||
# as the templating engine. This is done using the variables
|
||||
# from the vars section above mixed in with variables bubbled up
|
||||
# automatically from tools like facter and ohai. 'copy'
|
||||
# works just like 'template' but does not do variable subsitution.
|
||||
#
|
||||
# If and only if the file changes, restart apache at the very
|
||||
# end of the playbook run
|
||||
|
||||
- name: write some_random_foo configuration
|
||||
action: template src=templates/foo.j2 dest=/etc/some_random_foo.conf
|
||||
notify:
|
||||
- restart apache
|
||||
|
||||
# make sure httpd is installed at the latest version
|
||||
|
||||
- name: install httpd
|
||||
action: yum pkg=httpd state=latest
|
||||
|
||||
# make sure httpd is running
|
||||
|
||||
- name: httpd start
|
||||
action: service name=httpd state=running
|
||||
|
||||
# handlers are only run when things change, at the very end of each
|
||||
# play. Let's define some. The names are significant and must
|
||||
# match the 'notify' sections above
|
||||
|
||||
handlers:
|
||||
|
||||
# this particular handler is run when some_random_foo.conf
|
||||
# is changed, and only then
|
||||
|
||||
- name: restart apache
|
||||
action: service name=httpd state=restarted
|
||||
|
||||
|
||||
Reference in New Issue
Block a user