Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add swap module #5241

Closed
dlee opened this issue Dec 10, 2013 · 20 comments
Closed

Add swap module #5241

dlee opened this issue Dec 10, 2013 · 20 comments
Labels
feature This issue/PR relates to a feature request.

Comments

@dlee
Copy link

dlee commented Dec 10, 2013

There should be an idempotent way to ensure a swapfile is created and enabled.

@srgvg
Copy link
Contributor

srgvg commented Dec 10, 2013

Could be an extension to the mount module

@mpdehaan
Copy link
Contributor

yes, it could.

We're welcome to take submissions but I'm not going to keep this tracker ticket open to keep the queue a little bit less cluttered.

Someone can implement if they like.

Thanks!

@motdotla
Copy link

motdotla commented Jan 2, 2014

Anyone aware of someone creating this? I'm also in need for it and starting to figure out my own tasks to handle it for now.

@motdotla
Copy link

motdotla commented Jan 2, 2014

for anyone else coming across this thread, I ended up with this:

...
- name: Create swap space
  command: dd if=/dev/zero of=/extraswap bs=1M count=512
  when: ansible_swaptotal_mb < 1

- name: Make swap
  command: mkswap /extraswap
  when: ansible_swaptotal_mb < 1

- name: Add to fstab
  action: lineinfile dest=/etc/fstab regexp="extraswap" line="/extraswap none swap sw 0 0" state=present

- name: Turn swap on
  command: swapon -a

- name: Set swapiness
  shell: echo 0 | sudo tee /proc/sys/vm/swappiness
...

@jwilberding
Copy link

@scottmotte Just used this myself, very helpful, thanks!!

@samos123
Copy link

samos123 commented Feb 8, 2014

@scottmotte Thanks for the swap role, I just made a role of this with a variable called swap_mb

@motdotla
Copy link

motdotla commented Feb 8, 2014

@samos123 woot. Thanks.

@eaigner
Copy link

eaigner commented Feb 19, 2014

@scottmotte Is this snippet idempotent?

@jwilberding
Copy link

@eaignerThe first two commands have the the when clause, and the 3rd uses a special command to only add the line when it doesn't exist. The last two commands will always run, but should harmless, so fairly indempotent.

@eaigner
Copy link

eaigner commented Feb 19, 2014

Oh... I didn't realize ansible_swaptotal_mb is a global ansible var

@manuelmeurer
Copy link

Swappiness should be permanently set using the sysctl module IMO:

- name: set swapiness
  sysctl:
    name: vm.swappiness
    value: "1"

Also, setting it to 0 seems to disable swap so I'd set it to 1 to have minimum swappiness.

@JensDebergh
Copy link

I reused some of the snippets and adjusted them.
Ansible first pass through the commands work perfectly.
The second run fails because the swapfile already enabled is being used. Any idea how I can counter this in ansible?

@manuelmeurer
Copy link

@JensDebergh Here's my swap role which checks whether the swap file already exists: https://gist.github.com/manuelmeurer/a2c0a8c24a0bb5092250

@JensDebergh
Copy link

@manuelmeurer Awesome thanks ! 👍

@ozbillwang
Copy link
Contributor

ozbillwang commented Apr 26, 2016

There are two ways to create swap, on disk device and file, seems most documents and discussions are only talking about to create a swap file by ansible only.

In some aws ec2 instances, we can use ephemeral storage as swap space. But this ansible playbook has to be added to cronjob, otherwise, if the instance is stop/start, the swap setting will disappear.

Here are the codes I did with logic volume created from disk device

- name: creating new LVM volume group 
  lvg: vg=swap pvs=/dev/xvdc state=present

# Normally assigned swap size is two times of memory
- name: creating new LVM logical volume 
  lvol: vg=swap lv=lvswap size='{{ facter_memorysize_mb|int * 2 }}'

- name: Format swap device
  command: mkswap /dev/swap/lvswap
  when: ansible_swaptotal_mb < 1

- name: activate swap device
  command: swapon /dev/swap/lvswap
  when: ansible_swaptotal_mb < 1

@ozbillwang
Copy link
Contributor

ozbillwang commented Apr 27, 2016

@motdotla

Your way works, which @manuelmeurer 's way is not idempotent with my way to set swap on logic volume

@manuelmeurer
Copy link

@SydOps I'm pretty sure my script is idempotent. Can you explain?

@ozbillwang
Copy link
Contributor

ozbillwang commented Apr 27, 2016

@manuelmeurer

Sorry for the misunderstand, I didn't test your way when: not swap_file_check.stat.exists with swapfile, but have issue with logic volume in my way.

- name: check if swap file exists
  stat:
    path: "/dev/{{ vgname }}/{{ lvname }}"
  register: swap_file_check

# Here I can see the output, it is dict with stat.exist, value is true if it is set.
- command: echo {{ swap_file_check }}

- name: Format swap device
  command: mkswap /dev/swap/lvswap
  #when: ansible_swaptotal_mb < 1    
  when: not swap_file_check.stat.exists

The first when condition works fine, not the second.

@manuelmeurer
Copy link

I don't fully understand what your problem is but it seems it has nothing to do with the code I posted. Maybe a general confusion on your side how the register and when Ansible commands work?
http://docs.ansible.com/ansible/playbooks_variables.html#registered-variables

@edrozenberg
Copy link
Contributor

Following up on some comments regarding checking swapfile presence with "stat" module: strongly suggest using the following params on the "stat" module to make checking much faster (1 second vs. 45 seconds to check a 10GB file):

    get_attributes: False
    get_checksum:   False
    get_md5:        False
    get_mime:       False

@ansibot ansibot added feature This issue/PR relates to a feature request. and removed feature_idea labels Mar 2, 2018
@ansible ansible locked and limited conversation to collaborators Apr 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature This issue/PR relates to a feature request.
Projects
None yet
Development

No branches or pull requests