Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
ansible
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
ansible
Commits
58618474
Commit
58618474
authored
Jan 24, 2013
by
Stephen Fromm
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1907 from sfromm/issue1888
Update apt_repository to query if repo is already configured
parents
688524dd
e0741e78
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
49 additions
and
16 deletions
+49
-16
library/apt_repository
+49
-16
No files found.
library/apt_repository
View file @
58618474
...
...
@@ -52,10 +52,18 @@ examples:
description: Add nginx stable repository from PPA
- code: "apt_repository: repo='deb http://archive.canonical.com/ubuntu hardy partner'"
description: Add specified repository into sources.
requirements: [ python-apt ]
'''
import
platform
try
:
import
apt
import
apt_pkg
HAVE_PYAPT
=
True
except
ImportError
:
HAVE_PYAPT
=
False
APT
=
"/usr/bin/apt-get"
ADD_APT_REPO
=
'add-apt-repository'
...
...
@@ -64,6 +72,16 @@ def check_cmd_needs_y():
return
True
return
False
def
repo_exists
(
module
,
repo
):
configured
=
False
slist
=
apt_pkg
.
SourceList
()
if
not
slist
.
read_main_list
():
module
.
fail_json
(
msg
=
"Failed to parse sources.list"
)
for
metaindex
in
slist
.
list
:
if
repo
in
metaindex
.
uri
:
configured
=
True
return
configured
def
main
():
add_apt_repository
=
None
...
...
@@ -74,6 +92,9 @@ def main():
module
=
AnsibleModule
(
argument_spec
=
arg_spec
)
if
not
HAVE_PYAPT
:
module
.
fail_json
(
msg
=
"Could not import python modules: apt, apt_pkg. Please install python-apt package."
)
add_apt_repository
=
module
.
get_bin_path
(
ADD_APT_REPO
,
True
)
if
check_cmd_needs_y
():
add_apt_repository
+=
' -y'
...
...
@@ -81,30 +102,42 @@ def main():
repo
=
module
.
params
[
'repo'
]
state
=
module
.
params
[
'state'
]
cmd
=
'
%
s "
%
s" --remove'
%
(
add_apt_repository
,
repo
)
rc
,
out
,
err
=
module
.
run_command
(
cmd
)
existed
=
'Error'
not
in
out
if
state
==
'absent'
:
if
not
existed
:
module
.
exit_json
(
changed
=
False
,
repo
=
repo
,
state
=
state
)
else
:
module
.
exit_json
(
changed
=
True
,
repo
=
repo
,
state
=
state
)
cmd
=
'
%
s "
%
s"'
%
(
add_apt_repository
,
repo
)
rc
,
out
,
err
=
module
.
run_command
(
cmd
)
changed
=
rc
==
0
and
not
existed
repo_url
=
repo
if
'ppa'
in
repo_url
:
# looks like ppa:nginx/stable
repo_url
=
repo
.
split
(
':'
)[
1
]
elif
len
(
repo_url
.
split
(
' '
))
>
1
:
# could be:
# http://myserver/path/to/repo free non-free
# deb http://myserver/path/to/repo free non-free
for
i
in
repo_url
.
split
():
if
'http'
in
i
:
repo_url
=
i
exists
=
repo_exists
(
module
,
repo_url
)
rc
=
0
out
=
''
err
=
''
if
state
==
'absent'
and
exists
:
cmd
=
'
%
s "
%
s" --remove'
%
(
add_apt_repository
,
repo
)
rc
,
out
,
err
=
module
.
run_command
(
cmd
)
elif
state
==
'present'
and
not
exists
:
cmd
=
'
%
s "
%
s"'
%
(
add_apt_repository
,
repo
)
rc
,
out
,
err
=
module
.
run_command
(
cmd
)
else
:
module
.
exit_json
(
changed
=
False
,
repo
=
repo
,
state
=
state
)
if
rc
!=
0
:
module
.
fail_json
(
msg
=
err
)
else
:
changed
=
True
if
changed
:
if
state
==
'present'
and
changed
:
rc
,
out
,
err
=
module
.
run_command
(
'
%
s update'
%
APT
)
module
.
exit_json
(
changed
=
changed
,
repo
=
repo
,
state
=
state
)
# this is magic, see lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment