From c424d8b555ad63380c9f203e8f0704a3f5132f88 Mon Sep 17 00:00:00 2001 From: Monty Taylor Date: Tue, 17 Mar 2015 13:30:08 -0400 Subject: [PATCH] Copy in matching hiera files Find any files that match common, $fqdn and $group in the hiera structure and copy them to the target host's hiera structure so that the puppet apply command can operate. Change-Id: I833858e86b9e8fe0750642476f118ca7d0358380 --- library/puppet_get_hiera_file_list | 54 ++++++++++++++++++++++++++++++ tasks/main.yml | 47 ++++++++++++++++++++++++++ vars/main.yml | 3 ++ 3 files changed, 104 insertions(+) create mode 100644 library/puppet_get_hiera_file_list diff --git a/library/puppet_get_hiera_file_list b/library/puppet_get_hiera_file_list new file mode 100644 index 0000000..a37019f --- /dev/null +++ b/library/puppet_get_hiera_file_list @@ -0,0 +1,54 @@ +#!/usr/bin/python + +# Copyright (c) 2015 Hewlett-Packard Development Company, L.P. +# +# This module 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. +# +# This software 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 this software. If not, see . + +DOCUMENTATION = ''' +--- +module: puppet_get_hiera_file_list +short_description: Create a list of hiera file paths +description: + - Create a list of hiera file paths +options: + fqdn: + description: + - The ansible fqdn + required: true + groups: + description: + - The groups the host is in + required: true +requirements: [ ] +author: Monty Taylor +''' + + +def main(): + module = AnsibleModule( + argument_spec=dict( + fqdn=dict(required=True), + groups=dict(required=True), + ), + ) + p = module.params + + module.exit_json(paths_dict = {'paths': [ + 'common.yaml', + 'fqdn/%s.yaml' % p['fqdn'] ] + ['group/%s.yaml' % f for f in p['groups'] ]}) + +# import module snippets +from ansible.module_utils.basic import * + +main() diff --git a/tasks/main.yml b/tasks/main.yml index b9fb1ba..3808780 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -1,4 +1,51 @@ --- +- name: ensure hiera datadir + file: + state: directory + path: "{{ hieradata }}/{{ hieraenvironment }}" + owner: root + group: root + mode: 0700 + when: copy_hieradata +- name: ensure hiera datadir - fqdn + file: + state: directory + path: "{{ hieradata }}/{{ hieraenvironment }}/fqdn" + owner: root + group: root + mode: 0700 + when: copy_hieradata +- name: ensure hiera datadir - group + file: + state: directory + path: "{{ hieradata }}/{{ hieraenvironment }}/group" + owner: root + group: root + mode: 0700 + when: copy_hieradata +- name: make file list + puppet_get_hiera_file_list: + fqdn: "{{ ansible_fqdn }}" + groups: "{{ hostvars[inventory_hostname].group_names }}" + register: hiera_file_paths +- debug: msg="System {{hiera_file_paths.paths_dict.paths}}" +- name: find which files exist + stat: + path: "{{ hieradata }}/{{ hieraenvironment }}/{{ item }}" + register: st + with_items: hiera_file_paths.paths_dict.paths + delegate_to: localhost + when: copy_hieradata +- debug: msg="System {{st}}" +- name: copy hiera files + when: copy_hieradata and item.1.stat.exists + copy: + src: "{{ hieradata }}/{{ hieraenvironment }}/{{ item.1.item }}" + dest: "{{ hieradata }}/{{ hieraenvironment }}/{{ item.1.item }}" + mode: 0600 + with_together: + - hiera_file_paths.paths_dict.paths + - st.results - name: run puppet puppet: puppetmaster: "{{ puppetmaster|default(omit) }}" diff --git a/vars/main.yml b/vars/main.yml index 3856fd2..881149e 100644 --- a/vars/main.yml +++ b/vars/main.yml @@ -1,2 +1,5 @@ --- # vars file for ansible-puppet +hieradata: /etc/puppet/hieradata +hieraenvironment: production +copy_hieradata: False