Coder Social home page Coder Social logo

ansible-role-swap's Introduction

Ansible Role: Swap

CI

An Ansible Role that configures swap space on Linux.

Requirements

None.

Role Variables

Available variables are listed below, along with default values (see defaults/main.yml):

swap_file_path: /swapfile

The location of the swap file on the server.

swap_file_size_mb: '512'

How large (in mebibytes) to make the swap file.

swap_swappiness: '60'

The vm.swappiness value to be configured in sysconfig.

swap_file_state: present

If you wish to remove your swapfile, and disable swap, set this to absent. Generally you'd probably want to set this to present.

swap_file_create_command: "dd if=/dev/zero of={{ swap_file_path }} bs=1M count={{ swap_file_size_mb }}"

The command used to create the swap file. You could switch to using fallocate to write the swap file more quickly, though there may be inconsistencies if not writing the file with dd.

Dependencies

None.

Example Playbook

- hosts: all

  vars:
    swap_file_size_mb: '1024'

  roles:
    - geerlingguy.swap

License

MIT / BSD

Author Information

This role was created in 2018 by Jeff Geerling, author of Ansible for DevOps.

ansible-role-swap's People

Contributors

alexp11223 avatar earboxer avatar geerlingguy avatar misilot avatar peter-potvin avatar tpo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

ansible-role-swap's Issues

<urlopen error [SSL: CERTIFICATE_VERIFY_FAILED

Sorry, Jeff, I'm not so familiar with roles yet. How could I avoid this error anyhow? :)

ansible-galaxy install geerlingguy.swap
[WARNING]: - geerlingguy.swap was NOT installed successfully: Unknown error when attempting to call Galaxy at 'https://galaxy.ansible.com/api/': <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED]
certificate verify failed: unable to get local issuer certificate (_ssl.c:1056)>
ERROR! - you can use --ignore-errors to skip failed roles and finish processing the list.

Debian 9 issue with sysctl

Hello,

First of all thx for your investment in the ansible community, your roles are great.
I'm having an issue using this one tho. I'm using molecule with the docker driver and a basic debian:9 and can't make it work.
Here is the output:

 molecule converge
--> Validating schema /xxx/molecule/default/molecule.yml.
Validation completed successfully.
--> Test matrix

└── default
    ├── dependency
    ├── create
    ├── prepare
    └── converge

--> Scenario: 'default'
--> Action: 'dependency'
    - extracting swap to /tmp/molecule/ansible-role-flexibility/default/roles/swap
    - swap (1.0.0) was installed successfully
    - extracting elasticsearch to /tmp/molecule/ansible-role-flexibility/default/roles/elasticsearch
    - elasticsearch (3.0.1) was installed successfully
    - adding dependency: geerlingguy.java
    - extracting git to /tmp/molecule/ansible-role-flexibility/default/roles/git
    - git (2.0.4) was installed successfully
    - extracting apache to /tmp/molecule/ansible-role-flexibility/default/roles/apache
    - apache (3.0.3) was installed successfully
    - extracting mysql to /tmp/molecule/ansible-role-flexibility/default/roles/mysql
    - mysql (2.9.4) was installed successfully
    - extracting php-versions to /tmp/molecule/ansible-role-flexibility/default/roles/php-versions
    - php-versions (3.0.0) was installed successfully
    - extracting php to /tmp/molecule/ansible-role-flexibility/default/roles/php
    - php (3.7.0) was installed successfully
    - extracting sftp to /tmp/molecule/ansible-role-flexibility/default/roles/sftp
    - sftp (0.6.0) was installed successfully
    - extracting composer to /tmp/molecule/ansible-role-flexibility/default/roles/composer
    - composer (1.7.3) was installed successfully
    - extracting yarn to /tmp/molecule/ansible-role-flexibility/default/roles/yarn
    - yarn (v1.0.30) was installed successfully
    - downloading role 'java', owned by geerlingguy
    - downloading role from https://github.com/geerlingguy/ansible-role-java/archive/1.9.5.tar.gz
    - extracting geerlingguy.java to /tmp/molecule/ansible-role-flexibility/default/roles/geerlingguy.java
    - geerlingguy.java (1.9.5) was installed successfully
Dependency completed successfully.
--> Scenario: 'default'
--> Action: 'create'

    PLAY [Create] ******************************************************************

    TASK [Log into a Docker registry] **********************************************
    skipping: [localhost] => (item=None)

    TASK [Create Dockerfiles from image names] *************************************
    changed: [localhost] => (item=None)
    changed: [localhost]

    TASK [Discover local Docker images] ********************************************
    ok: [localhost] => (item=None)
    ok: [localhost]

    TASK [Build an Ansible compatible image] ***************************************
    changed: [localhost] => (item=None)
    changed: [localhost]

    TASK [Create docker network(s)] ************************************************

    TASK [Create molecule instance(s)] *********************************************
    changed: [localhost] => (item=None)
    changed: [localhost]

    TASK [Wait for instance(s) creation to complete] *******************************
    changed: [localhost] => (item=None)
    changed: [localhost]

    PLAY RECAP *********************************************************************
    localhost                  : ok=5    changed=4    unreachable=0    failed=0


--> Scenario: 'default'
--> Action: 'prepare'
Skipping, prepare playbook not configured.
--> Scenario: 'default'
--> Action: 'converge'

    PLAY [Converge] ****************************************************************

    TASK [Gathering Facts] *********************************************************
    ok: [debian-9]

    TASK [Enable Backports repository.] ********************************************
    changed: [debian-9]

    TASK [Update apt cache.] *******************************************************
    ok: [debian-9]

    TASK [swap : Manage swap file entry in fstab.] *********************************
    changed: [debian-9]

    TASK [swap : include_tasks] ****************************************************
    skipping: [debian-9]

    TASK [swap : include_tasks] ****************************************************
    included: /tmp/molecule/xxx/default/roles/swap/tasks/enable.yml for debian-9

    TASK [swap : Ensure swap file exists.] *****************************************
    changed: [debian-9]

    TASK [swap : Set permissions on swap file.] ************************************
    changed: [debian-9]

    TASK [swap : Make swap file if necessary.] *************************************
    changed: [debian-9]

    TASK [swap : Run swapon on the swap file.] *************************************
    changed: [debian-9]

    TASK [swap : Set swappiness.] **************************************************
    fatal: [debian-9]: FAILED! => {"changed": false, "msg": "Failed to find required executable sysctl in paths: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"}

    PLAY RECAP *********************************************************************
    debian-9                   : ok=9    changed=6    unreachable=0    failed=1

It is working great on debian:8. I noticed that procps isn't installed by default, thus sysctl isn't available.
If I confirm that it's the case would you accept a PR that installs procps if isn't installed yet?

Builds failing after fix for #32

Ah so fun... #32 fix from #26 fix means CI is failing with:

  TASK [geerlingguy.swap : include_tasks] ****************************************
  fatal: [instance]: FAILED! => {"msg": "The conditional check 'swap_file_state == 'absent' or (swap_file_state == 'present' and swap_file_existing_size_mb != swap_file_size_mb)' failed. The error was: error while evaluating conditional (swap_file_state == 'absent' or (swap_file_state == 'present' and swap_file_existing_size_mb != swap_file_size_mb)): 'swap_file_existing_size_mb' is undefined. 'swap_file_existing_size_mb' is undefined\n\nThe error appears to be in '/home/runner/work/ansible-role-swap/ansible-role-swap/geerlingguy.swap/tasks/main.yml': line 13, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- include_tasks: disable.yml\n  ^ here\n"}

Enable/Disable swap in /etc/fstab ?

Hi 👋

It looks like this role doesn't edit the /etc/fstab file, thus making the change non persistent after a reboot.
Would you consider adding a piece of code that edit this file in this role?

I can make the PR too, it's hacktoberfest month after all.

Feature request: support Ansible 2.10

Currently this role does not work with Ansible 2.10

$ ansible localhost -m import_role -a "name=geerlingguy.swap" --check
[WARNING]: You are running the development version of Ansible. You should only run Ansible from "devel" if you are modifying the Ansible engine,
or trying out features under development. This is a rapidly changing source of code and can become unstable at any point.
ERROR! couldn't resolve module/action 'mount'. This often indicates a misspelling, missing collection, or incorrect module path.

The error appears to be in '/home/yen/.ansible/galaxy-roles/geerlingguy.swap/tasks/main.yml': line 2, column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

---
- name: Manage swap file entry in fstab.
  ^ here

I managed to get it work by adding the following lines to ~/.ansible/galaxy-roles/geerlingguy.swap/meta/main.yml

collections:
  - ansible.posix

I'm not sure if it breaks compatibility with older Ansible versions. If so, maybe a new release with min_ansible_version: 2.10?

Support setting priority

On a system there can be multiple swap files/partitions, setting a priority allows to use the fast swap first and only later more slow swap destinations.

This is also useful if one wants to replace the current swap file in a graceful way:

  • Run the playbook to create (and use) the new swap file and set high priority.
  • Wait a while (depends on the system usage) so that the new swap file is being used
  • Run the playbook to disable and remove the old swap file.

Doing it in this way avoids spikes in resource usages.

Existing swapfile does not get resized

If I only pass the swap_file_size_mb parameter and set a different value than the existing swapfile's current size, the swapfile does not get modified to the new size.

Not sure if this is by design, but a note about this behavior does seem to be missing in the documentation.

The disable turns off ALL swap entries, not just this one

Looking at the disable.yml task, it calls the 'swapoff -a' command, which is wrong because it will remove all swap entries, not just the one the role is interested in. If you have multiple swap file entries, this will cause wierd problems.

I don't have a test case, but when I get a chance I'll try to whip one up to show the issue. But from what I can see, this role just assumes one swap file to be added, but disables all swap entries (device or file) when you disable.

Task hangs when creating large swapfile

Hi, thank you very much for all of your amazing ansible roles.

Ansible version: 2.6.4
local os: Mac OSx 10.14.3
host os: Debian GNU/Linux 9 (stretch)

When running this role with:
swap_file_size_mb: 32768
swap_swappiness: 10

and the default:
swap_file_create_command: "dd if=/dev/zero of={{ swap_file_path }} bs=1M count={{ swap_file_size_mb }}"

The task TASK [geerlingguy.swap : Ensure swap file exists.] hangs forever.
The dd command is correctly launched on a Debian 9 host, but it takes a long time to complete; I simply verify on the host by spamming ls -ahl /swapfile;

The playbook however hangs at that step and never recovers.
I need to ctrl+c it and start from scratch.

Running the role again afterwards obviously doesn't yield the expected results, as the swapfile exists and consequently the role doesn't run mkswap or swapon.

BTRFS support

During swapon command it fails with

swapon  /swapfile
[2377765.336953] BTRFS warning (device vda5): swapfile must not be copy-on-write
swapon: /swapfile: swapon failed: Invalid argument

'swap_file_existing_size_mb' is undefined

The revision Attempts to Resize Existing Swap File on master introduced a bug in the code.
the script fails when file with "swap_file_path" does not exist and swap_file_state" is set to "present"(default) and the script is run.
it first mount the swapfile in /etc/fstab.
after which it runs check-size.yml (recent change) where the variable "swap_file_existing_size_mb" gets defined only when swap file exists.
So in our case, after task in check-size.yml execution completes, the variable "swap_file_existing_size_mb" will not get defined.

after which it fails in when conditional clause of next include tasks
(swap_file_state == 'present' and swap_file_existing_size_mb != swap_file_size_mb) and return the following error.

ansible-role-swap is failing with following error:

TASK [ansible-role-swap : include_tasks] *****************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => 
 msg: |-
  The conditional check 'swap_file_state == 'absent' or (swap_file_state == 'present' and swap_file_existing_size_mb != swap_file_size_mb)' failed. The error was: error while evaluating conditional (swap_file_state == 'absent' or (swap_file_state == 'present' and swap_file_existing_size_mb != swap_file_size_mb)): 'swap_file_existing_size_mb' is undefined. 'swap_file_existing_size_mb' is undefined

complete logs

PLAY [localhost] *****************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************************************************************************************************
ok: [localhost]

TASK [ansible-role-swap : Manage swap file entry in fstab.] **********************************************************************************************************************************************************
changed: [localhost]

TASK [ansible-role-swap : include_tasks] *****************************************************************************************************************************************************************************
included: ***************/ansible-role-swap/tasks/check-size.yml for localhost

TASK [ansible-role-swap : Check if swap file exists] *****************************************************************************************************************************************************************
ok: [localhost]

TASK [ansible-role-swap : Set variable for existing swap file size] **************************************************************************************************************************************************
skipping: [localhost]

TASK [ansible-role-swap : include_tasks] *****************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => 
 msg: |-
  The conditional check 'swap_file_state == 'absent' or (swap_file_state == 'present' and swap_file_existing_size_mb != swap_file_size_mb)' failed. The error was: error while evaluating conditional (swap_file_state == 'absent' or (swap_file_state == 'present' and swap_file_existing_size_mb != swap_file_size_mb)): 'swap_file_existing_size_mb' is undefined. 'swap_file_existing_size_mb' is undefined
  
  The error appears to be in *********/ansible-role-swap/tasks/main.yml': line 13, column 3, but may
  be elsewhere in the file depending on the exact syntax problem.
  
  The offending line appears to be:
  
  
  - include_tasks: disable.yml
   ^ here

PLAY RECAP ***********************************************************************************************************************************************************************************************************
localhost         : ok=4   changed=1   unreachable=0  failed=1   skipped=1   rescued=0  ignored=0

script to reproduce the error

---
  roles:
  - role: ansible-role-swap
    vars:
      swap_file_size_mb: '1'
      swap_swappiness: '80'
      swap_file_path: '/temp_swapfile'
      swap_file_state: present
    become: True

Disable Swap not covered by Molecule tests

Taking a look at your molecule setup, it looks like there is no scenario for when the swap is disabled.

In addition, while trying to implement that with the docker driver, it seems like swapoff -a consistently fails inside a container. Either giving a "Not superuser" error when the container is ran unprivileged, or giving no output and yeilding a 255 error code when ran privileged. Any thoughts on this?

Updating swap value doesn't work?

  roles:
    - role: geerlingguy.swap
      become: true
      vars:
        swap_file_size_mb: '1024'

Previously I had this setup. After changing 1024 to 512, it does nothing.

get_md5 not available

The latest pull request, added an issue. I can't find the parameter in the documentation. Or do only I have this problem?
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/stat_module.html

TASK [swap : Check if swap file exists] ****************************************
fatal: [***********]: FAILED! => {"changed": false, "msg": "Unsupported parameters for (stat) module: get_md5. Supported parameters include: checksum_algorithm, follow, get_attributes, get_checksum, get_mime, path (attr, attributes, checksum, checksum_algo, dest, mime, mime-type, mime_type, name)."}

Add feature: Disable swap

I was thinking about adding some functionality to disable swap on my Kubernetes role... then realized I have this role, and it would be a perfect little feature addition.

Basically, for Kubernetes (and for other purposes sometimes), it's a good idea to disable swap entirely. Therefore, this role could be set into a mode to just disable swap permanently, using a bool variable like swap_disable.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.