aboutsummaryrefslogtreecommitdiff
path: root/personal_infra/playbooks/apply_puppet.yml
blob: 0a9068b56fafeae79fbf28a5b77dbd7b57a760b4 (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
---
- 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: ansible
              path: vars.json
              data_hash: json_data
      delegate_to: 127.0.0.1
    - name: dump all vars
      copy:
        dest: "{{ local_temp.path }}/data/vars.json"
        content: "{{ hostvars }}"
      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 {{ inventory_hostname }}
      environment:
        FACTER_ansible_inventory_hostname: "{{ inventory_hostname }}"
      delegate_to: 127.0.0.1
      register: catalog
    - name: install puppet
      package:
        name: puppet
    - 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