main.yml 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. ---
  2. - name: Enable crb on CentOS
  3. shell:
  4. cmd: dnf config-manager --set-enabled crb
  5. changed_when: false
  6. when:
  7. - ansible_distribution == "CentOS"
  8. - name: Check for EPEL Repo on CentOS
  9. shell:
  10. cmd: rpm -q epel-release
  11. changed_when: false
  12. failed_when: false
  13. register: epel_check
  14. when:
  15. - ansible_distribution == "CentOS"
  16. - name: Query latest EPEL RPM File
  17. shell:
  18. cmd: curl -s https://dl.fedoraproject.org/pub/epel/{{ ansible_distribution_major_version }}/Everything/x86_64/Packages/e/ | grep epel-release | tail -n1 | grep -oP 'href="\K[^"]+'
  19. changed_when: false
  20. failed_when: false
  21. register: epel_rpm
  22. when:
  23. - ansible_distribution == "CentOS"
  24. - epel_check.rc|int == 1
  25. - name: Enable EPEL Repo on CentOS 9+
  26. dnf:
  27. name:
  28. - https://dl.fedoraproject.org/pub/epel/{{ ansible_distribution_major_version }}/Everything/x86_64/Packages/e/{{ epel_rpm.stdout }}
  29. state: present
  30. disable_gpg_check: yes
  31. when:
  32. - ansible_distribution == "CentOS"
  33. - epel_check.rc|int == 1
  34. - name: Check for RPMFusion rpms
  35. shell:
  36. cmd: rpm -q rpmfusion-free-release
  37. failed_when: false
  38. changed_when: false
  39. register: rpmfusion_check
  40. - name: Download RPMFusion .rpms
  41. get_url:
  42. url: "{{ item }}"
  43. dest: /home/ansible/{{ item | basename }}
  44. owner: ansible
  45. group: ansible
  46. mode: '0644'
  47. loop:
  48. - https://download1.rpmfusion.org/free/{{ 'el' if ansible_distribution == 'CentOS' else 'fedora' }}/rpmfusion-free-release-{{ ansible_distribution_major_version }}.noarch.rpm
  49. - https://download1.rpmfusion.org/nonfree/{{ 'el' if ansible_distribution == 'CentOS' else 'fedora' }}/rpmfusion-nonfree-release-{{ ansible_distribution_major_version }}.noarch.rpm
  50. loop_control:
  51. label: "{{ item | basename }}"
  52. register: rpmfusion_repos
  53. when:
  54. - ansible_distribution == 'CentOS' or ansible_distribution == 'Fedora'
  55. - rpmfusion_check.rc != "0"
  56. - name: Install RPMFusion .rpms
  57. yum:
  58. name:
  59. - /home/ansible/rpmfusion-free-release-{{ ansible_distribution_major_version }}.noarch.rpm
  60. - /home/ansible/rpmfusion-nonfree-release-{{ ansible_distribution_major_version }}.noarch.rpm
  61. disable_gpg_check: yes
  62. state: present
  63. when:
  64. - rpmfusion_repos is defined
  65. - rpmfusion_repos.changed
  66. - ansible_distribution == 'CentOS' or ansible_distribution == 'Fedora'
  67. - name: Combine Packages (RPM)
  68. set_fact:
  69. all_pkgs: "{{ all_pkgs | default([]) | union(item) }}"
  70. loop:
  71. - "{{ common_pkgs }}"
  72. - "{{ common_pkgs_rpm }}"
  73. - "{{ host_pkgs | default([]) }}"
  74. loop_control:
  75. label: "{{ all_pkgs | default([]) | length }} Packages"
  76. when:
  77. - ansible_distribution == 'CentOS' or ansible_distribution == 'Fedora'
  78. - name: Combine Packages (DEB)
  79. set_fact:
  80. all_pkgs: "{{ all_pkgs | default([]) | union(item) }}"
  81. loop:
  82. - "{{ common_pkgs }}"
  83. - "{{ common_pkgs_deb }}"
  84. - "{{ host_pkgs | default([]) }}"
  85. when:
  86. - ansible_distribution == 'Ubuntu' or ansible_distribution == 'Debian'
  87. - name: Install packages
  88. package:
  89. name: "{{ all_pkgs }}"
  90. state: present
  91. when:
  92. - all_pkgs is defined
  93. - name: Install ansible SSH keys
  94. authorized_key:
  95. user: ansible
  96. state: present
  97. exclusive: yes
  98. key: "{{ ssh_ansible_keys | join('\n') }}"
  99. when:
  100. - ansible_os_family != 'LibreELEC'
  101. tags: ssh
  102. - name: Install root SSH keys
  103. authorized_key:
  104. user: root
  105. state: present
  106. exclusive: yes
  107. key: "{{ ssh_root_keys | union(host_ssh_root_keys) | join('\n') }}"
  108. tags: ssh
  109. - name: Create additional users
  110. user:
  111. name: "{{ item.name }}"
  112. uid: "{{ item.uid }}"
  113. state: present
  114. shell: "{{ item.shell | default('/bin/bash') }}"
  115. create_home: "{{ item.create_home | default('yes') }}"
  116. with_items:
  117. - "{{ users }}"
  118. when:
  119. - item.name is defined
  120. - item.uid is defined
  121. - name: Add Bash aliases for root user
  122. lineinfile:
  123. dest: /root/.bashrc
  124. create: yes
  125. mode: '0644'
  126. line: "alias {{ item.alias }}='{{ item.command }}'"
  127. regexp: "^alias {{ item.alias }}="
  128. with_items:
  129. - "{{ common_bash_aliases | default('') }}"
  130. - "{{ host_bash_aliases | default('') }}"
  131. when:
  132. - (item.user is not defined or item.user == 'root')
  133. - item.alias is defined
  134. - item.command is defined
  135. - ansible_os_family != 'LibreELEC'
  136. tags: aliases
  137. - name: Add bash aliases for non-root users
  138. lineinfile:
  139. dest: /home/{{ item.user }}/.bashrc
  140. create: no
  141. mode: '0644'
  142. line: "alias {{ item.alias }}='{{ item.command }}'"
  143. regexp: "^alias {{ item.alias }}="
  144. register: create_alias
  145. failed_when:
  146. - create_alias.rc is defined
  147. - create_alias.rc != 257
  148. with_items:
  149. - "{{ common_bash_aliases | default('') }}"
  150. - "{{ host_bash_aliases | default('') }}"
  151. when:
  152. - item.user is defined
  153. - item.user != 'root'
  154. - item.alias is defined
  155. - item.command is defined
  156. - ansible_os_family != 'LibreELEC'
  157. tags: aliases
  158. - name: Enforce .bash_profile
  159. ansible.builtin.template:
  160. src: bash_profile.j2
  161. dest: /root/.bash_profile
  162. owner: root
  163. group: root
  164. mode: '0640'
  165. - name: Enforce .bashrc aliases for ll (Debian)
  166. ansible.builtin.lineinfile:
  167. path: /root/.bashrc
  168. regexp: "{{ item.regexp }}"
  169. line: "{{ item.line }}"
  170. loop:
  171. - { 'regexp': '^#?\s*export LS_OPTIONS=', 'line': "export LS_OPTIONS='--color=auto'" }
  172. - { 'regexp': '^#?\s*eval \"$(dircolors)\"', 'line': "eval \"$(dircolors)\"" }
  173. - { "regexp": '^#?\s*alias ls=', 'line': "alias ls='ls $LS_OPTIONS'" }
  174. - { "regexp": '^#?\s*alias ll=', 'line': "alias ll='ls $LS_OPTIONS -lh'" }
  175. - { "regexp": '^#?\s*alias l=', 'line': "alias l='ls $LS_OPTIONS -lA'" }
  176. when:
  177. - ansible_distribution == 'Debian'