Saltstack Official Linux Formula
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.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. {%- from "linux/map.jinja" import storage with context %}
  2. {%- if storage.enabled %}
  3. {%- if storage.swap.enabled is not defined or storage.swap.enabled %}
  4. {%- for swap_name, swap in storage.swap.items() %}
  5. {%- if swap.enabled %}
  6. {%- if swap.engine == 'partition' %}
  7. linux_create_swap_partition_{{ swap.device }}:
  8. cmd.run:
  9. - name: 'mkswap {{ swap.device }}'
  10. - unless: file -L -s {{ swap.device }} | grep -q 'swap file'
  11. linux_set_swap_partition_{{ swap.device }}:
  12. cmd.run:
  13. - name: 'swapon {{ swap.device }}'
  14. - unless: grep $(readlink -f {{ swap.device }}) /proc/swaps
  15. - require:
  16. - cmd: linux_create_swap_partition_{{ swap.device }}
  17. {{ swap.device }}:
  18. mount.swap:
  19. - persist: True
  20. - require:
  21. - cmd: linux_set_swap_partition_{{ swap.device }}
  22. {%- elif swap.engine == 'file' %}
  23. linux_create_swap_file_{{ swap.device }}:
  24. cmd.run:
  25. - name: 'dd if=/dev/zero of={{ swap.device }} bs=1048576 count={{ swap.size }} && chmod 0600 {{ swap.device }}'
  26. - creates: {{ swap.device }}
  27. linux_set_swap_file_{{ swap.device }}:
  28. cmd.wait:
  29. - name: 'mkswap {{ swap.device }}'
  30. - watch:
  31. - cmd: linux_create_swap_file_{{ swap.device }}
  32. linux_set_swap_file_status_{{ swap.device }}:
  33. cmd.run:
  34. - name: 'swapon {{ swap.device }}'
  35. - unless: grep {{ swap.device }} /proc/swaps
  36. - require:
  37. - cmd: linux_set_swap_file_{{ swap.device }}
  38. {{ swap.device }}:
  39. mount.swap:
  40. - persist: True
  41. - require:
  42. - cmd: linux_set_swap_file_{{ swap.device }}
  43. {%- endif %}
  44. {%- else %}
  45. {{ swap.device }}:
  46. module.run:
  47. - name: mount.rm_fstab
  48. - m_name: none
  49. - device: {{ swap.device }}
  50. - onlyif: grep -q {{ swap.device }} /etc/fstab
  51. linux_disable_swap_{{ swap.engine }}_{{ swap.device }}:
  52. cmd.run:
  53. {%- if swap.engine == 'partition' %}
  54. - name: 'swapoff {{ swap.device }}'
  55. {%- elif swap.engine == 'file' %}
  56. - name: 'swapoff {{ swap.device }} && rm -f {{ swap.device }}'
  57. {%- endif %}
  58. - onlyif: file -L -s {{ swap.device }} | grep -q 'swap file'
  59. {%- endif %}
  60. {%- endfor %}
  61. {%- elif storage.swap.enabled is defined and not storage.swap.enabled %}
  62. linux_disable_swap:
  63. cmd.run:
  64. - name: 'swapoff -a'
  65. {%- endif %}
  66. {%- endif %}