New blockfromfile module #962

Open
wants to merge 8 commits into
from

Projects

None yet

5 participants

@EvanK
Contributor
EvanK commented Sep 11, 2015

A module for extracting values (via regexp pattern) from an existing file on remote nodes. Should fail silently by default (or loudly if desired), when said file doesn't exist.

The use case

I have a group of servers using keepalived to ping each other and elect a master for the group. They find each other via a list of peer ip addresses, and hold election based on differing numeric priorities.

The peer lists and priorities are empty/zeroed for newly provisioned servers, and a separate tool calculates and overwrites them after provisioning.

Should I re-provision an existing server, I need to carry over those specific config values to the regenerated config file, rather than emptying/zeroing them again.

The stopgap solution

I've been accomplishing this with a custom python script that is (1) copied to the remote node, (2) made executable, (3) run, and (4) its output registered to a variable. I then use the registered variable in a conditional of the config file template.

Said custom script literally just slurps the file and prints any groups returned from re.search.

The module in use

I've simplified it with the module in question, so my playbook went from this:

- copy: >
    src=unicast-peers.py
    dest=/tmp/unicast-peers.py
    mode=0755
- command: /tmp/unicast-peers.py
  register: keepalive_peers
- command: rm -f /tmp/unicast-peers.py
- debug: var=keepalive_peers.stdout

To this:

- blockfromfile: >
    src=/etc/keepalived/keepalived.conf
    regexp="^\s*unicast_peer\s*\{\n*(?P<peers>[^}]*?)\n*\s*\}"
  register: keepalive_peers
- debug: var=keepalive_peers.matches.0.named_groups.peers
@gregdek
Contributor
gregdek commented Sep 15, 2015

Thanks for submitting this new module.

To help facilitate reviews, we’ve broadened the number of people who can approve modules for inclusion into the Extras repository. The list of official reviewers can be found here:

https://github.com/ansible/ansible-modules-extras/blob/devel/REVIEWERS.md

Our new policy is that if a new module is reviewed and approved by at least two official module reviewers, and receives no -1 votes, the module will be approved for inclusion. We will be asking the community of reviewers to take a look at these modules on a regular basis.

To ensure that your module has the best chance of being approved, please double-check that you adhere to the Ansible module guidelines: http://docs.ansible.com/developing_modules.html#module-checklist

@robynbergeron
Contributor

Hi -- updating this PR with the new modules reviewer guidelines, which will hopefully help move this module along to get reviews for inclusion.

Thanks for submitting this new module to Ansible Extras! This module is now in community review, a process that is open to all Ansible users. In order for this module to be approved, it must gain the following votes, which can be contributed by anyone other than the author of the new module:

“works_for_me”: If you have tested the module thoroughly, including testing of all of the module’s options, and if the module works for you, please add “works_for_me” in the comments.

“passes_guidelines”: If you have gone through the module guidelines and the module meets all of the requirements, please add “passes_guidelines” in the comments. Guidelines are available here: http://docs.ansible.com/developing_modules.html#module-checklist

“needs_revision”: If the module fails to work for you, or if it doesn’t meet guidelines, please add “needs_revision” in the comments with details about what needs to be fixed.

When a module has both “works_for_me” and “passes_guidelines” tags, we will promote the module for inclusion in Ansible Extras. At this point, you will be expected to maintain the module by fixing bugs and evaluating pull requests in a timely manner.

More information about the review process can be seen here:
https://github.com/ansible/ansible-modules-extras/blob/devel/REVIEWERS.md

Thanks again for submitting your Ansible module!

@wkeeling
Contributor

Hi,

I have tested this module using the latest devel on Ubuntu 14.04 LTS. I tested:

  • using a valid src with a valid matching regexp => Received a success response containing the matches
  • using a valid src with a valid unmatching regexp => Received a success response containing 0 matches
  • using an invalid src but valid regexp with fail_on_missing = False => Received a success response with message "Source xxx does not exist"
  • using an invalid src but valid regexp with fail_on_missing = True => Received a failure response with message "Source xxx does not exist"
  • using a valid src but invalid regexp (containing syntax error) => Received an error response

So this behaved as I would expect.

works_for_me

@gregdek
Contributor
gregdek commented Mar 24, 2016

Thanks @EvanK for this PR. This PR requires revisions, either because it fails to build or by reviewer request. Please make the suggested revisions. When you are done, please comment with text 'ready_for_review' and we will put this PR back into review.

@EvanK
Contributor
EvanK commented Mar 25, 2016

Merged latest upstream devel, and added RETURN documentation.

ready_for_review

@gregdek
Contributor
gregdek commented Mar 29, 2016

Thanks @EvanK for this new module. When this module receives 'shipit' comments from two community members and any 'needs_revision' comments have been resolved, we will mark for inclusion.

[This message brought to you by your friendly Ansibull-bot.]

@gregdek
Contributor
gregdek commented May 5, 2016

Thanks @EvanK for this PR. This PR requires revisions, either because it fails to build or by reviewer request. Please make the suggested revisions. When you are done, please comment with text 'ready_for_review' and we will put this PR back into review.

[This message brought to you by your friendly Ansibull-bot.]

@EvanK
Contributor
EvanK commented May 6, 2016

ready_for_review

@gregdek
Contributor
gregdek commented May 6, 2016

Thanks @EvanK for this new module. When this module receives 'shipit' comments from two community members and any 'needs_revision' comments have been resolved, we will mark for inclusion.

[This message brought to you by your friendly Ansibull-bot.]

@ansibot
ansibot commented Dec 7, 2016

This repository has been locked. All new issues and pull requests should be filed in https://github.com/ansible/ansible

Please read through the repomerge page in the dev guide. The guide contains links to tools which automatically move your issue or pull request to the ansible/ansible repo.

@EvanK EvanK referenced this pull request in ansible/ansible Dec 11, 2016
Open

New blockfromfile module #19191

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment