summaryrefslogtreecommitdiff
path: root/tasks/main.yml
blob: 699013cad6e4f595a073fab58a11309a9a93641b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
---
- name: Get management server puppet version
  shell:
    cmd: "PATH=$PATH:/opt/puppetlabs/bin puppet --version | cut -d '.' -f 1"
  delegate_to: localhost
  register: mgmt_puppet_version

- name: Set management server puppet version fact
  set_fact:
    mgmt_puppet_version: "{{ mgmt_puppet_version.stdout }}"

- name: Sanity check management server puppet version
  fail: "Unsupported puppet version {{ mgmt_puppet_version }}"
  when: (mgmt_puppet_version != '3' and mgmt_puppet_version != '4')

- name: Get puppet version
  shell:
    cmd: "PATH=$PATH:/opt/puppetlabs/bin puppet --version | cut -d '.' -f 1"
  register: puppet_version

- name: Set puppet version fact
  set_fact:
    puppet_version: "{{ puppet_version.stdout }}"

- name: Sanity check puppet version
  fail: "Unsupported puppet version {{ puppet_version }}"
  when: (puppet_version != '3' and puppet_version != '4')

- block:
  - name: Set management server hieradata var
    set_fact:
      mgmt_hieradata: "{{ '/etc/puppet/hieradata/' + puppet_environment if mgmt_puppet_version == '3' else '/etc/puppetlabs/code/environments/' + puppet_environment + '/hieradata' }}"
    delegate_to: localhost

  - name: Set hieradata var
    set_fact:
      hieradata: "{{ '/etc/puppet/hieradata/' + puppet_environment if puppet_version == '3' else '/etc/puppetlabs/code/environments/' + puppet_environment + '/hieradata' }}"

  - name: ensure hiera directory
    file:
      state: directory
      path: "{{ hieradata }}/{{ item }}"
      owner: root
      group: root
      mode: 0700
    with_items:
    - fqdn
    - group

  - name: make file list
    puppet_get_hiera_file_list:
      fqdn: "{{ ansible_fqdn }}"
      groups: "{{ hostvars[inventory_hostname].group_names }}"
      location: "{{ hieradata }}"
    delegate_to: localhost
    register: hiera_file_paths

  - name: copy hiera files
    copy:
      src: "{{ mgmt_hieradata + '/' + item }}"
      dest: "{{ hieradata + '/' + item }}"
      mode: 0600
    with_items: "{{ hiera_file_paths.paths|default() }}"

  when: copy_hieradata

- block:
  - name: copy puppet modules
    synchronize:
      src: "{{ manifest_base }}/{{ puppet_environment }}"
      dest: "{{ manifest_base }}"

  - name: ensure hieradata manifest link is present
    file:
      src: "{{ '/etc/puppet/hieradata' if puppet_version == '3' else '/etc/puppetlabs/code/environments' }}"
      dest: "{{ manifest_base }}/hieradata"
      state: link
    when: copy_hieradata

  - name: Set management server puppet module dir to user-defined path
    set_fact:
      mgmt_puppet_module_dir: "{{ puppet_basemodulepath }}"
    when: puppet_basemodulepath != ''

  - name: Set management server puppet module dir
    set_fact:
      mgmt_puppet_module_dir: "{{ '/etc/puppet/modules' if mgmt_puppet_version == '3' else '/etc/puppetlabs/code/modules' }}"
    delegate_to: localhost
    when: mgmt_puppet_module_dir is not defined

  - name: Set puppet module dir to user-defined path
    set_fact:
      puppet_module_dir: "{{ puppet_basemodulepath }}"
    when: puppet_basemodulepath != ''

  - name: Set puppet module dir
    set_fact:
      puppet_module_dir: "{{ '/etc/puppet' if puppet_version == '3' else '/etc/puppetlabs/code' }}"
    when: puppet_module_dir is not defined

  - name: copy system puppet modules
    synchronize:
      src: "{{ mgmt_puppet_module_dir }}"
      dest: "{{ puppet_module_dir }}"

  when:
  - copy_puppet
  - manifest_base is defined

- name: setup config files
  when: manage_config|bool
  include: config.yml

- block:
  - name: run puppet
    puppet:
      puppetmaster: "{{ puppetmaster|default(omit) }}"
      manifest: "{{ manifest|default(omit) }}"
      show_diff: "{{ show_diff|default(false) }}"
      facts: "{{ facts|default(omit) }}"
      facter_basename: "{{ facter_basename|default(omit) }}"
      logdest: "{{ puppet_logdest|default(omit) }}"
      environment: "{{ puppet_environment|default(omit) }}"
      noop: "{{ puppet_noop|default(omit) }}"
      debug: "{{ puppet_debug|default(omit) }}"
      timeout: "{{ puppet_timeout|default(omit) }}"
- always:
  - name: find logs
    shell: "ls -tr /var/lib/puppet/reports/{{ ansible_fqdn }}/*_puppetdb.json"
    register: files
    failed_when: files.stdout_lines|default("") == ""

  - name: set log filename
    set_fact: puppet_logfile="{{ files.stdout_lines|sort|last }}"
    when: "{{ files.stdout_lines|length > 0 }}"

  - name: create reports directory
    file: path=/var/lib/puppet/reports/{{ ansible_fqdn }} owner=root group=root mode=0755 state=directory
    delegate_to: localhost
    when: "{{ files.stdout_lines|length > 0 }}"

  - name: fetch file
    synchronize:
      mode: pull
      src: "{{ puppet_logfile }}"
      dest: /var/lib/puppet/reports/{{ ansible_fqdn }}
    when: "{{ files.stdout_lines|length > 0 }}"

  - name: post facts
    puppet_post_puppetdb:
      puppetdb: "{{ puppetdb }}"
      hostvars: "{{ hostvars[inventory_hostname] }}"
      logfile: "{{ puppet_logfile }}"
      whoami: "{{ puppet_report_as }}"
    delegate_to: localhost
    when: "{{ files.stdout_lines|length > 0 }}"

  when: puppetdb is defined and puppet_report_as is defined