Skip to content

Instantly share code, notes, and snippets.

@geerlingguy
Last active December 14, 2024 17:13
Show Gist options
  • Save geerlingguy/73ef1e5ee45d8694570f334be385e181 to your computer and use it in GitHub Desktop.
Save geerlingguy/73ef1e5ee45d8694570f334be385e181 to your computer and use it in GitHub Desktop.

Revisions

  1. geerlingguy revised this gist Jun 18, 2023. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions ansible-role-test.sh
    Original file line number Diff line number Diff line change
    @@ -83,6 +83,7 @@ elif [ $distro = 'fedora37' ]; then
    elif [ $distro = 'fedora36' ]; then
    init="/usr/lib/systemd/systemd"
    opts="--privileged --cgroupns=host --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:rw"
    fi

    # Run the container using the supplied OS.
    printf ${green}"Starting Docker container: geerlingguy/docker-$distro-ansible."${neutral}"\n"
  2. geerlingguy revised this gist Jun 18, 2023. 1 changed file with 24 additions and 49 deletions.
    73 changes: 24 additions & 49 deletions ansible-role-test.sh
    Original file line number Diff line number Diff line change
    @@ -35,79 +35,54 @@ container_id=${container_id:-$timestamp}
    test_idempotence=${test_idempotence:-"true"}

    ## Set up vars for Docker setup.
    # Rocky Linux 8
    if [ $distro = 'rockylinux8' ]; then
    # Rocky Linux 9
    if [ $distro = 'rockylinux9' ]; then
    init="/usr/lib/systemd/systemd"
    opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    # CentOS 8
    elif [ $distro = 'centos8' ]; then
    opts="--privileged --cgroupns=host --volume=/sys/fs/cgroup:/sys/fs/cgroup:rw"
    # Rocky Linux 8
    elif [ $distro = 'rockylinux8' ]; then
    init="/usr/lib/systemd/systemd"
    opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    opts="--privileged --cgroupns=host --volume=/sys/fs/cgroup:/sys/fs/cgroup:rw"
    # CentOS 7
    elif [ $distro = 'centos7' ]; then
    init="/usr/lib/systemd/systemd"
    opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    # CentOS 6
    elif [ $distro = 'centos6' ]; then
    init="/sbin/init"
    opts="--privileged"
    opts="--privileged --cgroupns=host --volume=/sys/fs/cgroup:/sys/fs/cgroup:rw"
    # Ubuntu 22.04
    elif [ $distro = 'ubuntu2204' ]; then
    init="/lib/systemd/systemd"
    opts="--privileged --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    opts="--privileged --cgroupns=host --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:rw"
    # Ubuntu 20.04
    elif [ $distro = 'ubuntu2004' ]; then
    init="/lib/systemd/systemd"
    opts="--privileged --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    opts="--privileged --cgroupns=host --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:rw"
    # Ubuntu 18.04
    elif [ $distro = 'ubuntu1804' ]; then
    init="/lib/systemd/systemd"
    opts="--privileged --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    # Ubuntu 16.04
    elif [ $distro = 'ubuntu1604' ]; then
    opts="--privileged --cgroupns=host --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:rw"
    # Debian 12
    elif [ $distro = 'debian12' ]; then
    init="/lib/systemd/systemd"
    opts="--privileged --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    opts="--privileged --cgroupns=host --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:rw"
    # Debian 11
    elif [ $distro = 'debian11' ]; then
    init="/lib/systemd/systemd"
    opts="--privileged --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    opts="--privileged --cgroupns=host --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:rw"
    # Debian 10
    elif [ $distro = 'debian10' ]; then
    init="/lib/systemd/systemd"
    opts="--privileged --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    # Debian 9
    elif [ $distro = 'debian9' ]; then
    init="/lib/systemd/systemd"
    opts="--privileged --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    # Debian 8
    elif [ $distro = 'debian8' ]; then
    init="/lib/systemd/systemd"
    opts="--privileged --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    # Fedora 35
    elif [ $distro = 'fedora35' ]; then
    opts="--privileged --cgroupns=host --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:rw"
    # Fedora 38
    elif [ $distro = 'fedora38' ]; then
    init="/usr/lib/systemd/systemd"
    opts="--privileged --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    # Fedora 34
    elif [ $distro = 'fedora34' ]; then
    opts="--privileged --cgroupns=host --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:rw"
    # Fedora 37
    elif [ $distro = 'fedora37' ]; then
    init="/usr/lib/systemd/systemd"
    opts="--privileged --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    # Fedora 33
    elif [ $distro = 'fedora33' ]; then
    opts="--privileged --cgroupns=host --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:rw"
    # Fedora 36
    elif [ $distro = 'fedora36' ]; then
    init="/usr/lib/systemd/systemd"
    opts="--privileged --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    # Fedora 32
    elif [ $distro = 'fedora32' ]; then
    init="/usr/lib/systemd/systemd"
    opts="--privileged --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    # Fedora 31
    elif [ $distro = 'fedora31' ]; then
    init="/usr/lib/systemd/systemd"
    opts="--privileged --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    # Fedora 30
    elif [ $distro = 'fedora30' ]; then
    init="/usr/lib/systemd/systemd"
    opts="--privileged --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    fi
    opts="--privileged --cgroupns=host --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:rw"

    # Run the container using the supplied OS.
    printf ${green}"Starting Docker container: geerlingguy/docker-$distro-ansible."${neutral}"\n"
  3. geerlingguy revised this gist Apr 21, 2022. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions ansible-role-test.sh
    Original file line number Diff line number Diff line change
    @@ -51,6 +51,10 @@ elif [ $distro = 'centos7' ]; then
    elif [ $distro = 'centos6' ]; then
    init="/sbin/init"
    opts="--privileged"
    # Ubuntu 22.04
    elif [ $distro = 'ubuntu2204' ]; then
    init="/lib/systemd/systemd"
    opts="--privileged --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    # Ubuntu 20.04
    elif [ $distro = 'ubuntu2004' ]; then
    init="/lib/systemd/systemd"
  4. geerlingguy revised this gist Apr 15, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ansible-role-test.sh
    Original file line number Diff line number Diff line change
    @@ -40,7 +40,7 @@ if [ $distro = 'rockylinux8' ]; then
    init="/usr/lib/systemd/systemd"
    opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    # CentOS 8
    if [ $distro = 'centos8' ]; then
    elif [ $distro = 'centos8' ]; then
    init="/usr/lib/systemd/systemd"
    opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    # CentOS 7
  5. geerlingguy revised this gist Apr 15, 2022. 1 changed file with 21 additions and 1 deletion.
    22 changes: 21 additions & 1 deletion ansible-role-test.sh
    Original file line number Diff line number Diff line change
    @@ -27,14 +27,18 @@ neutral='\033[0m'
    timestamp=$(date +%s)

    # Allow environment variables to override defaults.
    distro=${distro:-"centos8"}
    distro=${distro:-"rockylinux8"}
    playbook=${playbook:-"test.yml"}
    role_dir=${role_dir:-"$PWD"}
    cleanup=${cleanup:-"true"}
    container_id=${container_id:-$timestamp}
    test_idempotence=${test_idempotence:-"true"}

    ## Set up vars for Docker setup.
    # Rocky Linux 8
    if [ $distro = 'rockylinux8' ]; then
    init="/usr/lib/systemd/systemd"
    opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    # CentOS 8
    if [ $distro = 'centos8' ]; then
    init="/usr/lib/systemd/systemd"
    @@ -59,6 +63,10 @@ elif [ $distro = 'ubuntu1804' ]; then
    elif [ $distro = 'ubuntu1604' ]; then
    init="/lib/systemd/systemd"
    opts="--privileged --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    # Debian 11
    elif [ $distro = 'debian11' ]; then
    init="/lib/systemd/systemd"
    opts="--privileged --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    # Debian 10
    elif [ $distro = 'debian10' ]; then
    init="/lib/systemd/systemd"
    @@ -71,6 +79,18 @@ elif [ $distro = 'debian9' ]; then
    elif [ $distro = 'debian8' ]; then
    init="/lib/systemd/systemd"
    opts="--privileged --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    # Fedora 35
    elif [ $distro = 'fedora35' ]; then
    init="/usr/lib/systemd/systemd"
    opts="--privileged --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    # Fedora 34
    elif [ $distro = 'fedora34' ]; then
    init="/usr/lib/systemd/systemd"
    opts="--privileged --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    # Fedora 33
    elif [ $distro = 'fedora33' ]; then
    init="/usr/lib/systemd/systemd"
    opts="--privileged --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    # Fedora 32
    elif [ $distro = 'fedora32' ]; then
    init="/usr/lib/systemd/systemd"
  6. geerlingguy revised this gist May 9, 2020. 1 changed file with 11 additions and 11 deletions.
    22 changes: 11 additions & 11 deletions ansible-role-test.sh
    Original file line number Diff line number Diff line change
    @@ -27,7 +27,7 @@ neutral='\033[0m'
    timestamp=$(date +%s)

    # Allow environment variables to override defaults.
    distro=${distro:-"centos7"}
    distro=${distro:-"centos8"}
    playbook=${playbook:-"test.yml"}
    role_dir=${role_dir:-"$PWD"}
    cleanup=${cleanup:-"true"}
    @@ -47,6 +47,10 @@ elif [ $distro = 'centos7' ]; then
    elif [ $distro = 'centos6' ]; then
    init="/sbin/init"
    opts="--privileged"
    # Ubuntu 20.04
    elif [ $distro = 'ubuntu2004' ]; then
    init="/lib/systemd/systemd"
    opts="--privileged --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    # Ubuntu 18.04
    elif [ $distro = 'ubuntu1804' ]; then
    init="/lib/systemd/systemd"
    @@ -55,10 +59,6 @@ elif [ $distro = 'ubuntu1804' ]; then
    elif [ $distro = 'ubuntu1604' ]; then
    init="/lib/systemd/systemd"
    opts="--privileged --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    # Ubuntu 14.04
    elif [ $distro = 'ubuntu1404' ]; then
    init="/sbin/init"
    opts="--privileged --volume=/var/lib/docker"
    # Debian 10
    elif [ $distro = 'debian10' ]; then
    init="/lib/systemd/systemd"
    @@ -71,16 +71,16 @@ elif [ $distro = 'debian9' ]; then
    elif [ $distro = 'debian8' ]; then
    init="/lib/systemd/systemd"
    opts="--privileged --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    # Fedora 30
    elif [ $distro = 'fedora30' ]; then
    # Fedora 32
    elif [ $distro = 'fedora32' ]; then
    init="/usr/lib/systemd/systemd"
    opts="--privileged --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    # Fedora 29
    elif [ $distro = 'fedora29' ]; then
    # Fedora 31
    elif [ $distro = 'fedora31' ]; then
    init="/usr/lib/systemd/systemd"
    opts="--privileged --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    # Fedora 27
    elif [ $distro = 'fedora27' ]; then
    # Fedora 30
    elif [ $distro = 'fedora30' ]; then
    init="/usr/lib/systemd/systemd"
    opts="--privileged --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    fi
  7. geerlingguy revised this gist Oct 8, 2019. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion ansible-role-test.sh
    Original file line number Diff line number Diff line change
    @@ -35,8 +35,12 @@ container_id=${container_id:-$timestamp}
    test_idempotence=${test_idempotence:-"true"}

    ## Set up vars for Docker setup.
    # CentOS 8
    if [ $distro = 'centos8' ]; then
    init="/usr/lib/systemd/systemd"
    opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    # CentOS 7
    if [ $distro = 'centos7' ]; then
    elif [ $distro = 'centos7' ]; then
    init="/usr/lib/systemd/systemd"
    opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    # CentOS 6
  8. geerlingguy revised this gist Aug 16, 2019. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions ansible-role-test.sh
    Original file line number Diff line number Diff line change
    @@ -55,6 +55,10 @@ elif [ $distro = 'ubuntu1604' ]; then
    elif [ $distro = 'ubuntu1404' ]; then
    init="/sbin/init"
    opts="--privileged --volume=/var/lib/docker"
    # Debian 10
    elif [ $distro = 'debian10' ]; then
    init="/lib/systemd/systemd"
    opts="--privileged --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    # Debian 9
    elif [ $distro = 'debian9' ]; then
    init="/lib/systemd/systemd"
    @@ -63,6 +67,10 @@ elif [ $distro = 'debian9' ]; then
    elif [ $distro = 'debian8' ]; then
    init="/lib/systemd/systemd"
    opts="--privileged --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    # Fedora 30
    elif [ $distro = 'fedora30' ]; then
    init="/usr/lib/systemd/systemd"
    opts="--privileged --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    # Fedora 29
    elif [ $distro = 'fedora29' ]; then
    init="/usr/lib/systemd/systemd"
  9. geerlingguy revised this gist Oct 23, 2018. 1 changed file with 2 additions and 6 deletions.
    8 changes: 2 additions & 6 deletions ansible-role-test.sh
    Original file line number Diff line number Diff line change
    @@ -55,10 +55,6 @@ elif [ $distro = 'ubuntu1604' ]; then
    elif [ $distro = 'ubuntu1404' ]; then
    init="/sbin/init"
    opts="--privileged --volume=/var/lib/docker"
    # Ubuntu 12.04
    elif [ $distro = 'ubuntu1204' ]; then
    init="/sbin/init"
    opts="--privileged --volume=/var/lib/docker"
    # Debian 9
    elif [ $distro = 'debian9' ]; then
    init="/lib/systemd/systemd"
    @@ -67,8 +63,8 @@ elif [ $distro = 'debian9' ]; then
    elif [ $distro = 'debian8' ]; then
    init="/lib/systemd/systemd"
    opts="--privileged --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    # Fedora 24
    elif [ $distro = 'fedora24' ]; then
    # Fedora 29
    elif [ $distro = 'fedora29' ]; then
    init="/usr/lib/systemd/systemd"
    opts="--privileged --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    # Fedora 27
  10. geerlingguy revised this gist May 10, 2018. 1 changed file with 8 additions and 8 deletions.
    16 changes: 8 additions & 8 deletions ansible-role-test.sh
    Original file line number Diff line number Diff line change
    @@ -46,35 +46,35 @@ elif [ $distro = 'centos6' ]; then
    # Ubuntu 18.04
    elif [ $distro = 'ubuntu1804' ]; then
    init="/lib/systemd/systemd"
    opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    opts="--privileged --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    # Ubuntu 16.04
    elif [ $distro = 'ubuntu1604' ]; then
    init="/lib/systemd/systemd"
    opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    opts="--privileged --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    # Ubuntu 14.04
    elif [ $distro = 'ubuntu1404' ]; then
    init="/sbin/init"
    opts="--privileged"
    opts="--privileged --volume=/var/lib/docker"
    # Ubuntu 12.04
    elif [ $distro = 'ubuntu1204' ]; then
    init="/sbin/init"
    opts="--privileged"
    opts="--privileged --volume=/var/lib/docker"
    # Debian 9
    elif [ $distro = 'debian9' ]; then
    init="/lib/systemd/systemd"
    opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    opts="--privileged --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    # Debian 8
    elif [ $distro = 'debian8' ]; then
    init="/lib/systemd/systemd"
    opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    opts="--privileged --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    # Fedora 24
    elif [ $distro = 'fedora24' ]; then
    init="/usr/lib/systemd/systemd"
    opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    opts="--privileged --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    # Fedora 27
    elif [ $distro = 'fedora27' ]; then
    init="/usr/lib/systemd/systemd"
    opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    opts="--privileged --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    fi

    # Run the container using the supplied OS.
  11. geerlingguy revised this gist Mar 21, 2018. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions ansible-role-test.sh
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,7 @@
    # Usage: [OPTIONS] ./tests/test.sh
    # - distro: a supported Docker distro version (default = "centos7")
    # - playbook: a playbook in the tests directory (default = "test.yml")
    # - role_dir: the directory where the role exists (default = $PWD)
    # - cleanup: whether to remove the Docker container (default = true)
    # - container_id: the --name to set for the container (default = timestamp)
    # - test_idempotence: whether to test playbook's idempotence (default = true)
    @@ -28,6 +29,7 @@ timestamp=$(date +%s)
    # Allow environment variables to override defaults.
    distro=${distro:-"centos7"}
    playbook=${playbook:-"test.yml"}
    role_dir=${role_dir:-"$PWD"}
    cleanup=${cleanup:-"true"}
    container_id=${container_id:-$timestamp}
    test_idempotence=${test_idempotence:-"true"}
    @@ -78,12 +80,12 @@ fi
    # Run the container using the supplied OS.
    printf ${green}"Starting Docker container: geerlingguy/docker-$distro-ansible."${neutral}"\n"
    docker pull geerlingguy/docker-$distro-ansible:latest
    docker run --detach --volume="$PWD":/etc/ansible/roles/role_under_test:rw --name $container_id $opts geerlingguy/docker-$distro-ansible:latest $init
    docker run --detach --volume="$role_dir":/etc/ansible/roles/role_under_test:rw --name $container_id $opts geerlingguy/docker-$distro-ansible:latest $init

    printf "\n"

    # Install requirements if `requirements.yml` is present.
    if [ -f "$PWD/tests/requirements.yml" ]; then
    if [ -f "$role_dir/tests/requirements.yml" ]; then
    printf ${green}"Requirements file detected; installing dependencies."${neutral}"\n"
    docker exec --tty $container_id env TERM=xterm ansible-galaxy install -r /etc/ansible/roles/role_under_test/tests/requirements.yml
    fi
  12. geerlingguy revised this gist Mar 9, 2018. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions ansible-role-test.sh
    Original file line number Diff line number Diff line change
    @@ -41,6 +41,10 @@ if [ $distro = 'centos7' ]; then
    elif [ $distro = 'centos6' ]; then
    init="/sbin/init"
    opts="--privileged"
    # Ubuntu 18.04
    elif [ $distro = 'ubuntu1804' ]; then
    init="/lib/systemd/systemd"
    opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    # Ubuntu 16.04
    elif [ $distro = 'ubuntu1604' ]; then
    init="/lib/systemd/systemd"
  13. geerlingguy revised this gist Mar 5, 2018. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions ansible-role-test.sh
    Original file line number Diff line number Diff line change
    @@ -9,6 +9,10 @@
    # - container_id: the --name to set for the container (default = timestamp)
    # - test_idempotence: whether to test playbook's idempotence (default = true)
    #
    # If you place a requirements.yml file in tests/requirements.yml, the
    # requirements listed inside that file will be installed via Ansible Galaxy
    # prior to running tests.
    #
    # License: MIT

    # Exit on any individual command failure.
  14. geerlingguy revised this gist Feb 17, 2018. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion ansible-role-test.sh
    Original file line number Diff line number Diff line change
    @@ -61,7 +61,6 @@ elif [ $distro = 'debian8' ]; then
    elif [ $distro = 'fedora24' ]; then
    init="/usr/lib/systemd/systemd"
    opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    fi
    # Fedora 27
    elif [ $distro = 'fedora27' ]; then
    init="/usr/lib/systemd/systemd"
  15. geerlingguy revised this gist Feb 17, 2018. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions ansible-role-test.sh
    Original file line number Diff line number Diff line change
    @@ -62,6 +62,11 @@ elif [ $distro = 'fedora24' ]; then
    init="/usr/lib/systemd/systemd"
    opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    fi
    # Fedora 27
    elif [ $distro = 'fedora27' ]; then
    init="/usr/lib/systemd/systemd"
    opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    fi

    # Run the container using the supplied OS.
    printf ${green}"Starting Docker container: geerlingguy/docker-$distro-ansible."${neutral}"\n"
  16. geerlingguy revised this gist Jan 23, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ansible-role-test.sh
    Original file line number Diff line number Diff line change
    @@ -49,7 +49,7 @@ elif [ $distro = 'ubuntu1404' ]; then
    elif [ $distro = 'ubuntu1204' ]; then
    init="/sbin/init"
    opts="--privileged"
    # Debian 8
    # Debian 9
    elif [ $distro = 'debian9' ]; then
    init="/lib/systemd/systemd"
    opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
  17. geerlingguy revised this gist Jun 26, 2017. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions ansible-role-test.sh
    Original file line number Diff line number Diff line change
    @@ -50,6 +50,10 @@ elif [ $distro = 'ubuntu1204' ]; then
    init="/sbin/init"
    opts="--privileged"
    # Debian 8
    elif [ $distro = 'debian9' ]; then
    init="/lib/systemd/systemd"
    opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    # Debian 8
    elif [ $distro = 'debian8' ]; then
    init="/lib/systemd/systemd"
    opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
  18. geerlingguy revised this gist Jun 21, 2017. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions ansible-role-test.sh
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,8 @@
    # - cleanup: whether to remove the Docker container (default = true)
    # - container_id: the --name to set for the container (default = timestamp)
    # - test_idempotence: whether to test playbook's idempotence (default = true)
    #
    # License: MIT

    # Exit on any individual command failure.
    set -e
  19. geerlingguy revised this gist May 26, 2017. 1 changed file with 12 additions and 8 deletions.
    20 changes: 12 additions & 8 deletions ansible-role-test.sh
    Original file line number Diff line number Diff line change
    @@ -7,6 +7,7 @@
    # - playbook: a playbook in the tests directory (default = "test.yml")
    # - cleanup: whether to remove the Docker container (default = true)
    # - container_id: the --name to set for the container (default = timestamp)
    # - test_idempotence: whether to test playbook's idempotence (default = true)

    # Exit on any individual command failure.
    set -e
    @@ -23,6 +24,7 @@ distro=${distro:-"centos7"}
    playbook=${playbook:-"test.yml"}
    cleanup=${cleanup:-"true"}
    container_id=${container_id:-$timestamp}
    test_idempotence=${test_idempotence:-"true"}

    ## Set up vars for Docker setup.
    # CentOS 7
    @@ -80,14 +82,16 @@ printf "\n"
    printf ${green}"Running command: docker exec $container_id env TERM=xterm ansible-playbook /etc/ansible/roles/role_under_test/tests/$playbook"${neutral}
    docker exec $container_id env TERM=xterm env ANSIBLE_FORCE_COLOR=1 ansible-playbook /etc/ansible/roles/role_under_test/tests/$playbook

    # Run Ansible playbook again (idempotence test).
    printf ${green}"Running playbook again: idempotence test"${neutral}
    idempotence=$(mktemp)
    docker exec $container_id ansible-playbook /etc/ansible/roles/role_under_test/tests/$playbook | tee -a $idempotence
    tail $idempotence \
    | grep -q 'changed=0.*failed=0' \
    && (printf ${green}'Idempotence test: pass'${neutral}"\n") \
    || (printf ${red}'Idempotence test: fail'${neutral}"\n" && exit 1)
    if [ "$test_idempotence" = true ]; then
    # Run Ansible playbook again (idempotence test).
    printf ${green}"Running playbook again: idempotence test"${neutral}
    idempotence=$(mktemp)
    docker exec $container_id ansible-playbook /etc/ansible/roles/role_under_test/tests/$playbook | tee -a $idempotence
    tail $idempotence \
    | grep -q 'changed=0.*failed=0' \
    && (printf ${green}'Idempotence test: pass'${neutral}"\n") \
    || (printf ${red}'Idempotence test: fail'${neutral}"\n" && exit 1)
    fi

    # Remove the Docker container (if configured).
    if [ "$cleanup" = true ]; then
  20. geerlingguy revised this gist Apr 2, 2017. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions ansible-role-test.sh
    Original file line number Diff line number Diff line change
    @@ -32,19 +32,19 @@ if [ $distro = 'centos7' ]; then
    # CentOS 6
    elif [ $distro = 'centos6' ]; then
    init="/sbin/init"
    opts=""
    opts="--privileged"
    # Ubuntu 16.04
    elif [ $distro = 'ubuntu1604' ]; then
    init="/lib/systemd/systemd"
    opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    # Ubuntu 14.04
    elif [ $distro = 'ubuntu1404' ]; then
    init="/sbin/init"
    opts=""
    opts="--privileged"
    # Ubuntu 12.04
    elif [ $distro = 'ubuntu1204' ]; then
    init="/sbin/init"
    opts=""
    opts="--privileged"
    # Debian 8
    elif [ $distro = 'debian8' ]; then
    init="/lib/systemd/systemd"
  21. geerlingguy revised this gist Apr 2, 2017. 1 changed file with 18 additions and 15 deletions.
    33 changes: 18 additions & 15 deletions ansible-role-test.sh
    Original file line number Diff line number Diff line change
    @@ -2,9 +2,11 @@
    #
    # Ansible role test shim.
    #
    # Usage: test.sh [os] [playbook]
    # - [os] = One of the supported distros.
    # = [playbook] = The test playbook to run. (Defaults to test.yml if unset).
    # Usage: [OPTIONS] ./tests/test.sh
    # - distro: a supported Docker distro version (default = "centos7")
    # - playbook: a playbook in the tests directory (default = "test.yml")
    # - cleanup: whether to remove the Docker container (default = true)
    # - container_id: the --name to set for the container (default = timestamp)

    # Exit on any individual command failure.
    set -e
    @@ -14,9 +16,13 @@ red='\033[0;31m'
    green='\033[0;32m'
    neutral='\033[0m'

    # TODO: Check to make sure OS is provided.
    distro=$1
    playbook=${2:-"test.yml"}
    timestamp=$(date +%s)

    # Allow environment variables to override defaults.
    distro=${distro:-"centos7"}
    playbook=${playbook:-"test.yml"}
    cleanup=${cleanup:-"true"}
    container_id=${container_id:-$timestamp}

    ## Set up vars for Docker setup.
    # CentOS 7
    @@ -49,15 +55,10 @@ elif [ $distro = 'fedora24' ]; then
    opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    fi

    # Create a container ID.
    container_id=$(mktemp)

    # Run the container using the supplied OS.
    printf ${green}"Starting Docker container: geerlingguy/docker-$distro-ansible."${neutral}"\n"
    docker pull geerlingguy/docker-$distro-ansible:latest
    docker run --detach --volume="$PWD":/etc/ansible/roles/role_under_test:rw $opts geerlingguy/docker-$distro-ansible:latest $init > "$container_id"

    container_id=$(cat $container_id)
    docker run --detach --volume="$PWD":/etc/ansible/roles/role_under_test:rw --name $container_id $opts geerlingguy/docker-$distro-ansible:latest $init

    printf "\n"

    @@ -88,6 +89,8 @@ tail $idempotence \
    && (printf ${green}'Idempotence test: pass'${neutral}"\n") \
    || (printf ${red}'Idempotence test: fail'${neutral}"\n" && exit 1)

    # Kill the Docker container?
    printf "Removing Docker container...\n"
    docker rm -f $container_id
    # Remove the Docker container (if configured).
    if [ "$cleanup" = true ]; then
    printf "Removing Docker container...\n"
    docker rm -f $container_id
    fi
  22. geerlingguy revised this gist Apr 2, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ansible-role-test.sh
    Original file line number Diff line number Diff line change
    @@ -77,7 +77,7 @@ printf "\n"

    # Run Ansible playbook.
    printf ${green}"Running command: docker exec $container_id env TERM=xterm ansible-playbook /etc/ansible/roles/role_under_test/tests/$playbook"${neutral}
    docker exec --tty $container_id env TERM=xterm ansible-playbook /etc/ansible/roles/role_under_test/tests/$playbook
    docker exec $container_id env TERM=xterm env ANSIBLE_FORCE_COLOR=1 ansible-playbook /etc/ansible/roles/role_under_test/tests/$playbook

    # Run Ansible playbook again (idempotence test).
    printf ${green}"Running playbook again: idempotence test"${neutral}
  23. geerlingguy revised this gist Apr 1, 2017. 1 changed file with 8 additions and 5 deletions.
    13 changes: 8 additions & 5 deletions ansible-role-test.sh
    Original file line number Diff line number Diff line change
    @@ -61,14 +61,17 @@ container_id=$(cat $container_id)

    printf "\n"

    # Test Ansible syntax.
    printf ${green}"Checking Ansible playbook syntax."${neutral}
    docker exec --tty $container_id env TERM=xterm ansible-playbook /etc/ansible/roles/role_under_test/tests/$playbook --syntax-check
    # Install requirements if `requirements.yml` is present.
    if [ -f "$PWD/tests/requirements.yml" ]; then
    printf ${green}"Requirements file detected; installing dependencies."${neutral}"\n"
    docker exec --tty $container_id env TERM=xterm ansible-galaxy install -r /etc/ansible/roles/role_under_test/tests/requirements.yml
    fi

    printf "\n"

    # Install requirements if `requirements.yml` is present.
    # TODO
    # Test Ansible syntax.
    printf ${green}"Checking Ansible playbook syntax."${neutral}
    docker exec --tty $container_id env TERM=xterm ansible-playbook /etc/ansible/roles/role_under_test/tests/$playbook --syntax-check

    printf "\n"

  24. geerlingguy created this gist Apr 1, 2017.
    90 changes: 90 additions & 0 deletions ansible-role-test.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,90 @@
    #!/bin/bash
    #
    # Ansible role test shim.
    #
    # Usage: test.sh [os] [playbook]
    # - [os] = One of the supported distros.
    # = [playbook] = The test playbook to run. (Defaults to test.yml if unset).

    # Exit on any individual command failure.
    set -e

    # Pretty colors.
    red='\033[0;31m'
    green='\033[0;32m'
    neutral='\033[0m'

    # TODO: Check to make sure OS is provided.
    distro=$1
    playbook=${2:-"test.yml"}

    ## Set up vars for Docker setup.
    # CentOS 7
    if [ $distro = 'centos7' ]; then
    init="/usr/lib/systemd/systemd"
    opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    # CentOS 6
    elif [ $distro = 'centos6' ]; then
    init="/sbin/init"
    opts=""
    # Ubuntu 16.04
    elif [ $distro = 'ubuntu1604' ]; then
    init="/lib/systemd/systemd"
    opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    # Ubuntu 14.04
    elif [ $distro = 'ubuntu1404' ]; then
    init="/sbin/init"
    opts=""
    # Ubuntu 12.04
    elif [ $distro = 'ubuntu1204' ]; then
    init="/sbin/init"
    opts=""
    # Debian 8
    elif [ $distro = 'debian8' ]; then
    init="/lib/systemd/systemd"
    opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    # Fedora 24
    elif [ $distro = 'fedora24' ]; then
    init="/usr/lib/systemd/systemd"
    opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
    fi

    # Create a container ID.
    container_id=$(mktemp)

    # Run the container using the supplied OS.
    printf ${green}"Starting Docker container: geerlingguy/docker-$distro-ansible."${neutral}"\n"
    docker pull geerlingguy/docker-$distro-ansible:latest
    docker run --detach --volume="$PWD":/etc/ansible/roles/role_under_test:rw $opts geerlingguy/docker-$distro-ansible:latest $init > "$container_id"

    container_id=$(cat $container_id)

    printf "\n"

    # Test Ansible syntax.
    printf ${green}"Checking Ansible playbook syntax."${neutral}
    docker exec --tty $container_id env TERM=xterm ansible-playbook /etc/ansible/roles/role_under_test/tests/$playbook --syntax-check

    printf "\n"

    # Install requirements if `requirements.yml` is present.
    # TODO

    printf "\n"

    # Run Ansible playbook.
    printf ${green}"Running command: docker exec $container_id env TERM=xterm ansible-playbook /etc/ansible/roles/role_under_test/tests/$playbook"${neutral}
    docker exec --tty $container_id env TERM=xterm ansible-playbook /etc/ansible/roles/role_under_test/tests/$playbook

    # Run Ansible playbook again (idempotence test).
    printf ${green}"Running playbook again: idempotence test"${neutral}
    idempotence=$(mktemp)
    docker exec $container_id ansible-playbook /etc/ansible/roles/role_under_test/tests/$playbook | tee -a $idempotence
    tail $idempotence \
    | grep -q 'changed=0.*failed=0' \
    && (printf ${green}'Idempotence test: pass'${neutral}"\n") \
    || (printf ${red}'Idempotence test: fail'${neutral}"\n" && exit 1)

    # Kill the Docker container?
    printf "Removing Docker container...\n"
    docker rm -f $container_id