aboutsummaryrefslogtreecommitdiff
path: root/personal_infra/playbooks/apply_puppet.yml
blob: 0aa5d48451854153465e54f319224fc74e906581 (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
---
- name: apply puppet config
  hosts: all
  collections:
    - ansible.builtin
    - community.general

  tasks:
    - name: create local temporary directory
      tempfile:
        state: directory
        path: "{{ inventory_dir }}/tmp"
      register: local_temp
      delegate_to: 127.0.0.1
    - name: create data directory in local temp
      file:
        path: "{{ local_temp.path }}/data"
        state: directory
      delegate_to: 127.0.0.1
    - name: create hiera.yaml
      copy:
        dest: "{{ local_temp.path }}/hiera.yaml"
        content: |
          version: 5
          hierarchy:
            - name: hostvars
              path: hostvars.json
              data_hash: json_data
            - name: this
              path: this.json
              data_hash: json_data
      delegate_to: 127.0.0.1
    - name: dump hostvars
      copy:
        dest: "{{ local_temp.path }}/data/hostvars.json"
        content: "{'hostvars': {{ hostvars }} }"
      delegate_to: 127.0.0.1
    - name: dump this
      copy:
        dest: "{{ local_temp.path }}/data/this.json"
        content: "{{ hostvars[inventory_hostname] }}"
      delegate_to: 127.0.0.1
    - name: install epel
      package:
        name: epel-release
      when: ansible_distribution_file_variety == 'RedHat'
    - name: install puppet
      package:
        name: puppet
    - name: get facts
      command: facter -y
      register: facter_output
    - name: create facts directory in local temp
      file:
        path: "{{ local_temp.path }}/yaml/facts"
        state: directory
      delegate_to: 127.0.0.1
    - name: dump facts
      copy:
        dest: "{{ local_temp.path }}/yaml/facts/{{ inventory_hostname }}.yaml"
        content: "--- !ruby/object:Puppet::Node::Facts\nvalues:\n  {{ facter_output.stdout | indent(width=2) }}"
      delegate_to: 127.0.0.1
    - name: compile catalogs
      command: puppet catalog compile --modulepath={{ inventory_dir }}/puppet/modules --hiera_config={{ local_temp.path }}/hiera.yaml --manifest={{ inventory_dir }}/puppet/site --terminus compiler --vardir {{ local_temp.path }}/ --facts_terminus yaml {{ inventory_hostname }}
      delegate_to: 127.0.0.1
      register: catalog
    - name: create remote temporary directory
      tempfile:
        state: directory
      register: remote_temp
    - name: write catalog
      copy:
        dest: "{{ remote_temp.path }}/catalog.json"
        content: "{{ catalog.stdout | regex_replace('\\A.*?\\n', multiline=True) }}"
    - name: preview catalog
      command: puppet apply --catalog {{ remote_temp.path }}/catalog.json --noop --test
      register: catalog_apply
    - name: display catalog preview
      debug:
        msg: "{{ catalog_apply.stdout_lines }}"
    - name: pause to confirm
      pause:
      tags: pause
    - name: apply catalog
      command: puppet apply --catalog {{ remote_temp.path }}/catalog.json
      register: catalog_apply
    - name: display catalog application
      debug:
        msg: "{{ catalog_apply.stdout_lines }}"
    - name: clean up remote temporary directory
      file:
        state: absent
        path: "{{ remote_temp.path }}"
    - name: clean up local temporary directory
      file:
        state: absent
        path: "{{ local_temp.path}}"
      delegate_to: 127.0.0.1