Skip to content

Commit

Permalink
Replaced awk with the {apt,yum}_madison modules
Browse files Browse the repository at this point in the history
  • Loading branch information
atosatto committed Sep 9, 2016
1 parent 0dcaa4a commit 7a372fa
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 12 deletions.
70 changes: 70 additions & 0 deletions library/apt_madison
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()
67 changes: 67 additions & 0 deletions library/yum_madison
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()
15 changes: 13 additions & 2 deletions tasks/inspect-Debian.yml
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 %}
22 changes: 17 additions & 5 deletions tasks/inspect-RedHat.yml
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 %}
4 changes: 0 additions & 4 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@

- include: "inspect-{{ ansible_os_family }}.yml"

- name: Export the pdns_recursor_version variable
set_fact:
pdns_recursor_version: "{{ pdns_recursor_version_result['stdout'].split('-')[0] }}"

- include: configure.yml
tags:
- conf
Expand Down
2 changes: 1 addition & 1 deletion tasks/repo-Debian.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
apt_repository:
repo: "{{ pdns_rec_apt_repo }}"

- name: Pin the PowerDNS Recursor package the PowerDNS APT Repository
- name: Pin the PowerDNS Recursor to the PowerDNS APT Repository
template:
src: pdns-recursor.pin
dest: /etc/apt/preferences.d/pdns-recursor

0 comments on commit 7a372fa

Please sign in to comment.