Commit Graph

54 Commits

Author SHA1 Message Date
Dmitriy Rabotyagov 7422a0941b Move config_template to plugins/action
Unlike to the modules, ansible-galaxy tool does not respect symlinks,
which results in collection brokeness after it installation as plugin
itself is just missing there.
In the meanwhile, symlinks are respected by regular git clone which
is used for regular module retrievment.

Change-Id: I747b217e6b0706c05e5fae98bc5b89be3cbb5930
2021-11-23 20:37:39 +02:00
Dmitriy Rabotyagov ea07ed008f Fix section overrides when commented option persist
In cases, when key already exist, but commented out without space
(ie "#key) and is not in default section, it's not updated with
sectdict.insert statement. That is possible happens because
self._sections keys are protected and can't be overwritten (at least
new assignment makes no difference)

Change-Id: Ie0c588a2d559bd38c68b3d018e547a036d83214c
Closes-Bug: #1889547
2020-07-30 20:18:07 +03:00
Kevin Carter 4ed672e399 Update config_template to use the builtin _get_remote_user function
This change will ensure the config_template module is able to pull the appropriate
remote use when creating a temp file or directory.

Related-Bug: #1884816
Change-Id: I36b13db4d0b722e7c6c7244ab142003193a56ffe
Signed-off-by: Kevin Carter <kecarter@redhat.com>
2020-06-29 13:05:57 +00:00
Zuul 51356b811c Merge "Stop to use the __future__ module." 2020-06-24 12:31:28 +00:00
Dmitriy Rabotyagov 4e6d993457 Make code compatible with older ansible versions
Change-Id: I42b04b5a70aeba0eba0ca9444079cddc0a95e2ae
2020-06-18 12:14:15 +03:00
Dmitriy Rabotyagov 170e46b44d Templar: use available_variables
This aims to resolve deprecation warning: set_available_variables is
being deprecated. Use "@available_variables.setter" instead

Change-Id: I95191ad596656051bd6a2ba8dd720823ea3434ec
2020-06-13 01:00:24 +03:00
Hervé Beraud d7bf68f694 Stop to use the __future__ module.
The __future__ module [1] was used in this context to ensure compatibility
between python 2 and python 3.

We previously dropped the support of python 2.7 [2] and now we only support
python 3 so we don't need to continue to use this module and the imports
listed below.

Imports commonly used and their related PEPs:
- `division` is related to PEP 238 [3]
- `print_function` is related to PEP 3105 [4]
- `unicode_literals` is related to PEP 3112 [5]
- `with_statement` is related to PEP 343 [6]
- `absolute_import` is related to PEP 328 [7]

[1] https://docs.python.org/3/library/__future__.html
[2] https://governance.openstack.org/tc/goals/selected/ussuri/drop-py27.html
[3] https://www.python.org/dev/peps/pep-0238
[4] https://www.python.org/dev/peps/pep-3105
[5] https://www.python.org/dev/peps/pep-3112
[6] https://www.python.org/dev/peps/pep-0343
[7] https://www.python.org/dev/peps/pep-0328

Change-Id: I1737d3d66046117a78e852133f7764f193a67e34
2020-06-02 19:32:36 +02:00
Kevin Carter abc807a70a
Add option to enable / disable the template engine
This change allows a deployer to selectively enable or disable the
template engine. This will allow folks to override the contents of
a configuration file via the `config_overrides` option but not render
any jinja related content within the file, except for what is provided
by the `config_overrides` option.

Change-Id: If226b4814a88b3bd7968b5e2ef64474710665ae0
Signed-off-by: Kevin Carter <kecarter@redhat.com>
2019-12-17 15:51:15 -06:00
Kevin Carter 80cc8ed4b1
Add traps for py3 and fix remote src
This change adds traps for the FileNotFound exception which is used instead
of OSError when a file is not found. This will ensure that that fall back
exception handeling works properly in py2 and py3.

The remote_src flag was not being respected with the new fallback options,
this change adds a check for remote_src so it works in normal and fallback
mode.

Change-Id: I136dd1f3220d3b004d5275a9fa15edea53d58f6b
Signed-off-by: Kevin Carter <kecarter@redhat.com>
2019-12-17 11:31:50 -06:00
Kevin Carter 420acdf0e4
Add fallback option when stating the source file
This change will now fall back to using the ansible core module
`stat` to stat a file when the local stat function fails for a
permissions error. This is being done because there is an issue
with action plugins not respecting "become: true" on local files
being written over a local connection.

Change-Id: I646f0688f1f4bf3cddaef9114dac51c2a807b25b
Signed-off-by: Kevin Carter <kecarter@redhat.com>
2019-12-05 13:01:24 -06:00
Zuul ffc97df5d3 Merge "Add the ability to parse list only files" 2019-07-27 21:25:20 +00:00
Dmitriy Rabotyagov b7f38639a2 Fix writing of keys without values
Sometimes there might be a key in configfile, that should be empty but
defined correctly. This patch fixes such behaviour and adds testing.

Change-Id: I693bc3a2aade7015d0465583001962df4ba53443
2019-07-26 17:07:07 +00:00
Kevin Carter c53966f310
Add the ability to parse list only files
This change will allow config_template to parse list only files
and extend or replace the content of the list based on the
selected module options.

Change-Id: I2a24033b0323bcc25bd1b50ffb4034441ab2f468
Signed-off-by: Kevin Carter <kecarter@redhat.com>
2019-07-25 16:37:56 -05:00
Kevin Carter 09c76e2380
Enhance the config_template comment parser
The config_template comment parser will now respect all comments in
INI files, as they are written. This will ensure spacing, paragraphs,
and other comments bits that may be in an INI file remain intact,
even when overriding options and sections. With this feature we'll
now be able to insert options in OSLO config generated files without
truncating or making a mess of the file structure.

This is an internal enhancement and requires no change from the
operator or from within any ansible task. To ensure enhanced comments
are working, new tests have been added which will run though all of
the `config_template` INI file functions using a mock service file.
Existing tests for the old comment structure has been removed.
This includes tests that were running redundant tasks for file
diffs or expected the only style, merged, comment layout. All of the
tests have been broken out into descriptive task files. This was
largely done for readability. It was difficult to see what tests
we had and how I needed to extend them to test the enhanced comments
functionality given the INI type tests were all thoughout the
`tests/test-common-tasks.yml`. Now that the files have been broken
out developers will easily be able to audit our tests making it
simple to extend.

Change-Id: Ia6cdc215c35fa9ac45b718c211616a9887a74e37
Signed-off-by: Kevin Carter <kecarter@redhat.com>
2019-07-23 12:49:41 -05:00
Kevin Carter 73aa099f0a Add option to use a remote source
The config template action plugin assumes that the local node is
supplying a configuration file that will be used to copy content
to a target node. This change gives the module the ability to
change configuration files on target nodes using all remote
sources. The new boolean option, `remote_src` has been added to
enable or disable this functionality. When enabled, the module
will retrieve content from the target node and load it as
"user_content". By pre-loading it in the user content the
module will function normally without any major structural
changes or performance impact.

Change-Id: Id9c7e16fb935c2da0b32b7cf53449f68bd1e9c89
Signed-off-by: Kevin Carter <kecarter@redhat.com>
2019-07-09 16:19:08 +00:00
Georgina 1769158a0e config_template does not respect yaml multiline as overrides
Added a flag called yml_multilines which when used will not split on
lines that have a newline UNLESS they have a comma.

Change-Id: I90364403e215a67b320dfc7e67a85d47c774e634
Partial-Bug: #1819974
2019-04-25 11:30:59 +00:00
Georgina Shippey 66a97f2b40 Remove whitespace before comments
Python 2.7 ConfigParser does not accept whitespace before comments.
To keep config files produced by ansible-config_temple parsable
we will strip out the whitespace preceding comments.

Change-Id: I39c88578d0a9e42fded7410d7eb4ec8bd415550c
Related-Bug: #1755821
2019-02-14 11:50:50 +00:00
cloudnull 78bcedc36e
Correct "changed" to work with the latest ansible
This commit adds a check for "changed" in the return from the copy
module. If the value is not possitive `rc['changed']` will be set
with the constant "changed" from within the action plugin.

Change-Id: Id65e6e57d174c4c25d99836b09ad049302d4e42c
Signed-off-by: cloudnull <kevin@cloudnull.com>
2019-02-05 00:13:12 -06:00
Jesse Pretorius 861e3a9806 Fix pep8 errors
There are several pep8 test failures which needed fixing.

Change-Id: I1692408544a6375c539702013bb975c46f070268
2018-10-30 18:27:05 +00:00
Michael Vollman b74d7acbf4 Compare dict vars to determine changed
Compare dict vars of before and after configuration to determine whether
the config keys or values have changed so a configuration file will not
be incorrectly marked as changed when only the ordering has changed.

Set diff return variable to a dict of changes applied.

Change-Id: Ie67119b1420936c8ed89f8338ea9dce4c47e185c
2018-09-24 20:08:51 -04:00
Zuul 058b5b5bd6 Merge "Replace unicode with six.text_type" 2018-07-10 22:13:14 +00:00
Vu Cong Tuan 1d402f078a Replace unicode with six.text_type
The unicode() built-in does not exist under Python 3 so use
six.text_type, which is set correctly to str or unicode, instead.

Change-Id: I9aa5df72b2248f9bd32e1c49118d0eb889a95691
2018-07-05 13:44:28 +07:00
Andy McCrae fcf669b78c Fix config_template to work with Ansible 2.6
In Ansible 2.6 the copy module now recognizes the "_original_basename"
option and not the "original_basename" option. In order for this module
to work with versions less than 2.6.0 and 2.6.0+ we need to allow an if
statement based on the version of Ansible being used.

Change-Id: I3f7a12dec77ea6e8e3c9fcae6ce9c162df57e50c
2018-07-04 12:22:02 +01:00
Andy McCrae 41ebaf72f9 Remove --diff mode support
In ansible 2.5 the way files are generated changes meaning that --diff
mode will stop working, and generate an error instead. Pending a better
solution, we should remove --diff mode.

Change-Id: I0d9b7baf08be00b978b8198f95db8962e7fa7787
2018-06-22 14:01:35 +01:00
Andy McCrae 4d93182786 Fix ordering for --diff mode setup
Due to the ordering of the vars passed to the _get_diff_data mode,
--diff mode would only work with --check activated, otherwise the file
used didn't exist and you would get an error similar to:

https://github.com/ceph/ceph-ansible/issues/2742

This PR allows this to work by configuring the diff data before running
the copy module, so the file exists.

Additionally, we should run with --diff mode enabled by default on tests
for this repository to ensure any functional issues with diff mode are
caught.

Change-Id: I020a181731c3ee87f04933f8c62213ea12cfa3a4
2018-06-20 14:04:06 +01:00
Andy McCrae 75cb8670fb Allow default section in ini to be specified
The default section in an ini file will always be 'DEFAULT', but for some
configuration files you may want to be able to set this to ensure
another section is at the top. 'global' for example, when working with
fio.

This PR adds the ability to set the section to be at the top, but will
default to 'DEFAULT' so no change will happen to existing tasks running
config_template.

Change-Id: Ifbc0b91aef46f3c2d98e73fdc9ab888244550bab
2018-05-09 12:57:33 +01:00
Andy McCrae fc1c9311a8 Fix py3 unicode issue
unicode is nolonger a type in py3 - instead the str type is used for
unicode, and the bytes type would cover the str type from py2.

To avoid having to version the code, we should try to use unicode,
except the NameError and continue as though it is py3 if that fails.

Additionally, this patch adds a test that will fail if you revert the
config_template.py back to it's original in py3.

Closes-bug: 1763422
Change-Id: Ifda972caada27ade2d80f77b3df70568406226ff
2018-04-25 12:03:03 +01:00
Zuul 5e8320a0cb Merge "Add support for --diff" 2018-04-03 08:05:46 +00:00
Zuul 906ef3c4b9 Merge "Retain the ordering when using with MultiStringOp" 2018-03-30 11:46:54 +00:00
Kevin Carter 0f91706487 Retain the ordering when using with MultiStringOp
When using config_template with a MultiStringOp the order was subject to
change because of the use of set theory. This change converts the use of
sets to tuples which allow us to retain the ability to override options
without losing the ordering of keys.

Change-Id: Id0bba86629a33ce74528c0bd51b11183f5315d86
Signed-off-by: Kevin Carter <kevin.carter@rackspace.com>
(cherry picked from commit 662a2805c0dce43a1efec31a85e09056207ec3d2)
2018-03-28 20:26:30 +00:00
Jean-Philippe Evrard bc707fe0fd Utilise sorted to ensure no random changes
As part of the ceph-ansible project, this change was put in:
ec042219e6
This was to handle the case where the config would change, initiating a
restart handler, even though no actual change happened.

We can juse use sorted() since we are passing back a list with .items()
which can then be sorted.

The above change didn't include a change to ensure the order of the
sections, which meant that although the individual items were sorted the
sections could change order causing unnecessary changes.

Additionally, this adds a test to retemplate the file 3 times, with a
lot more sections to ensure the order remains the same, this will fail
when the config template changes even though no changes have happened.

Change-Id: I7c7cd9ef1dd767a2a5d754ac4663cf8655dd60c2
(cherry-picked from plugins repository)
2018-03-27 11:14:58 -05:00
Andy McCrae 2c6ebef2a4 Fix pep8 for config_template.py
Gate tests will fail until we fix pep8 for config_template.py
This PR fixes the config_template module for pep8 compliance,
follows the latest PTI for docs compliance,
and re-enables the linters.

Change-Id: Ic288b462a53ac45983f47f29a7d5d8d48d0b9c0b
Co-Authored-By: Jean-Philippe Evrard <jean-philippe@evrard.me>
2018-03-26 12:53:57 +00:00
Andy McCrae a55d4a22b0 Add support for --diff
Change-Id: Idd7650a93df1676065ae7b944fab616de68ed4aa
2018-03-22 10:03:38 +00:00
Flavio Percoco 3f5a274374 Preserve comments in INI files
config_template currently strips comments out from the INI files. This
commit adds support for comments.

The default ConfigParser supports reading comments but it doesn't writes
them back. There are other libraries that have better support for
comments, however, adopting another library would require a bigger
refactor of this code.

Change-Id: I376be69cd2cf34dc147f9ade716791125a424e2c
2017-11-23 14:46:18 +01:00
Jimmy McCrory 7593eb3ba9 Remove plugin compatibility with Ansible v1.9
Ansible v1.9 was last used by OSA in Mitaka. The extra code to keep the
config_template and py_pkgs plugins compatible should no longer be
needed.

Change-Id: I14b32af1dc9bfe8b09c52f0205afaaad9139b58c
2017-09-12 10:41:49 -06:00
Jimmy McCrory ca0cf34163 Python3 compatibility for config_template overrides
Strings are used where they were encoded to bytes and cast back to
strings before. The StringIO module is imported to retain backwards
compatibility with Python 2.7. Also, the convert_2_string function is
removed and its usage replaced with Ansible's to_text function.

Change-Id: I396d96b8047e653f4526350375abc1f5f0dcd756
Fixes-Bug: 1711347
2017-09-06 15:05:49 -07:00
Kevin Carter 2d1dd94264
remove the use of iteritems
iteritems is a py2 callable and will break when using py3.

Change-Id: Icea444baea8f3597cb8dd24e3d61e9ed0926cdf5
Signed-off-by: Kevin Carter <kevin.carter@rackspace.com>
2017-08-02 15:04:48 -05:00
Jenkins c5d588fce9 Merge "Fix dumping hostvars to yaml from overrides" 2017-08-02 14:01:53 +00:00
Logan V 2a286fb0fb config_template python 3 compatibility
d.iteritems() -> d.items()

Refs:
http://legacy.python.org/dev/peps/pep-0469/
https://stackoverflow.com/questions/10458437/what-is-the-difference-between-dict-items-and-dict-iteritems
https://stackoverflow.com/questions/24189379/what-is-the-advantage-of-iteritems

Change-Id: Ie5a46ddea8927e4c6d585789fcc8a552943106be
2017-08-01 15:12:05 -05:00
Logan V 6d496d6c5b Fix dumping hostvars to yaml from overrides
Using AnsibleDumper adds support for dumping the internal ansible
data types that are used in hostvars.

Fixes-Bug: #1707265
Change-Id: Ie59bbef31306cd3e99aa827db875922e17084e83
2017-08-01 19:36:36 +00:00
Miguel Alex Cantu 76d5f02a32 Add ignore_none_type to config template
It is sometimes useful to tell ConfigTemplateParser to write
out options that are valueless and not suffixed with '=' or ':',
such as when overriding a my.cnf.

This commit adds the 'ignore_none_type' attribute to the config_template
module. If this attribute is set to false, then valueless options will be
written out to the resultant INI file as-is, without the '=' or ':' suffix.

Change-Id: I5c88b2019c01b44193a5d0df9299ecce6de52f01
Partial-Bug: #1693234
2017-06-26 19:12:43 +00:00
Miguel Alex Cantu 371cbc1fde Handle 'dict' type as input of 'content'
When users specificy the 'content' attribute when
using the config_template module, it's possible that
this input can be a dict. This commit handles
dictionary input, and dumps it accordingly based
on 'config_type'

Change-Id: I1f12810ad7ce5746f8938ec9d608c532ba530d58
2017-06-22 21:46:31 +00:00
Kevin Carter 80d59cdf96
Added yaml class dumper to support indentation
The default py yaml `safe_dumps` keeps to the yaml spec which states
incremental indentations are not required. Sadly, without the
indentations some consumers of yaml configurations files will break.
This commit adds an incremental indentation dumper to cater for all
yaml configuration consumers.

Change-Id: Ifa5bcc6ff9f0a9cb5e619e62d0356f5d6887ca6e
Signed-off-by: Kevin Carter <kevin.carter@rackspace.com>
2017-01-25 23:50:31 -06:00
Jean-Philippe Evrard 6d76bfe7df Cleanup temporary files after content argument usage
When using content: the config_template module will create a
temporary file. This patch ensures a deterministic path to the
temp file is used, and removes it at the end of the module
execution.

Change-Id: I3b3354178ba1a2b9c95decb3d3e0c9a1c2689056
2017-01-03 10:12:14 +00:00
Jean-Philippe Evrard 1f404d7f97 Introduce content argument
Copy module can take content argument, I think we should introduce this
feature to config_template. It brings the possibility to use lookups for
inline passing of content to template.

Example, we could use:

config_template:
  content: "{{ lookup('file',<file>) }}" to replace current behavior

or:

config_template:
  content: "{{ lookup('url','<url>',wantlist=True) | join ('\n') }}"

to bring inline templating of external sources.

Change-Id: Id5b2743d309f0313603afbbf84279ce0b1e49cfb
Signed-off-by: Jean-Philippe Evrard <jean-philippe.evrard@rackspace.co.uk>
2016-12-15 12:08:17 +00:00
Jean-Philippe Evrard 5bf7a5c3f1 Adapt to_unicode and to_bytes for ansible 2.2
The config_template is using the old ansible objects, this should
make it work with the newest standards.

Change-Id: I9159be497c201ca6c2f558336843be5175f0ee2c
Closes-Bug: 1640458
2016-11-09 12:00:23 +00:00
Kevin Carter 5f42556157 Updated for deprecated "ansible_ssh_user"
Ansible has changed the variable ansible_ssh_user to ansible_user
this change adds support for the option ansible_user while 
maintaining compatibility with the legacy option.

Change-Id: I032a3562ccebbbf8f7248bfbec1e2c59b452587b
Signed-off-by: Kevin Carter <kevin.carter@rackspace.com>
2016-10-25 21:22:21 +00:00
Jimmy McCrory 2f6cac2cf6 Remove deprecated first_available_file
The first_available_file option on tasks has been deprecated in
Ansible 2, and will be removed in 2.2. Remove its use in the
config_template plugin similarly to its removal from the upstream
template plugin [0].

[0] https://github.com/ansible/ansible/pull/17643

Change-Id: Icd4f70706979a3f59dd93f116d1c5ef96d66edbe
2016-10-19 11:15:44 -07:00
Kevin Carter 80beba88da Add option to toggle list extensions
By default a list item in a JSON or YAML format will extend if a its already
defined in the target template and a config_override using a list is being set
for the existing "key".

This change allows that functionality to be toggled on or off. The boolean
option to enable list extension is ``list_extend``.

To maintain the API this feature will remain True by default.

Co-Authored-By: Travis Truman <travis_truman@cable.comcast.com>
Change-Id: I97e06aef2cc778f048f3d6863fe61d10eddb8602
Signed-off-by: Kevin Carter <kevin.carter@rackspace.com>
2016-09-09 16:15:00 -05:00
Kevin Carter f946160dd0
Added option to support case-insensitive keys
This change allows keys in INI format to be any case.
The default ConfigParse module sets this to be lower
however in some cases keys are needed to be upper and/or
mixed.

Change-Id: I4e0dedb1b73ee596929bd425af6b0aaefd3a6c27
Signed-off-by: Kevin Carter <kevin.carter@rackspace.com>
2016-04-21 16:09:18 -05:00