Add script to make ansible groups from patterns

We have a set of hostname patterns which is not a thing that ansible
supports in inventory files. While we can put hostname patterns into
playbooks directly, that does not help us with copying hiera group files
since ansible doesn't know about the groups in site.pp and puppet
doesn't know about the ansible groups.

Instead, do a quick expansion any time the groups.txt file changes and
at the end of launch-node. It will be left to admins to run
expand-groups.sh whenever they delete a node.

Change-Id: I00c60748ddb2d35a3b98f78d828dabebcf065118
This commit is contained in:
Monty Taylor 2016-01-10 10:29:28 -05:00
parent e777d234a7
commit 7699226007
4 changed files with 66 additions and 0 deletions

View File

@ -300,6 +300,7 @@ def main():
# server
if os.path.exists('/var/cache/ansible-inventory/ansible-inventory.cache'):
os.unlink('/var/cache/ansible-inventory/ansible-inventory.cache')
os.system('/usr/local/bin/expand-groups.sh')
if __name__ == '__main__':
main()

View File

@ -0,0 +1,31 @@
#!/bin/bash
# Copyright 2016 IBM Corp
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
outdir=$(mktemp -d)
trap "rm -rf $outdir" EXIT
outfile=$outdir/generated-groups
echo "# This file is autogenerated" > $outfile
IFS=$'\n'
for line in $(</etc/ansible/groups.txt); do
name=$(echo $line | cut -f1 -d' ')
pattern=$(echo $line | cut -f2 -d' ')
echo "[${name}]" >> $outfile
ansible "~${pattern}" --list-hosts >> $outfile
done
cp $outfile /etc/ansible/hosts/generated-groups

View File

@ -0,0 +1,11 @@
jenkins jenkins.*\.openstack\.org
logstash-worker logstash-worker\d+\.openstack\.org
subunit-worker subunit-worker\d+\.openstack\.org
elasticsearch elasticsearch0[1-7]\.openstack\.org
git-loadbalancer git(-fe\d+)?\.openstack\.org
git-server git\d+\.openstack\.org
pypi pypi\..*\.openstack\.org
zuul-merger zm\d+\.openstack\.org
ci-backup ci-backup-.*\.openstack\.org
afsdb afsdb.*\.openstack\.org
afs afs.*\..*\.openstack\.org

View File

@ -233,4 +233,27 @@ class openstack_project::puppetmaster (
group => 'root',
mode => '0644',
}
file { '/etc/ansible/groups.txt':
owner => 'root',
group => 'root',
mode => '0444',
source => 'puppet:///modules/openstack_project/puppetmaster/groups.txt',
notify => Exec['expand_groups'],
}
file { '/usr/local/bin/expand-groups.sh':
owner => 'root',
group => 'root',
mode => '0755',
source => 'puppet:///modules/openstack_project/puppetmaster/expand-groups.sh',
notify => Exec['expand_groups'],
}
exec { 'expand_groups':
command => 'expand-groups.sh',
path => '/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin',
refreshonly => true,
}
}