2014-01-04 19:31:44 +01:00
|
|
|
# (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com>
|
2012-05-26 06:37:34 +02:00
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2015-05-04 04:47:26 +02:00
|
|
|
# Make coding more python3-ish
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
2014-12-03 16:27:27 +01:00
|
|
|
|
2016-11-15 21:36:53 +01:00
|
|
|
from ansible import constants as C
|
Become plugins (#50991)
* [WIP] become plugins
Move from hardcoded method to plugins for ease of use, expansion and overrides
- load into connection as it is going to be the main consumer
- play_context will also use to keep backwards compat API
- ensure shell is used to construct commands when needed
- migrate settings remove from base config in favor of plugin specific configs
- cleanup ansible-doc
- add become plugin docs
- remove deprecated sudo/su code and keywords
- adjust become options for cli
- set plugin options from context
- ensure config defs are avaialbe before instance
- refactored getting the shell plugin, fixed tests
- changed into regex as they were string matching, which does not work with random string generation
- explicitly set flags for play context tests
- moved plugin loading up front
- now loads for basedir also
- allow pyc/o for non m modules
- fixes to tests and some plugins
- migrate to play objects fro play_context
- simiplify gathering
- added utf8 headers
- moved option setting
- add fail msg to dzdo
- use tuple for multiple options on fail/missing
- fix relative plugin paths
- shift from play context to play
- all tasks already inherit this from play directly
- remove obsolete 'set play'
- correct environment handling
- add wrap_exe option to pfexec
- fix runas to noop
- fixed setting play context
- added password configs
- removed required false
- remove from doc building till they are ready
future development:
- deal with 'enable' and 'runas' which are not 'command wrappers' but 'state flags' and currently hardcoded in diff subsystems
* cleanup
remove callers to removed func
removed --sudo cli doc refs
remove runas become_exe
ensure keyerorr on plugin
also fix backwards compat, missing method is attributeerror, not ansible error
get remote_user consistently
ignore missing system_tmpdirs on plugin load
correct config precedence
add deprecation
fix networking imports
backwards compat for plugins using BECOME_METHODS
* Port become_plugins to context.CLIARGS
This is a work in progress:
* Stop passing options around everywhere as we can use context.CLIARGS
instead
* Refactor make_become_commands as asked for by alikins
* Typo in comment fix
* Stop loading values from the cli in more than one place
Both play and play_context were saving default values from the cli
arguments directly. This changes things so that the default values are
loaded into the play and then play_context takes them from there.
* Rename BECOME_PLUGIN_PATH to DEFAULT_BECOME_PLUGIN_PATH
As alikins said, all other plugin paths are named
DEFAULT_plugintype_PLUGIN_PATH. If we're going to rename these, that
should be done all at one time rather than piecemeal.
* One to throw away
This is a set of hacks to get setting FieldAttribute defaults to command
line args to work. It's not fully done yet.
After talking it over with sivel and jimi-c this should be done by
fixing FieldAttributeBase and _get_parent_attribute() calls to do the
right thing when there is a non-None default.
What we want to be able to do ideally is something like this:
class Base(FieldAttributeBase):
_check_mode = FieldAttribute([..] default=lambda: context.CLIARGS['check'])
class Play(Base):
# lambda so that we have a chance to parse the command line args
# before we get here. In the future we might be able to restructure
# this so that the cli parsing code runs before these classes are
# defined.
class Task(Base):
pass
And still have a playbook like this function:
---
- hosts:
tasks:
- command: whoami
check_mode: True
(The check_mode test that is added as a separate commit in this PR will
let you test variations on this case).
There's a few separate reasons that the code doesn't let us do this or
a non-ugly workaround for this as written right now. The fix that
jimi-c, sivel, and I talked about may let us do this or it may still
require a workaround (but less ugly) (having one class that has the
FieldAttributes with default values and one class that inherits from
that but just overrides the FieldAttributes which now have defaults)
* Revert "One to throw away"
This reverts commit 23aa883cbed11429ef1be2a2d0ed18f83a3b8064.
* Set FieldAttr defaults directly from CLIARGS
* Remove dead code
* Move timeout directly to PlayContext, it's never needed on Play
* just for backwards compat, add a static version of BECOME_METHODS to constants
* Make the become attr on the connection public, since it's used outside of the connection
* Logic fix
* Nuke connection testing if it supports specific become methods
* Remove unused vars
* Address rebase issues
* Fix path encoding issue
* Remove unused import
* Various cleanups
* Restore network_cli check in _low_level_execute_command
* type improvements for cliargs_deferred_get and swap shallowcopy to default to False
* minor cleanups
* Allow the su plugin to work, since it doesn't define a prompt the same way
* Fix up ksu become plugin
* Only set prompt if build_become_command was called
* Add helper to assist connection plugins in knowing they need to wait for a prompt
* Fix tests and code expectations
* Doc updates
* Various additional minor cleanups
* Make doas functional
* Don't change connection signature, load become plugin from TaskExecutor
* Remove unused imports
* Add comment about setting the become plugin on the playcontext
* Fix up tests for recent changes
* Support 'Password:' natively for the doas plugin
* Make default prompts raw
* wording cleanups. ci_complete
* Remove unrelated changes
* Address spelling mistake
* Restore removed test, and udpate to use new functionality
* Add changelog fragment
* Don't hard fail in set_attributes_from_cli on missing CLI keys
* Remove unrelated change to loader
* Remove internal deprecated FieldAttributes now
* Emit deprecation warnings now
2019-02-11 18:27:44 +01:00
|
|
|
from ansible import context
|
2017-11-13 17:51:18 +01:00
|
|
|
from ansible.errors import AnsibleParserError, AnsibleAssertionError
|
2019-05-22 17:28:06 +02:00
|
|
|
from ansible.module_utils._text import to_native
|
2017-03-23 21:35:05 +01:00
|
|
|
from ansible.module_utils.six import string_types
|
2015-11-11 17:09:11 +01:00
|
|
|
from ansible.playbook.attribute import FieldAttribute
|
2015-05-04 04:47:26 +02:00
|
|
|
from ansible.playbook.base import Base
|
2015-07-22 17:20:50 +02:00
|
|
|
from ansible.playbook.block import Block
|
2019-03-28 18:41:39 +01:00
|
|
|
from ansible.playbook.collectionsearch import CollectionSearch
|
2015-05-04 04:47:26 +02:00
|
|
|
from ansible.playbook.helpers import load_list_of_blocks, load_list_of_roles
|
|
|
|
from ansible.playbook.role import Role
|
|
|
|
from ansible.playbook.taggable import Taggable
|
2017-05-23 23:16:49 +02:00
|
|
|
from ansible.vars.manager import preprocess_vars
|
2018-11-21 00:06:51 +01:00
|
|
|
from ansible.utils.display import Display
|
2012-08-22 04:08:08 +02:00
|
|
|
|
2018-11-21 00:06:51 +01:00
|
|
|
display = Display()
|
2015-09-09 23:52:44 +02:00
|
|
|
|
2012-05-26 06:37:34 +02:00
|
|
|
|
2015-11-11 17:09:11 +01:00
|
|
|
__all__ = ['Play']
|
|
|
|
|
|
|
|
|
2019-04-12 18:13:55 +02:00
|
|
|
class Play(Base, Taggable, CollectionSearch):
|
2012-05-26 07:20:53 +02:00
|
|
|
|
2015-05-04 04:47:26 +02:00
|
|
|
"""
|
|
|
|
A play is a language feature that represents a list of roles and/or
|
|
|
|
task/handler blocks to execute on a given set of hosts.
|
2012-05-26 06:37:34 +02:00
|
|
|
|
2015-05-04 04:47:26 +02:00
|
|
|
Usage:
|
2012-05-26 06:37:34 +02:00
|
|
|
|
2015-05-04 04:47:26 +02:00
|
|
|
Play.load(datastructure) -> Play
|
|
|
|
Play.something(...)
|
|
|
|
"""
|
2012-05-26 06:37:34 +02:00
|
|
|
|
2015-05-04 04:47:26 +02:00
|
|
|
# =================================================================================
|
2017-09-17 05:35:50 +02:00
|
|
|
_hosts = FieldAttribute(isa='list', required=True, listof=string_types, always_post_validate=True)
|
2012-05-26 06:37:34 +02:00
|
|
|
|
2017-09-17 05:35:50 +02:00
|
|
|
# Facts
|
2017-06-02 13:14:11 +02:00
|
|
|
_gather_facts = FieldAttribute(isa='bool', default=None, always_post_validate=True)
|
2019-07-22 23:59:22 +02:00
|
|
|
_gather_subset = FieldAttribute(isa='list', default=(lambda: C.DEFAULT_GATHER_SUBSET), listof=string_types, always_post_validate=True)
|
Become plugins (#50991)
* [WIP] become plugins
Move from hardcoded method to plugins for ease of use, expansion and overrides
- load into connection as it is going to be the main consumer
- play_context will also use to keep backwards compat API
- ensure shell is used to construct commands when needed
- migrate settings remove from base config in favor of plugin specific configs
- cleanup ansible-doc
- add become plugin docs
- remove deprecated sudo/su code and keywords
- adjust become options for cli
- set plugin options from context
- ensure config defs are avaialbe before instance
- refactored getting the shell plugin, fixed tests
- changed into regex as they were string matching, which does not work with random string generation
- explicitly set flags for play context tests
- moved plugin loading up front
- now loads for basedir also
- allow pyc/o for non m modules
- fixes to tests and some plugins
- migrate to play objects fro play_context
- simiplify gathering
- added utf8 headers
- moved option setting
- add fail msg to dzdo
- use tuple for multiple options on fail/missing
- fix relative plugin paths
- shift from play context to play
- all tasks already inherit this from play directly
- remove obsolete 'set play'
- correct environment handling
- add wrap_exe option to pfexec
- fix runas to noop
- fixed setting play context
- added password configs
- removed required false
- remove from doc building till they are ready
future development:
- deal with 'enable' and 'runas' which are not 'command wrappers' but 'state flags' and currently hardcoded in diff subsystems
* cleanup
remove callers to removed func
removed --sudo cli doc refs
remove runas become_exe
ensure keyerorr on plugin
also fix backwards compat, missing method is attributeerror, not ansible error
get remote_user consistently
ignore missing system_tmpdirs on plugin load
correct config precedence
add deprecation
fix networking imports
backwards compat for plugins using BECOME_METHODS
* Port become_plugins to context.CLIARGS
This is a work in progress:
* Stop passing options around everywhere as we can use context.CLIARGS
instead
* Refactor make_become_commands as asked for by alikins
* Typo in comment fix
* Stop loading values from the cli in more than one place
Both play and play_context were saving default values from the cli
arguments directly. This changes things so that the default values are
loaded into the play and then play_context takes them from there.
* Rename BECOME_PLUGIN_PATH to DEFAULT_BECOME_PLUGIN_PATH
As alikins said, all other plugin paths are named
DEFAULT_plugintype_PLUGIN_PATH. If we're going to rename these, that
should be done all at one time rather than piecemeal.
* One to throw away
This is a set of hacks to get setting FieldAttribute defaults to command
line args to work. It's not fully done yet.
After talking it over with sivel and jimi-c this should be done by
fixing FieldAttributeBase and _get_parent_attribute() calls to do the
right thing when there is a non-None default.
What we want to be able to do ideally is something like this:
class Base(FieldAttributeBase):
_check_mode = FieldAttribute([..] default=lambda: context.CLIARGS['check'])
class Play(Base):
# lambda so that we have a chance to parse the command line args
# before we get here. In the future we might be able to restructure
# this so that the cli parsing code runs before these classes are
# defined.
class Task(Base):
pass
And still have a playbook like this function:
---
- hosts:
tasks:
- command: whoami
check_mode: True
(The check_mode test that is added as a separate commit in this PR will
let you test variations on this case).
There's a few separate reasons that the code doesn't let us do this or
a non-ugly workaround for this as written right now. The fix that
jimi-c, sivel, and I talked about may let us do this or it may still
require a workaround (but less ugly) (having one class that has the
FieldAttributes with default values and one class that inherits from
that but just overrides the FieldAttributes which now have defaults)
* Revert "One to throw away"
This reverts commit 23aa883cbed11429ef1be2a2d0ed18f83a3b8064.
* Set FieldAttr defaults directly from CLIARGS
* Remove dead code
* Move timeout directly to PlayContext, it's never needed on Play
* just for backwards compat, add a static version of BECOME_METHODS to constants
* Make the become attr on the connection public, since it's used outside of the connection
* Logic fix
* Nuke connection testing if it supports specific become methods
* Remove unused vars
* Address rebase issues
* Fix path encoding issue
* Remove unused import
* Various cleanups
* Restore network_cli check in _low_level_execute_command
* type improvements for cliargs_deferred_get and swap shallowcopy to default to False
* minor cleanups
* Allow the su plugin to work, since it doesn't define a prompt the same way
* Fix up ksu become plugin
* Only set prompt if build_become_command was called
* Add helper to assist connection plugins in knowing they need to wait for a prompt
* Fix tests and code expectations
* Doc updates
* Various additional minor cleanups
* Make doas functional
* Don't change connection signature, load become plugin from TaskExecutor
* Remove unused imports
* Add comment about setting the become plugin on the playcontext
* Fix up tests for recent changes
* Support 'Password:' natively for the doas plugin
* Make default prompts raw
* wording cleanups. ci_complete
* Remove unrelated changes
* Address spelling mistake
* Restore removed test, and udpate to use new functionality
* Add changelog fragment
* Don't hard fail in set_attributes_from_cli on missing CLI keys
* Remove unrelated change to loader
* Remove internal deprecated FieldAttributes now
* Emit deprecation warnings now
2019-02-11 18:27:44 +01:00
|
|
|
_gather_timeout = FieldAttribute(isa='int', default=C.DEFAULT_GATHER_TIMEOUT, always_post_validate=True)
|
|
|
|
_fact_path = FieldAttribute(isa='string', default=C.DEFAULT_FACT_PATH)
|
2012-05-26 06:37:34 +02:00
|
|
|
|
2015-05-04 04:47:26 +02:00
|
|
|
# Variable Attributes
|
2018-10-12 17:43:09 +02:00
|
|
|
_vars_files = FieldAttribute(isa='list', default=list, priority=99)
|
|
|
|
_vars_prompt = FieldAttribute(isa='list', default=list, always_post_validate=False)
|
2012-08-09 16:56:40 +02:00
|
|
|
|
2015-08-05 19:54:00 +02:00
|
|
|
# Role Attributes
|
2018-10-12 17:43:09 +02:00
|
|
|
_roles = FieldAttribute(isa='list', default=list, priority=90)
|
2015-08-05 19:54:00 +02:00
|
|
|
|
2015-05-04 04:47:26 +02:00
|
|
|
# Block (Task) Lists Attributes
|
2018-10-12 17:43:09 +02:00
|
|
|
_handlers = FieldAttribute(isa='list', default=list)
|
|
|
|
_pre_tasks = FieldAttribute(isa='list', default=list)
|
|
|
|
_post_tasks = FieldAttribute(isa='list', default=list)
|
|
|
|
_tasks = FieldAttribute(isa='list', default=list)
|
2012-08-09 16:56:40 +02:00
|
|
|
|
2015-05-04 04:47:26 +02:00
|
|
|
# Flag/Setting Attributes
|
Become plugins (#50991)
* [WIP] become plugins
Move from hardcoded method to plugins for ease of use, expansion and overrides
- load into connection as it is going to be the main consumer
- play_context will also use to keep backwards compat API
- ensure shell is used to construct commands when needed
- migrate settings remove from base config in favor of plugin specific configs
- cleanup ansible-doc
- add become plugin docs
- remove deprecated sudo/su code and keywords
- adjust become options for cli
- set plugin options from context
- ensure config defs are avaialbe before instance
- refactored getting the shell plugin, fixed tests
- changed into regex as they were string matching, which does not work with random string generation
- explicitly set flags for play context tests
- moved plugin loading up front
- now loads for basedir also
- allow pyc/o for non m modules
- fixes to tests and some plugins
- migrate to play objects fro play_context
- simiplify gathering
- added utf8 headers
- moved option setting
- add fail msg to dzdo
- use tuple for multiple options on fail/missing
- fix relative plugin paths
- shift from play context to play
- all tasks already inherit this from play directly
- remove obsolete 'set play'
- correct environment handling
- add wrap_exe option to pfexec
- fix runas to noop
- fixed setting play context
- added password configs
- removed required false
- remove from doc building till they are ready
future development:
- deal with 'enable' and 'runas' which are not 'command wrappers' but 'state flags' and currently hardcoded in diff subsystems
* cleanup
remove callers to removed func
removed --sudo cli doc refs
remove runas become_exe
ensure keyerorr on plugin
also fix backwards compat, missing method is attributeerror, not ansible error
get remote_user consistently
ignore missing system_tmpdirs on plugin load
correct config precedence
add deprecation
fix networking imports
backwards compat for plugins using BECOME_METHODS
* Port become_plugins to context.CLIARGS
This is a work in progress:
* Stop passing options around everywhere as we can use context.CLIARGS
instead
* Refactor make_become_commands as asked for by alikins
* Typo in comment fix
* Stop loading values from the cli in more than one place
Both play and play_context were saving default values from the cli
arguments directly. This changes things so that the default values are
loaded into the play and then play_context takes them from there.
* Rename BECOME_PLUGIN_PATH to DEFAULT_BECOME_PLUGIN_PATH
As alikins said, all other plugin paths are named
DEFAULT_plugintype_PLUGIN_PATH. If we're going to rename these, that
should be done all at one time rather than piecemeal.
* One to throw away
This is a set of hacks to get setting FieldAttribute defaults to command
line args to work. It's not fully done yet.
After talking it over with sivel and jimi-c this should be done by
fixing FieldAttributeBase and _get_parent_attribute() calls to do the
right thing when there is a non-None default.
What we want to be able to do ideally is something like this:
class Base(FieldAttributeBase):
_check_mode = FieldAttribute([..] default=lambda: context.CLIARGS['check'])
class Play(Base):
# lambda so that we have a chance to parse the command line args
# before we get here. In the future we might be able to restructure
# this so that the cli parsing code runs before these classes are
# defined.
class Task(Base):
pass
And still have a playbook like this function:
---
- hosts:
tasks:
- command: whoami
check_mode: True
(The check_mode test that is added as a separate commit in this PR will
let you test variations on this case).
There's a few separate reasons that the code doesn't let us do this or
a non-ugly workaround for this as written right now. The fix that
jimi-c, sivel, and I talked about may let us do this or it may still
require a workaround (but less ugly) (having one class that has the
FieldAttributes with default values and one class that inherits from
that but just overrides the FieldAttributes which now have defaults)
* Revert "One to throw away"
This reverts commit 23aa883cbed11429ef1be2a2d0ed18f83a3b8064.
* Set FieldAttr defaults directly from CLIARGS
* Remove dead code
* Move timeout directly to PlayContext, it's never needed on Play
* just for backwards compat, add a static version of BECOME_METHODS to constants
* Make the become attr on the connection public, since it's used outside of the connection
* Logic fix
* Nuke connection testing if it supports specific become methods
* Remove unused vars
* Address rebase issues
* Fix path encoding issue
* Remove unused import
* Various cleanups
* Restore network_cli check in _low_level_execute_command
* type improvements for cliargs_deferred_get and swap shallowcopy to default to False
* minor cleanups
* Allow the su plugin to work, since it doesn't define a prompt the same way
* Fix up ksu become plugin
* Only set prompt if build_become_command was called
* Add helper to assist connection plugins in knowing they need to wait for a prompt
* Fix tests and code expectations
* Doc updates
* Various additional minor cleanups
* Make doas functional
* Don't change connection signature, load become plugin from TaskExecutor
* Remove unused imports
* Add comment about setting the become plugin on the playcontext
* Fix up tests for recent changes
* Support 'Password:' natively for the doas plugin
* Make default prompts raw
* wording cleanups. ci_complete
* Remove unrelated changes
* Address spelling mistake
* Restore removed test, and udpate to use new functionality
* Add changelog fragment
* Don't hard fail in set_attributes_from_cli on missing CLI keys
* Remove unrelated change to loader
* Remove internal deprecated FieldAttributes now
* Emit deprecation warnings now
2019-02-11 18:27:44 +01:00
|
|
|
_force_handlers = FieldAttribute(isa='bool', default=context.cliargs_deferred_get('force_handlers'), always_post_validate=True)
|
2015-08-28 21:54:20 +02:00
|
|
|
_max_fail_percentage = FieldAttribute(isa='percent', always_post_validate=True)
|
2018-10-12 17:43:09 +02:00
|
|
|
_serial = FieldAttribute(isa='list', default=list, always_post_validate=True)
|
2017-06-02 13:14:11 +02:00
|
|
|
_strategy = FieldAttribute(isa='string', default=C.DEFAULT_STRATEGY, always_post_validate=True)
|
|
|
|
_order = FieldAttribute(isa='string', always_post_validate=True)
|
2012-08-09 16:56:40 +02:00
|
|
|
|
2015-05-04 04:47:26 +02:00
|
|
|
# =================================================================================
|
2012-05-26 06:37:34 +02:00
|
|
|
|
2015-05-04 04:47:26 +02:00
|
|
|
def __init__(self):
|
|
|
|
super(Play, self).__init__()
|
2014-04-03 02:46:51 +02:00
|
|
|
|
2017-02-21 20:46:10 +01:00
|
|
|
self._included_conditional = None
|
2015-09-29 06:25:59 +02:00
|
|
|
self._included_path = None
|
2016-11-08 01:54:09 +01:00
|
|
|
self._removed_hosts = []
|
2015-07-10 08:43:53 +02:00
|
|
|
self.ROLE_CACHE = {}
|
|
|
|
|
Become plugins (#50991)
* [WIP] become plugins
Move from hardcoded method to plugins for ease of use, expansion and overrides
- load into connection as it is going to be the main consumer
- play_context will also use to keep backwards compat API
- ensure shell is used to construct commands when needed
- migrate settings remove from base config in favor of plugin specific configs
- cleanup ansible-doc
- add become plugin docs
- remove deprecated sudo/su code and keywords
- adjust become options for cli
- set plugin options from context
- ensure config defs are avaialbe before instance
- refactored getting the shell plugin, fixed tests
- changed into regex as they were string matching, which does not work with random string generation
- explicitly set flags for play context tests
- moved plugin loading up front
- now loads for basedir also
- allow pyc/o for non m modules
- fixes to tests and some plugins
- migrate to play objects fro play_context
- simiplify gathering
- added utf8 headers
- moved option setting
- add fail msg to dzdo
- use tuple for multiple options on fail/missing
- fix relative plugin paths
- shift from play context to play
- all tasks already inherit this from play directly
- remove obsolete 'set play'
- correct environment handling
- add wrap_exe option to pfexec
- fix runas to noop
- fixed setting play context
- added password configs
- removed required false
- remove from doc building till they are ready
future development:
- deal with 'enable' and 'runas' which are not 'command wrappers' but 'state flags' and currently hardcoded in diff subsystems
* cleanup
remove callers to removed func
removed --sudo cli doc refs
remove runas become_exe
ensure keyerorr on plugin
also fix backwards compat, missing method is attributeerror, not ansible error
get remote_user consistently
ignore missing system_tmpdirs on plugin load
correct config precedence
add deprecation
fix networking imports
backwards compat for plugins using BECOME_METHODS
* Port become_plugins to context.CLIARGS
This is a work in progress:
* Stop passing options around everywhere as we can use context.CLIARGS
instead
* Refactor make_become_commands as asked for by alikins
* Typo in comment fix
* Stop loading values from the cli in more than one place
Both play and play_context were saving default values from the cli
arguments directly. This changes things so that the default values are
loaded into the play and then play_context takes them from there.
* Rename BECOME_PLUGIN_PATH to DEFAULT_BECOME_PLUGIN_PATH
As alikins said, all other plugin paths are named
DEFAULT_plugintype_PLUGIN_PATH. If we're going to rename these, that
should be done all at one time rather than piecemeal.
* One to throw away
This is a set of hacks to get setting FieldAttribute defaults to command
line args to work. It's not fully done yet.
After talking it over with sivel and jimi-c this should be done by
fixing FieldAttributeBase and _get_parent_attribute() calls to do the
right thing when there is a non-None default.
What we want to be able to do ideally is something like this:
class Base(FieldAttributeBase):
_check_mode = FieldAttribute([..] default=lambda: context.CLIARGS['check'])
class Play(Base):
# lambda so that we have a chance to parse the command line args
# before we get here. In the future we might be able to restructure
# this so that the cli parsing code runs before these classes are
# defined.
class Task(Base):
pass
And still have a playbook like this function:
---
- hosts:
tasks:
- command: whoami
check_mode: True
(The check_mode test that is added as a separate commit in this PR will
let you test variations on this case).
There's a few separate reasons that the code doesn't let us do this or
a non-ugly workaround for this as written right now. The fix that
jimi-c, sivel, and I talked about may let us do this or it may still
require a workaround (but less ugly) (having one class that has the
FieldAttributes with default values and one class that inherits from
that but just overrides the FieldAttributes which now have defaults)
* Revert "One to throw away"
This reverts commit 23aa883cbed11429ef1be2a2d0ed18f83a3b8064.
* Set FieldAttr defaults directly from CLIARGS
* Remove dead code
* Move timeout directly to PlayContext, it's never needed on Play
* just for backwards compat, add a static version of BECOME_METHODS to constants
* Make the become attr on the connection public, since it's used outside of the connection
* Logic fix
* Nuke connection testing if it supports specific become methods
* Remove unused vars
* Address rebase issues
* Fix path encoding issue
* Remove unused import
* Various cleanups
* Restore network_cli check in _low_level_execute_command
* type improvements for cliargs_deferred_get and swap shallowcopy to default to False
* minor cleanups
* Allow the su plugin to work, since it doesn't define a prompt the same way
* Fix up ksu become plugin
* Only set prompt if build_become_command was called
* Add helper to assist connection plugins in knowing they need to wait for a prompt
* Fix tests and code expectations
* Doc updates
* Various additional minor cleanups
* Make doas functional
* Don't change connection signature, load become plugin from TaskExecutor
* Remove unused imports
* Add comment about setting the become plugin on the playcontext
* Fix up tests for recent changes
* Support 'Password:' natively for the doas plugin
* Make default prompts raw
* wording cleanups. ci_complete
* Remove unrelated changes
* Address spelling mistake
* Restore removed test, and udpate to use new functionality
* Add changelog fragment
* Don't hard fail in set_attributes_from_cli on missing CLI keys
* Remove unrelated change to loader
* Remove internal deprecated FieldAttributes now
* Emit deprecation warnings now
2019-02-11 18:27:44 +01:00
|
|
|
self.only_tags = set(context.CLIARGS.get('tags', [])) or frozenset(('all',))
|
|
|
|
self.skip_tags = set(context.CLIARGS.get('skip_tags', []))
|
|
|
|
|
2015-05-04 04:47:26 +02:00
|
|
|
def __repr__(self):
|
|
|
|
return self.get_name()
|
2012-05-26 06:37:34 +02:00
|
|
|
|
2015-05-04 04:47:26 +02:00
|
|
|
def get_name(self):
|
2015-11-11 17:09:11 +01:00
|
|
|
''' return the name of the Play '''
|
2019-01-23 18:40:07 +01:00
|
|
|
return self.name
|
2012-05-26 06:37:34 +02:00
|
|
|
|
2015-05-04 04:47:26 +02:00
|
|
|
@staticmethod
|
2018-04-30 23:13:43 +02:00
|
|
|
def load(data, variable_manager=None, loader=None, vars=None):
|
2016-02-25 17:25:17 +01:00
|
|
|
if ('name' not in data or data['name'] is None) and 'hosts' in data:
|
2019-05-21 17:11:16 +02:00
|
|
|
if data['hosts'] is None or all(host is None for host in data['hosts']):
|
|
|
|
raise AnsibleParserError("Hosts list cannot be empty - please check your playbook")
|
2016-02-25 17:29:44 +01:00
|
|
|
if isinstance(data['hosts'], list):
|
|
|
|
data['name'] = ','.join(data['hosts'])
|
|
|
|
else:
|
2016-09-17 09:45:29 +02:00
|
|
|
data['name'] = data['hosts']
|
2015-05-04 04:47:26 +02:00
|
|
|
p = Play()
|
2018-04-30 23:13:43 +02:00
|
|
|
if vars:
|
|
|
|
p.vars = vars.copy()
|
2015-05-04 04:47:26 +02:00
|
|
|
return p.load_data(data, variable_manager=variable_manager, loader=loader)
|
2012-05-26 06:37:34 +02:00
|
|
|
|
2015-05-04 04:47:26 +02:00
|
|
|
def preprocess_data(self, ds):
|
|
|
|
'''
|
|
|
|
Adjusts play datastructure to cleanup old/legacy items
|
|
|
|
'''
|
2014-04-10 19:43:59 +02:00
|
|
|
|
2017-11-13 17:51:18 +01:00
|
|
|
if not isinstance(ds, dict):
|
|
|
|
raise AnsibleAssertionError('while preprocessing data (%s), ds should be a dict but was a %s' % (ds, type(ds)))
|
2014-08-15 22:57:02 +02:00
|
|
|
|
2015-05-04 04:47:26 +02:00
|
|
|
# The use of 'user' in the Play datastructure was deprecated to
|
|
|
|
# line up with the same change for Tasks, due to the fact that
|
|
|
|
# 'user' conflicted with the user module.
|
|
|
|
if 'user' in ds:
|
|
|
|
# this should never happen, but error out with a helpful message
|
|
|
|
# to the user if it does...
|
|
|
|
if 'remote_user' in ds:
|
2017-06-02 13:14:11 +02:00
|
|
|
raise AnsibleParserError("both 'user' and 'remote_user' are set for %s. "
|
|
|
|
"The use of 'user' is deprecated, and should be removed" % self.get_name(), obj=ds)
|
2014-08-15 22:57:02 +02:00
|
|
|
|
2015-05-04 04:47:26 +02:00
|
|
|
ds['remote_user'] = ds['user']
|
|
|
|
del ds['user']
|
|
|
|
|
|
|
|
return super(Play, self).preprocess_data(ds)
|
|
|
|
|
|
|
|
def _load_tasks(self, attr, ds):
|
|
|
|
'''
|
|
|
|
Loads a list of blocks from a list which may be mixed tasks/blocks.
|
|
|
|
Bare tasks outside of a block are given an implicit block.
|
|
|
|
'''
|
2015-09-23 14:56:36 +02:00
|
|
|
try:
|
|
|
|
return load_list_of_blocks(ds=ds, play=self, variable_manager=self._variable_manager, loader=self._loader)
|
2017-06-09 19:13:15 +02:00
|
|
|
except AssertionError as e:
|
2019-05-22 17:28:06 +02:00
|
|
|
raise AnsibleParserError("A malformed block was encountered while loading tasks: %s" % to_native(e), obj=self._ds, orig_exc=e)
|
2015-05-04 04:47:26 +02:00
|
|
|
|
|
|
|
def _load_pre_tasks(self, attr, ds):
|
|
|
|
'''
|
|
|
|
Loads a list of blocks from a list which may be mixed tasks/blocks.
|
|
|
|
Bare tasks outside of a block are given an implicit block.
|
|
|
|
'''
|
2015-09-23 14:56:36 +02:00
|
|
|
try:
|
|
|
|
return load_list_of_blocks(ds=ds, play=self, variable_manager=self._variable_manager, loader=self._loader)
|
2017-06-09 19:13:15 +02:00
|
|
|
except AssertionError as e:
|
|
|
|
raise AnsibleParserError("A malformed block was encountered while loading pre_tasks", obj=self._ds, orig_exc=e)
|
2015-05-04 04:47:26 +02:00
|
|
|
|
|
|
|
def _load_post_tasks(self, attr, ds):
|
|
|
|
'''
|
|
|
|
Loads a list of blocks from a list which may be mixed tasks/blocks.
|
|
|
|
Bare tasks outside of a block are given an implicit block.
|
|
|
|
'''
|
2015-09-23 14:56:36 +02:00
|
|
|
try:
|
|
|
|
return load_list_of_blocks(ds=ds, play=self, variable_manager=self._variable_manager, loader=self._loader)
|
2017-06-09 19:13:15 +02:00
|
|
|
except AssertionError as e:
|
|
|
|
raise AnsibleParserError("A malformed block was encountered while loading post_tasks", obj=self._ds, orig_exc=e)
|
2015-05-04 04:47:26 +02:00
|
|
|
|
|
|
|
def _load_handlers(self, attr, ds):
|
|
|
|
'''
|
|
|
|
Loads a list of blocks from a list which may be mixed handlers/blocks.
|
|
|
|
Bare handlers outside of a block are given an implicit block.
|
|
|
|
'''
|
2015-09-23 14:56:36 +02:00
|
|
|
try:
|
2018-05-01 19:18:36 +02:00
|
|
|
return self._extend_value(
|
|
|
|
self.handlers,
|
|
|
|
load_list_of_blocks(ds=ds, play=self, use_handlers=True, variable_manager=self._variable_manager, loader=self._loader),
|
|
|
|
prepend=True
|
|
|
|
)
|
2017-06-09 19:13:15 +02:00
|
|
|
except AssertionError as e:
|
|
|
|
raise AnsibleParserError("A malformed block was encountered while loading handlers", obj=self._ds, orig_exc=e)
|
2015-05-04 04:47:26 +02:00
|
|
|
|
|
|
|
def _load_roles(self, attr, ds):
|
|
|
|
'''
|
|
|
|
Loads and returns a list of RoleInclude objects from the datastructure
|
|
|
|
list of role definitions and creates the Role from those objects
|
|
|
|
'''
|
|
|
|
|
2015-05-05 20:17:04 +02:00
|
|
|
if ds is None:
|
|
|
|
ds = []
|
|
|
|
|
2015-09-23 14:56:36 +02:00
|
|
|
try:
|
|
|
|
role_includes = load_list_of_roles(ds, play=self, variable_manager=self._variable_manager, loader=self._loader)
|
2017-06-09 19:13:15 +02:00
|
|
|
except AssertionError as e:
|
|
|
|
raise AnsibleParserError("A malformed role declaration was encountered.", obj=self._ds, orig_exc=e)
|
2015-05-04 04:47:26 +02:00
|
|
|
|
|
|
|
roles = []
|
|
|
|
for ri in role_includes:
|
2015-07-10 07:53:59 +02:00
|
|
|
roles.append(Role.load(ri, play=self))
|
2018-10-23 18:08:48 +02:00
|
|
|
|
|
|
|
return self._extend_value(
|
|
|
|
self.roles,
|
|
|
|
roles,
|
|
|
|
prepend=True
|
|
|
|
)
|
2015-05-04 04:47:26 +02:00
|
|
|
|
2015-09-09 23:52:44 +02:00
|
|
|
def _load_vars_prompt(self, attr, ds):
|
|
|
|
new_ds = preprocess_vars(ds)
|
|
|
|
vars_prompts = []
|
2018-02-08 18:25:20 +01:00
|
|
|
if new_ds is not None:
|
|
|
|
for prompt_data in new_ds:
|
|
|
|
if 'name' not in prompt_data:
|
2018-08-20 23:26:10 +02:00
|
|
|
raise AnsibleParserError("Invalid vars_prompt data structure", obj=ds)
|
2018-02-08 18:25:20 +01:00
|
|
|
else:
|
|
|
|
vars_prompts.append(prompt_data)
|
2015-09-09 23:52:44 +02:00
|
|
|
return vars_prompts
|
2015-05-04 04:47:26 +02:00
|
|
|
|
|
|
|
def _compile_roles(self):
|
|
|
|
'''
|
|
|
|
Handles the role compilation step, returning a flat list of tasks
|
|
|
|
with the lowest level dependencies first. For example, if a role R
|
|
|
|
has a dependency D1, which also has a dependency D2, the tasks from
|
|
|
|
D2 are merged first, followed by D1, and lastly by the tasks from
|
|
|
|
the parent role R last. This is done for all roles in the Play.
|
|
|
|
'''
|
|
|
|
|
|
|
|
block_list = []
|
|
|
|
|
|
|
|
if len(self.roles) > 0:
|
|
|
|
for r in self.roles:
|
2018-07-15 16:59:30 +02:00
|
|
|
# Don't insert tasks from ``import/include_role``, preventing
|
|
|
|
# duplicate execution at the wrong time
|
|
|
|
if r.from_include:
|
|
|
|
continue
|
2015-05-04 04:47:26 +02:00
|
|
|
block_list.extend(r.compile(play=self))
|
|
|
|
|
|
|
|
return block_list
|
|
|
|
|
2015-06-20 10:40:41 +02:00
|
|
|
def compile_roles_handlers(self):
|
|
|
|
'''
|
|
|
|
Handles the role handler compilation step, returning a flat list of Handlers
|
|
|
|
This is done for all roles in the Play.
|
|
|
|
'''
|
|
|
|
|
|
|
|
block_list = []
|
|
|
|
|
|
|
|
if len(self.roles) > 0:
|
|
|
|
for r in self.roles:
|
2018-08-29 22:05:55 +02:00
|
|
|
if r.from_include:
|
|
|
|
continue
|
2016-07-06 21:40:11 +02:00
|
|
|
block_list.extend(r.get_handler_blocks(play=self))
|
2015-06-20 10:40:41 +02:00
|
|
|
|
|
|
|
return block_list
|
|
|
|
|
2015-05-04 04:47:26 +02:00
|
|
|
def compile(self):
|
|
|
|
'''
|
|
|
|
Compiles and returns the task list for this play, compiled from the
|
|
|
|
roles (which are themselves compiled recursively) and/or the list of
|
|
|
|
tasks specified in the play.
|
|
|
|
'''
|
|
|
|
|
2015-07-22 17:20:50 +02:00
|
|
|
# create a block containing a single flush handlers meta
|
|
|
|
# task, so we can be sure to run handlers at certain points
|
|
|
|
# of the playbook execution
|
|
|
|
flush_block = Block.load(
|
|
|
|
data={'meta': 'flush_handlers'},
|
|
|
|
play=self,
|
|
|
|
variable_manager=self._variable_manager,
|
|
|
|
loader=self._loader
|
|
|
|
)
|
|
|
|
|
2015-05-04 04:47:26 +02:00
|
|
|
block_list = []
|
|
|
|
|
|
|
|
block_list.extend(self.pre_tasks)
|
2015-07-22 17:20:50 +02:00
|
|
|
block_list.append(flush_block)
|
2015-05-04 04:47:26 +02:00
|
|
|
block_list.extend(self._compile_roles())
|
|
|
|
block_list.extend(self.tasks)
|
2015-07-22 17:20:50 +02:00
|
|
|
block_list.append(flush_block)
|
2015-05-04 04:47:26 +02:00
|
|
|
block_list.extend(self.post_tasks)
|
2015-07-22 17:20:50 +02:00
|
|
|
block_list.append(flush_block)
|
2015-05-04 04:47:26 +02:00
|
|
|
|
|
|
|
return block_list
|
|
|
|
|
|
|
|
def get_vars(self):
|
|
|
|
return self.vars.copy()
|
|
|
|
|
|
|
|
def get_vars_files(self):
|
2017-10-09 17:27:50 +02:00
|
|
|
if self.vars_files is None:
|
|
|
|
return []
|
2018-06-22 20:58:40 +02:00
|
|
|
elif not isinstance(self.vars_files, list):
|
|
|
|
return [self.vars_files]
|
2015-05-04 04:47:26 +02:00
|
|
|
return self.vars_files
|
|
|
|
|
|
|
|
def get_handlers(self):
|
|
|
|
return self.handlers[:]
|
|
|
|
|
|
|
|
def get_roles(self):
|
|
|
|
return self.roles[:]
|
|
|
|
|
|
|
|
def get_tasks(self):
|
|
|
|
tasklist = []
|
|
|
|
for task in self.pre_tasks + self.tasks + self.post_tasks:
|
|
|
|
if isinstance(task, Block):
|
|
|
|
tasklist.append(task.block + task.rescue + task.always)
|
2012-05-26 06:37:34 +02:00
|
|
|
else:
|
2015-05-04 04:47:26 +02:00
|
|
|
tasklist.append(task)
|
|
|
|
return tasklist
|
|
|
|
|
|
|
|
def serialize(self):
|
|
|
|
data = super(Play, self).serialize()
|
|
|
|
|
|
|
|
roles = []
|
|
|
|
for role in self.get_roles():
|
|
|
|
roles.append(role.serialize())
|
|
|
|
data['roles'] = roles
|
2015-09-29 06:25:59 +02:00
|
|
|
data['included_path'] = self._included_path
|
2015-05-04 04:47:26 +02:00
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
def deserialize(self, data):
|
|
|
|
super(Play, self).deserialize(data)
|
|
|
|
|
2015-09-29 06:25:59 +02:00
|
|
|
self._included_path = data.get('included_path', None)
|
2015-05-04 04:47:26 +02:00
|
|
|
if 'roles' in data:
|
|
|
|
role_data = data.get('roles', [])
|
|
|
|
roles = []
|
|
|
|
for role in role_data:
|
|
|
|
r = Role()
|
|
|
|
r.deserialize(role)
|
|
|
|
roles.append(r)
|
|
|
|
|
|
|
|
setattr(self, 'roles', roles)
|
|
|
|
del data['roles']
|
|
|
|
|
2015-07-10 08:43:53 +02:00
|
|
|
def copy(self):
|
|
|
|
new_me = super(Play, self).copy()
|
|
|
|
new_me.ROLE_CACHE = self.ROLE_CACHE.copy()
|
2017-02-21 20:46:10 +01:00
|
|
|
new_me._included_conditional = self._included_conditional
|
2015-09-29 06:25:59 +02:00
|
|
|
new_me._included_path = self._included_path
|
2015-07-10 08:43:53 +02:00
|
|
|
return new_me
|