Ansible role for running puppet
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.yml 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. ---
  2. - name: Get management server puppet version
  3. shell:
  4. cmd: "PATH=$PATH:/opt/puppetlabs/bin puppet --version | cut -d '.' -f 1"
  5. delegate_to: localhost
  6. register: mgmt_puppet_version
  7. - name: Set management server puppet version fact
  8. set_fact:
  9. mgmt_puppet_version: "{{ mgmt_puppet_version.stdout }}"
  10. - name: Sanity check management server puppet version
  11. fail: "Unsupported puppet version {{ mgmt_puppet_version }}"
  12. when: (mgmt_puppet_version != '3' and mgmt_puppet_version != '4')
  13. - name: Get puppet version
  14. shell:
  15. cmd: "PATH=$PATH:/opt/puppetlabs/bin puppet --version | cut -d '.' -f 1"
  16. register: puppet_version
  17. - name: Set puppet version fact
  18. set_fact:
  19. puppet_version: "{{ puppet_version.stdout }}"
  20. - name: Sanity check puppet version
  21. fail: "Unsupported puppet version {{ puppet_version }}"
  22. when: (puppet_version != '3' and puppet_version != '4')
  23. - block:
  24. - name: Set management server hieradata var
  25. set_fact:
  26. mgmt_hieradata: "{{ '/etc/puppet/hieradata/' + puppet_environment if mgmt_puppet_version == '3' else '/etc/puppetlabs/code/environments/' + puppet_environment + '/hieradata' }}"
  27. delegate_to: localhost
  28. - name: Set hieradata var
  29. set_fact:
  30. hieradata: "{{ '/etc/puppet/hieradata/' + puppet_environment if puppet_version == '3' else '/etc/puppetlabs/code/environments/' + puppet_environment + '/hieradata' }}"
  31. - name: ensure hiera directory
  32. file:
  33. state: directory
  34. path: "{{ hieradata }}/{{ item }}"
  35. owner: root
  36. group: root
  37. mode: 0700
  38. with_items:
  39. - group_vars
  40. - host_vars
  41. - name: make file list
  42. puppet_get_hiera_file_list:
  43. fqdn: "{{ ansible_fqdn }}"
  44. groups: "{{ hostvars[inventory_hostname].group_names }}"
  45. location: "{{ hieradata }}"
  46. delegate_to: localhost
  47. register: hiera_file_paths
  48. - name: copy hiera files
  49. copy:
  50. src: "{{ mgmt_hieradata + '/' + item }}"
  51. dest: "{{ hieradata + '/' + item }}"
  52. mode: 0600
  53. with_items: "{{ hiera_file_paths.paths|default() }}"
  54. when: copy_hieradata
  55. - block:
  56. - name: copy puppet modules
  57. synchronize:
  58. src: "{{ manifest_base }}/{{ puppet_environment }}"
  59. dest: "{{ manifest_base }}"
  60. - name: ensure hieradata manifest link is present
  61. file:
  62. src: "{{ '/etc/puppet/hieradata' if puppet_version == '3' else '/etc/puppetlabs/code/environments' }}"
  63. dest: "{{ manifest_base }}/hieradata"
  64. state: link
  65. when: copy_hieradata
  66. - name: Set management server puppet module dir to user-defined path
  67. set_fact:
  68. mgmt_puppet_module_dir: "{{ puppet_basemodulepath }}"
  69. when: puppet_basemodulepath != ''
  70. - name: Set management server puppet module dir
  71. set_fact:
  72. mgmt_puppet_module_dir: "{{ '/etc/puppet/modules' if mgmt_puppet_version == '3' else '/etc/puppetlabs/code/modules' }}"
  73. delegate_to: localhost
  74. when: mgmt_puppet_module_dir is not defined
  75. - name: Set puppet module dir to user-defined path
  76. set_fact:
  77. puppet_module_dir: "{{ puppet_basemodulepath }}"
  78. when: puppet_basemodulepath != ''
  79. - name: Set puppet module dir
  80. set_fact:
  81. puppet_module_dir: "{{ '/etc/puppet' if puppet_version == '3' else '/etc/puppetlabs/code' }}"
  82. when: puppet_module_dir is not defined
  83. - name: copy system puppet modules
  84. synchronize:
  85. src: "{{ mgmt_puppet_module_dir }}"
  86. dest: "{{ puppet_module_dir }}"
  87. when:
  88. - copy_puppet
  89. - manifest_base is defined
  90. - name: setup config files
  91. when: manage_config|bool
  92. include: config.yml
  93. - block:
  94. - name: run puppet
  95. puppet:
  96. puppetmaster: "{{ puppetmaster|default(omit) }}"
  97. manifest: "{{ manifest|default(omit) }}"
  98. show_diff: "{{ show_diff|default(false) }}"
  99. facts: "{{ facts|default(omit) }}"
  100. facter_basename: "{{ facter_basename|default(omit) }}"
  101. logdest: "{{ puppet_logdest|default(omit) }}"
  102. environment: "{{ puppet_environment|default(omit) }}"
  103. noop: "{{ puppet_noop|default(omit) }}"
  104. debug: "{{ puppet_debug|default(omit) }}"
  105. timeout: "{{ puppet_timeout|default(omit) }}"
  106. - always:
  107. - name: find logs
  108. shell: "ls -tr /var/lib/puppet/reports/{{ ansible_fqdn }}/*_puppetdb.json"
  109. register: files
  110. failed_when: files.stdout_lines|default("") == ""
  111. - name: set log filename
  112. set_fact: puppet_logfile="{{ files.stdout_lines|sort|last }}"
  113. when: "{{ files.stdout_lines|length > 0 }}"
  114. - name: create reports directory
  115. file: path=/var/lib/puppet/reports/{{ ansible_fqdn }} owner=root group=root mode=0755 state=directory
  116. delegate_to: localhost
  117. when: "{{ files.stdout_lines|length > 0 }}"
  118. - name: fetch file
  119. synchronize:
  120. mode: pull
  121. src: "{{ puppet_logfile }}"
  122. dest: /var/lib/puppet/reports/{{ ansible_fqdn }}
  123. when: "{{ files.stdout_lines|length > 0 }}"
  124. - name: post facts
  125. puppet_post_puppetdb:
  126. puppetdb: "{{ puppetdb }}"
  127. hostvars: "{{ hostvars[inventory_hostname] }}"
  128. logfile: "{{ puppet_logfile }}"
  129. whoami: "{{ puppet_report_as }}"
  130. delegate_to: localhost
  131. when: "{{ files.stdout_lines|length > 0 }}"
  132. when: puppetdb is defined and puppet_report_as is defined