-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replaced awk with the {apt,yum}_madison modules
- Loading branch information
Showing
6 changed files
with
168 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#!/usr/bin/python | ||
# -*- coding: utf-8 -*- | ||
|
||
# Originally published at | ||
# https://gist.github.com/atosatto/cb8d5b1866357cc491cefe840c160575 | ||
|
||
import apt | ||
|
||
DOCUMENTATION = """ | ||
--- | ||
module: apt_madison | ||
short_description: Extract the available versions of a package from the APT cache. | ||
description: | ||
- This module extracts available versions of a package from the APT cache. | ||
options: | ||
name: | ||
required: true | ||
description: | ||
- Name of the package. | ||
aliases: [ pkg, package ] | ||
update_cache: | ||
required: false | ||
default: no | ||
choices: [ no, yes ] | ||
description: | ||
- Run the equivalent of `apt-get update` before the operation. | ||
requirements: [ python-apt ] | ||
""" | ||
|
||
def main(): | ||
module = AnsibleModule( | ||
argument_spec=dict( | ||
package=dict(required=True, aliases=['pkg', 'name'], type='str'), | ||
update_cache = dict(default=False, aliases=['update-cache'], type='bool') | ||
)) | ||
|
||
cache = apt.Cache() | ||
if module.params.get('update_cache'): | ||
cache.update() | ||
|
||
pkg_name = module.params.get('package') | ||
pkg_versions = None | ||
try: | ||
pkg_versions = cache[pkg_name].versions | ||
except: | ||
module.fail_json(msg="Could not find package %s in the apt cache." % pkg_name) | ||
|
||
retvals = [] | ||
for v in pkg_versions: | ||
for repo in v.origins: | ||
retvals.append({ | ||
"name": pkg_name, | ||
"version": v.version, | ||
"architecture": v.architecture, | ||
"raw_description": v.raw_description, | ||
"size": v.size, | ||
"repo_name": repo.origin, | ||
"repo_archive": repo.archive, | ||
"repo_component": repo.component, | ||
"repo_site": repo.site, | ||
"repo_label": repo.label, | ||
"repo_trusted": repo.trusted | ||
}) | ||
|
||
module.exit_json(changed=False, versions=retvals) | ||
|
||
# import module snippets | ||
from ansible.module_utils.basic import * | ||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#!/usr/bin/python | ||
# -*- coding: utf-8 -*- | ||
|
||
# Originally published at | ||
# https://gist.github.com/atosatto/12c01e8d22533884c87a755989cd36eb | ||
|
||
DOCUMENTATION = """ | ||
--- | ||
module: yum_madison | ||
short_description: Extract the available versions of a package from YUM. | ||
description: | ||
- This module extracts the available versions of a package from YUM. | ||
options: | ||
name: | ||
required: true | ||
description: | ||
- Name of the package. | ||
aliases: [ pkg, package ] | ||
update_cache: | ||
required: false | ||
default: no | ||
choices: [ no, yes ] | ||
description: | ||
- Force updating the cache. | ||
requirements: [ yum ] | ||
""" | ||
|
||
def main(): | ||
module = AnsibleModule( | ||
argument_spec=dict( | ||
package=dict(required=True, aliases=['pkg', 'name'], type='str'), | ||
update_cache = dict(default=False, aliases=['update-cache'], type='bool') | ||
)) | ||
|
||
yumbin = module.get_bin_path('yum') | ||
pkg_name = module.params.get('package') | ||
yum_basecmd = [yumbin, '-d', '2', '-y'] | ||
|
||
if module.params.get('update_cache'): | ||
module.run_command(yum_basecmd + ['makecache']) | ||
|
||
yum_madcmd = yum_basecmd + ['list', '--showduplicates', '--quiet', pkg_name ] | ||
rc, out, err = module.run_command(yum_madcmd) | ||
|
||
if rc != 0: | ||
module.fail_json(msg='Error from yum: %s: %s' % (yum_madcmd, err)) | ||
out_lines = [ o for o in out.split('\n') if o.strip() ] | ||
|
||
retvals = [] | ||
state = "" | ||
for l in out_lines: | ||
|
||
if l.startswith(pkg_name): | ||
p = l.split() | ||
retvals.append({ | ||
"name": pkg_name, | ||
"version": p[1], | ||
"architecture": p[0].split('.')[-1], | ||
"repo_name": p[2] | ||
}) | ||
|
||
module.exit_json(changed=False, versions=retvals) | ||
|
||
# import module snippets | ||
from ansible.module_utils.basic import * | ||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,17 @@ | ||
--- | ||
|
||
- name: Extract the PowerDNS Recursor Version from APT | ||
shell: "apt-cache madison pdns-recursor | awk '/{%if pdns_rec_repo_provider == 'powerdns'%}repo.powerdns.com{%endif%}/ {print $3}' | head -1" | ||
register: pdns_recursor_version_result | ||
apt_madison: | ||
name: "pdns-recursor" | ||
update_cache: yes | ||
changed_when: False | ||
register: pdns_recursor_version_result | ||
|
||
- name: Export the pdns_recursor_version variable for Debian | ||
set_fact: | ||
pdns_recursor_version: | | ||
{% if pdns_rec_repo_provider == 'powerdns' %} | ||
{{ pdns_recursor_version_result.versions | selectattr("repo_name", "equalto", "PowerDNS") | map(attribute='version') | first }} | ||
{% else %} | ||
{{ pdns_recursor_version_result.versions | map(attribute='version') | first }} | ||
{% endif %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,21 @@ | ||
--- | ||
|
||
## NB: The `which yum` is needed to suppress the annoying | ||
## "[WARNING]: Consider using yum module rather than running yum" | ||
## message. | ||
# XXX: This should be definitively improved once issue | ||
# https://github.com/ansible/ansible-modules-core/issues/1706 | ||
# will get solved | ||
|
||
- name: Extract the PowerDNS Recursor Version from YUM | ||
shell: "`which yum` list pdns-recursor 2>&1 | awk '/pdns-recursor/ {print $2}'" | ||
register: pdns_recursor_version_result | ||
yum_madison: | ||
name: "pdns-recursor" | ||
update_cache: yes | ||
changed_when: False | ||
register: pdns_recursor_version_result | ||
|
||
- name: Export the pdns_recursor_version variable for RedHat | ||
set_fact: | ||
pdns_recursor_version: | | ||
{% if pdns_rec_repo_provider == 'powerdns' %} | ||
{{ pdns_recursor_version_result.versions | selectattr("repo_name", "match", "^powerdns") | map(attribute='version') | first }} | ||
{% else %} | ||
{{ pdns_recursor_version_result.versions | map(attribute='version') | first }} | ||
{% endif %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters