Merge pull request #1696 from gregorg/enhance_limit
Allow regex to be used to select hosts in addition to fnmatch pattern
This commit is contained in:
commit
70bb7d1e54
4 changed files with 36 additions and 6 deletions
|
@ -2,12 +2,21 @@
|
||||||
.\" Title: ansible
|
.\" Title: ansible
|
||||||
.\" Author: [see the "AUTHOR" section]
|
.\" Author: [see the "AUTHOR" section]
|
||||||
.\" Generator: DocBook XSL Stylesheets v1.75.2 <http://docbook.sf.net/>
|
.\" Generator: DocBook XSL Stylesheets v1.75.2 <http://docbook.sf.net/>
|
||||||
.\" Date: 11/03/2012
|
.\" Date: 12/04/2012
|
||||||
.\" Manual: System administration commands
|
.\" Manual: System administration commands
|
||||||
.\" Source: Ansible 0.9
|
.\" Source: Ansible 0.9
|
||||||
.\" Language: English
|
.\" Language: English
|
||||||
.\"
|
.\"
|
||||||
.TH "ANSIBLE" "1" "11/03/2012" "Ansible 0\&.9" "System administration commands"
|
.TH "ANSIBLE" "1" "12/04/2012" "Ansible 0\&.9" "System administration commands"
|
||||||
|
.\" -----------------------------------------------------------------
|
||||||
|
.\" * Define some portability stuff
|
||||||
|
.\" -----------------------------------------------------------------
|
||||||
|
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
.\" http://bugs.debian.org/507673
|
||||||
|
.\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
|
||||||
|
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
.ie \n(.g .ds Aq \(aq
|
||||||
|
.el .ds Aq '
|
||||||
.\" -----------------------------------------------------------------
|
.\" -----------------------------------------------------------------
|
||||||
.\" * set default formatting
|
.\" * set default formatting
|
||||||
.\" -----------------------------------------------------------------
|
.\" -----------------------------------------------------------------
|
||||||
|
@ -25,7 +34,7 @@ ansible \- run a command somewhere else
|
||||||
ansible <host\-pattern> [\-f forks] [\-m module_name] [\-a args]
|
ansible <host\-pattern> [\-f forks] [\-m module_name] [\-a args]
|
||||||
.SH "DESCRIPTION"
|
.SH "DESCRIPTION"
|
||||||
.sp
|
.sp
|
||||||
\fBAnsible\fR is an extra\-simple tool/framework/API for doing \'remote things\' over SSH\&.
|
\fBAnsible\fR is an extra\-simple tool/framework/API for doing \*(Aqremote things\*(Aq over SSH\&.
|
||||||
.SH "ARGUMENTS"
|
.SH "ARGUMENTS"
|
||||||
.PP
|
.PP
|
||||||
\fBhost\-pattern\fR
|
\fBhost\-pattern\fR
|
||||||
|
@ -73,7 +82,7 @@ to load modules from\&. The default is
|
||||||
\fI/usr/share/ansible\fR\&.
|
\fI/usr/share/ansible\fR\&.
|
||||||
.RE
|
.RE
|
||||||
.PP
|
.PP
|
||||||
\fB\-a\fR \'\fIARGUMENTS\fR\', \fB\-\-args=\fR\'\fIARGUMENTS\fR\'
|
\fB\-a\fR \*(Aq\fIARGUMENTS\fR\*(Aq, \fB\-\-args=\fR\*(Aq\fIARGUMENTS\fR\*(Aq
|
||||||
.RS 4
|
.RS 4
|
||||||
The
|
The
|
||||||
\fIARGUMENTS\fR
|
\fIARGUMENTS\fR
|
||||||
|
@ -156,6 +165,11 @@ is mostly useful for crontab or kickstarts\&.
|
||||||
.RS 4
|
.RS 4
|
||||||
Further limits the selected host/group patterns\&.
|
Further limits the selected host/group patterns\&.
|
||||||
.RE
|
.RE
|
||||||
|
.PP
|
||||||
|
\fB\-l\fR \fI~REGEX\fR, \fB\-\-limit=\fR\fI~REGEX\fR
|
||||||
|
.RS 4
|
||||||
|
Further limits hosts with a regex pattern\&.
|
||||||
|
.RE
|
||||||
.SH "INVENTORY"
|
.SH "INVENTORY"
|
||||||
.sp
|
.sp
|
||||||
Ansible stores the hosts it can potentially operate on in an inventory file\&. The syntax is one host per line\&. Groups headers are allowed and are included on their own line, enclosed in square brackets that start the line\&.
|
Ansible stores the hosts it can potentially operate on in an inventory file\&. The syntax is one host per line\&. Groups headers are allowed and are included on their own line, enclosed in square brackets that start the line\&.
|
||||||
|
|
|
@ -117,6 +117,10 @@ and 'local'. 'local' is mostly useful for crontab or kickstarts.
|
||||||
|
|
||||||
Further limits the selected host/group patterns.
|
Further limits the selected host/group patterns.
|
||||||
|
|
||||||
|
*-l* '\~REGEX', *--limit=*'~REGEX'::
|
||||||
|
|
||||||
|
Further limits hosts with a regex pattern.
|
||||||
|
|
||||||
INVENTORY
|
INVENTORY
|
||||||
---------
|
---------
|
||||||
|
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
|
|
||||||
import fnmatch
|
import fnmatch
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
|
|
||||||
import subprocess
|
import subprocess
|
||||||
import ansible.constants as C
|
import ansible.constants as C
|
||||||
|
@ -92,7 +93,10 @@ class Inventory(object):
|
||||||
raise errors.AnsibleError("YAML inventory support is deprecated in 0.6 and removed in 0.7, see the migration script in examples/scripts in the git checkout")
|
raise errors.AnsibleError("YAML inventory support is deprecated in 0.6 and removed in 0.7, see the migration script in examples/scripts in the git checkout")
|
||||||
|
|
||||||
def _match(self, str, pattern_str):
|
def _match(self, str, pattern_str):
|
||||||
return fnmatch.fnmatch(str, pattern_str)
|
if pattern_str.startswith('~'):
|
||||||
|
return re.search(pattern_str[1:], str)
|
||||||
|
else:
|
||||||
|
return fnmatch.fnmatch(str, pattern_str)
|
||||||
|
|
||||||
def get_hosts(self, pattern="all"):
|
def get_hosts(self, pattern="all"):
|
||||||
"""
|
"""
|
||||||
|
@ -157,7 +161,7 @@ class Inventory(object):
|
||||||
a tuple of (start, stop) or None
|
a tuple of (start, stop) or None
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if not "[" in pattern:
|
if not "[" in pattern or pattern.startswith('~'):
|
||||||
return (pattern, None)
|
return (pattern, None)
|
||||||
(first, rest) = pattern.split("[")
|
(first, rest) = pattern.split("[")
|
||||||
rest = rest.replace("]","")
|
rest = rest.replace("]","")
|
||||||
|
|
|
@ -158,6 +158,14 @@ class TestInventory(unittest.TestCase):
|
||||||
print "EXPECTED=%s" % sorted(expected_hosts)
|
print "EXPECTED=%s" % sorted(expected_hosts)
|
||||||
assert sorted(hosts) == sorted(expected_hosts)
|
assert sorted(hosts) == sorted(expected_hosts)
|
||||||
|
|
||||||
|
def test_regex_exclude(self):
|
||||||
|
inventory = self.complex_inventory()
|
||||||
|
hosts = inventory.list_hosts("~rtp_[ac]")
|
||||||
|
expected_hosts = ['rtp_a', 'rtp_c']
|
||||||
|
print "HOSTS=%s" % sorted(hosts)
|
||||||
|
print "EXPECTED=%s" % sorted(expected_hosts)
|
||||||
|
assert sorted(hosts) == sorted(expected_hosts)
|
||||||
|
|
||||||
def test_complex_enumeration(self):
|
def test_complex_enumeration(self):
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue