diff options
| author | Sam Yaple <sam@yaple.net> | 2015-07-05 07:03:12 +0000 |
|---|---|---|
| committer | Sam Yaple <sam@yaple.net> | 2015-07-06 07:26:00 +0000 |
| commit | 39e6075f2902a56ce133d5f80703ce0e486d7446 (patch) | |
| tree | b3d364ceaa768e12f1aa5a0564ab6bcbda0f44eb | |
| parent | 1c5988e642d1ca938f82f0b4546778c53abb56c8 (diff) | |
| download | kolla-39e6075f2902a56ce133d5f80703ce0e486d7446.tar.gz kolla-39e6075f2902a56ce133d5f80703ce0e486d7446.zip | |
make merge_configs idempotent
merge_configs can now check if the destination file has the same content
will be written. This information is used to inform ansible no change has
occured
Closes-Bug: 1471514
Change-Id: I78bce04505349d5aafbb027fd3f7d76ab6eccf6a
| -rw-r--r-- | ansible/library/merge_configs | 23 |
1 files changed, 16 insertions, 7 deletions
diff --git a/ansible/library/merge_configs b/ansible/library/merge_configs index b1baeb3d1..61d667be7 100644 --- a/ansible/library/merge_configs +++ b/ansible/library/merge_configs @@ -14,9 +14,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -# TODO(SamYaple): Provide idempotency for module (Note to self: pull logic from -# pervious bslurp module in yaodu) - DOCUMENTATION = ''' --- module: merge_configs @@ -54,6 +51,8 @@ Merge multiple configs: ''' import ConfigParser +from hashlib import sha1 +from StringIO import StringIO def main(): module = AnsibleModule( @@ -67,17 +66,27 @@ def main(): sources = module.params.pop('sources') dest = module.params.pop('dest') + changed = False + dest_digest = None + fakedest = StringIO() + config = ConfigParser.ConfigParser() for source_file in sources: config.read(source_file) - with open(dest, 'wb') as dest_file: - config.write(dest_file) + if os.path.exists(dest) and os.access(dest, os.R_OK): + config.write(fakedest) + with open(dest, 'rb') as f: + dest_digest = sha1(f.read()).hexdigest() + + if dest_digest != sha1(fakedest.getvalue()).hexdigest(): + changed = True + with open(dest, 'wb') as f: + config.write(f) - module.exit_json(changed=True) + module.exit_json(changed=changed) except Exception, e: - changed = True module.exit_json(failed=True, changed=changed, msg=repr(e)) |
