2015-07-11 09:29:18 +02:00
#!/usr/bin/python
2014-09-30 10:25:40 +02:00
# -*- coding: utf-8 -*-
2015-05-22 21:44:32 +02:00
# Copyright 2015 Cristian van Ee <cristian at cvee.org>
# Copyright 2015 Igor Gnatenko <i.gnatenko.brain@gmail.com>
2014-09-30 10:25:40 +02:00
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
#
DOCUMENTATION = '''
- - -
module : dnf
2015-02-04 04:31:46 +01:00
version_added : 1.9
2014-09-30 10:25:40 +02:00
short_description : Manages packages with the I ( dnf ) package manager
description :
- Installs , upgrade , removes , and lists packages and groups with the I ( dnf ) package manager .
options :
name :
description :
- " Package name, or package specifier with version, like C(name-1.0). When using state=latest, this can be ' * ' which means run: dnf -y update. You can also pass a url or a local path to a rpm file. "
required : true
default : null
aliases : [ ]
2015-07-11 09:29:18 +02:00
2014-09-30 10:25:40 +02:00
list :
description :
- Various ( non - idempotent ) commands for usage with C ( / usr / bin / ansible ) and I ( not ) playbooks . See examples .
required : false
default : null
2015-07-11 09:29:18 +02:00
2014-09-30 10:25:40 +02:00
state :
description :
- Whether to install ( C ( present ) , C ( latest ) ) , or remove ( C ( absent ) ) a package .
required : false
choices : [ " present " , " latest " , " absent " ]
default : " present "
2015-07-11 09:29:18 +02:00
2014-09-30 10:25:40 +02:00
enablerepo :
description :
- I ( Repoid ) of repositories to enable for the install / update operation .
These repos will not persist beyond the transaction .
When specifying multiple repos , separate them with a " , " .
required : false
default : null
aliases : [ ]
2015-02-04 04:31:46 +01:00
2014-09-30 10:25:40 +02:00
disablerepo :
description :
- I ( Repoid ) of repositories to disable for the install / update operation .
These repos will not persist beyond the transaction .
When specifying multiple repos , separate them with a " , " .
required : false
default : null
aliases : [ ]
conf_file :
description :
- The remote dnf configuration file to use for the transaction .
required : false
default : null
aliases : [ ]
disable_gpg_check :
description :
- Whether to disable the GPG checking of signatures of packages being
installed . Has an effect only if state is I ( present ) or I ( latest ) .
required : false
default : " no "
choices : [ " yes " , " no " ]
aliases : [ ]
notes : [ ]
# informational: requirements for nodes
2015-05-22 21:44:32 +02:00
requirements :
- " python >= 2.6 "
2015-10-05 06:32:10 +02:00
- python - dnf
2015-05-22 21:44:32 +02:00
author :
2015-07-11 09:29:18 +02:00
- ' " Igor Gnatenko (@ignatenkobrain) " <i.gnatenko.brain@gmail.com> '
2015-05-22 21:44:32 +02:00
- ' " Cristian van Ee (@DJMuggs) " <cristian at cvee.org> '
2014-09-30 10:25:40 +02:00
'''
EXAMPLES = '''
- name : install the latest version of Apache
dnf : name = httpd state = latest
- name : remove the Apache package
dnf : name = httpd state = absent
- name : install the latest version of Apache from the testing repo
dnf : name = httpd enablerepo = testing state = present
- name : upgrade all packages
dnf : name = * state = latest
- name : install the nginx rpm from a remote repo
dnf : name = http : / / nginx . org / packages / centos / 6 / noarch / RPMS / nginx - release - centos - 6 - 0. el6 . ngx . noarch . rpm state = present
- name : install nginx rpm from a local file
dnf : name = / usr / local / src / nginx - release - centos - 6 - 0. el6 . ngx . noarch . rpm state = present
- name : install the ' Development tools ' package group
dnf : name = " @Development tools " state = present
'''
2015-07-11 09:29:18 +02:00
import os
2014-09-30 10:25:40 +02:00
2015-07-11 09:29:18 +02:00
try :
import dnf
from dnf import cli , const , exceptions , subject , util
HAS_DNF = True
except ImportError :
HAS_DNF = False
def _fail_if_no_dnf ( module ) :
""" Fail if unable to import dnf. """
if not HAS_DNF :
module . fail_json (
msg = " `python-dnf` is not installed, but it is required for the Ansible dnf module. " )
def _configure_base ( module , base , conf_file , disable_gpg_check ) :
""" Configure the dnf Base object. """
conf = base . conf
# Turn off debug messages in the output
conf . debuglevel = 0
# Set whether to check gpg signatures
conf . gpgcheck = not disable_gpg_check
# Don't prompt for user confirmations
conf . assumeyes = True
# Change the configuration file path if provided
if conf_file :
# Fail if we can't read the configuration file.
if not os . access ( conf_file , os . R_OK ) :
module . fail_json (
msg = " cannot read configuration file " , conf_file = conf_file )
else :
conf . config_file_path = conf_file
# Read the configuration file
conf . read ( )
def _specify_repositories ( base , disablerepo , enablerepo ) :
""" Enable and disable repositories matching the provided patterns. """
base . read_all_repos ( )
repos = base . repos
# Disable repositories
for repo_pattern in disablerepo :
for repo in repos . get_matching ( repo_pattern ) :
repo . disable ( )
# Enable repositories
for repo_pattern in enablerepo :
for repo in repos . get_matching ( repo_pattern ) :
repo . enable ( )
def _base ( module , conf_file , disable_gpg_check , disablerepo , enablerepo ) :
""" Return a fully configured dnf Base object. """
_fail_if_no_dnf ( module )
base = dnf . Base ( )
_configure_base ( module , base , conf_file , disable_gpg_check )
_specify_repositories ( base , disablerepo , enablerepo )
base . fill_sack ( )
return base
def _package_dict ( package ) :
""" Return a dictionary of information for the package. """
# NOTE: This no longer contains the 'dnfstate' field because it is
# already known based on the query type.
result = {
' name ' : package . name ,
' arch ' : package . arch ,
' epoch ' : str ( package . epoch ) ,
' release ' : package . release ,
' version ' : package . version ,
' repo ' : package . repoid }
result [ ' nevra ' ] = ' {epoch} : {name} - {version} - {release} . {arch} ' . format (
* * result )
return result
def list_items ( module , base , command ) :
""" List package info based on the command. """
# Rename updates to upgrades
if command == ' updates ' :
command = ' upgrades '
# Return the corresponding packages
if command in [ ' installed ' , ' upgrades ' , ' available ' ] :
results = [
_package_dict ( package )
for package in getattr ( base . sack . query ( ) , command ) ( ) ]
# Return the enabled repository ids
elif command in [ ' repos ' , ' repositories ' ] :
results = [
{ ' repoid ' : repo . id , ' state ' : ' enabled ' }
for repo in base . repos . iter_enabled ( ) ]
# Return any matching packages
2014-09-30 10:25:40 +02:00
else :
2015-07-11 09:29:18 +02:00
packages = subject . Subject ( command ) . get_best_query ( base . sack )
results = [ _package_dict ( package ) for package in packages ]
module . exit_json ( results = results )
2014-09-30 10:25:40 +02:00
2015-07-09 23:00:22 +02:00
2015-07-11 09:29:18 +02:00
def _mark_package_install ( module , base , pkg_spec ) :
2015-07-09 23:00:22 +02:00
""" Mark the package for install. """
try :
2015-07-11 09:29:18 +02:00
base . install ( pkg_spec )
except exceptions . MarkingError :
2015-09-22 06:50:52 +02:00
module . fail_json ( msg = " No package {} available. " . format ( pkg_spec ) )
2015-07-11 09:29:18 +02:00
def ensure ( module , base , state , names ) :
2016-02-02 19:39:19 +01:00
allow_erasing = False
2015-07-11 09:29:18 +02:00
if names == [ ' * ' ] and state == ' latest ' :
base . upgrade_all ( )
2015-07-09 23:00:22 +02:00
else :
2015-07-11 09:29:18 +02:00
pkg_specs , group_specs , filenames = cli . commands . parse_spec_group_file (
names )
if group_specs :
base . read_comps ( )
groups = [ ]
for group_spec in group_specs :
group = base . comps . group_by_pattern ( group_spec )
if group :
groups . append ( group )
else :
module . fail_json (
msg = " No group {} available. " . format ( group_spec ) )
2015-07-09 23:00:22 +02:00
if state in [ ' installed ' , ' present ' ] :
# Install files.
for filename in filenames :
2015-07-11 09:29:18 +02:00
base . package_install ( base . add_remote_rpm ( filename ) )
2015-07-09 23:00:22 +02:00
# Install groups.
2015-07-11 09:29:18 +02:00
for group in groups :
base . group_install ( group , const . GROUP_PACKAGE_TYPES )
2015-07-09 23:00:22 +02:00
# Install packages.
for pkg_spec in pkg_specs :
2015-07-11 09:29:18 +02:00
_mark_package_install ( module , base , pkg_spec )
2015-07-09 23:00:22 +02:00
elif state == ' latest ' :
2015-07-11 09:29:18 +02:00
# "latest" is same as "installed" for filenames.
for filename in filenames :
base . package_install ( base . add_remote_rpm ( filename ) )
for group in groups :
try :
base . group_upgrade ( group )
except exceptions . CompsError :
# If not already installed, try to install.
base . group_install ( group , const . GROUP_PACKAGE_TYPES )
2015-07-09 23:00:22 +02:00
for pkg_spec in pkg_specs :
2016-02-12 15:51:48 +01:00
# best effort causes to install the latest package
# even if not previously installed
base . conf . best = True
base . install ( pkg_spec )
2015-07-09 23:00:22 +02:00
2015-07-11 09:29:18 +02:00
else :
2016-02-02 19:39:19 +01:00
# state == absent
2015-07-11 09:29:18 +02:00
if filenames :
module . fail_json (
msg = " Cannot remove paths -- please specify package name. " )
2015-05-22 22:04:16 +02:00
2015-07-11 09:29:18 +02:00
installed = base . sack . query ( ) . installed ( )
for group in groups :
if installed . filter ( name = group . name ) :
base . group_remove ( group )
for pkg_spec in pkg_specs :
if installed . filter ( name = pkg_spec ) :
base . remove ( pkg_spec )
2016-02-02 19:39:19 +01:00
# Like the dnf CLI we want to allow recursive removal of dependent
# packages
allow_erasing = True
2015-05-22 22:04:16 +02:00
2016-02-02 19:39:19 +01:00
if not base . resolve ( allow_erasing = allow_erasing ) :
2015-07-11 09:29:18 +02:00
module . exit_json ( msg = " Nothing to do " )
else :
if module . check_mode :
module . exit_json ( changed = True )
base . download_packages ( base . transaction . install_set )
base . do_transaction ( )
response = { ' changed ' : True , ' results ' : [ ] }
for package in base . transaction . install_set :
response [ ' results ' ] . append ( " Installed: {} " . format ( package ) )
for package in base . transaction . remove_set :
response [ ' results ' ] . append ( " Removed: {} " . format ( package ) )
2014-09-30 10:25:40 +02:00
2015-07-11 09:29:18 +02:00
module . exit_json ( * * response )
2014-09-30 10:25:40 +02:00
2015-07-11 09:29:18 +02:00
def main ( ) :
""" The main function. """
2014-09-30 10:25:40 +02:00
module = AnsibleModule (
2015-07-11 09:29:18 +02:00
argument_spec = dict (
name = dict ( aliases = [ ' pkg ' ] , type = ' list ' ) ,
state = dict (
default = ' installed ' ,
choices = [
' absent ' , ' present ' , ' installed ' , ' removed ' , ' latest ' ] ) ,
enablerepo = dict ( type = ' list ' , default = [ ] ) ,
disablerepo = dict ( type = ' list ' , default = [ ] ) ,
2014-09-30 10:25:40 +02:00
list = dict ( ) ,
conf_file = dict ( default = None ) ,
2015-07-11 09:29:18 +02:00
disable_gpg_check = dict ( default = False , type = ' bool ' ) ,
2014-09-30 10:25:40 +02:00
) ,
2015-07-11 09:29:18 +02:00
required_one_of = [ [ ' name ' , ' list ' ] ] ,
mutually_exclusive = [ [ ' name ' , ' list ' ] ] ,
supports_check_mode = True )
2014-09-30 10:25:40 +02:00
params = module . params
if params [ ' list ' ] :
2015-11-01 21:58:20 +01:00
base = _base (
module , params [ ' conf_file ' ] , params [ ' disable_gpg_check ' ] ,
params [ ' disablerepo ' ] , params [ ' enablerepo ' ] )
2015-07-11 09:29:18 +02:00
list_items ( module , base , params [ ' list ' ] )
2014-09-30 10:25:40 +02:00
else :
2015-11-01 21:58:20 +01:00
# Note: base takes a long time to run so we want to check for failure
# before running it.
if not util . am_i_root ( ) :
module . fail_json ( msg = " This command has to be run under the root user. " )
base = _base (
module , params [ ' conf_file ' ] , params [ ' disable_gpg_check ' ] ,
params [ ' disablerepo ' ] , params [ ' enablerepo ' ] )
2015-07-11 09:29:18 +02:00
ensure ( module , base , params [ ' state ' ] , params [ ' name ' ] )
2014-09-30 10:25:40 +02:00
# import module snippets
from ansible . module_utils . basic import *
2015-05-22 21:44:32 +02:00
if __name__ == ' __main__ ' :
main ( )