From e9d2d3e52cbb8856433d5ae4d10bd2f8d453bbc6 Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Thu, 16 May 2019 15:46:19 -0700 Subject: [PATCH] Vault is now working Getting closer to having Jenkins run Ansible playbooks. --- .gitignore | 4 + README.md | 29 +++++++ ansible.cfg | 4 +- dev.yml | 46 +++++++++++ hosts.template | 21 ----- jenkins/README.md | 4 - jenkins/auto-securityupdates.yml | 17 ----- playbook/linux/install-jenkins.yml | 6 ++ roles/geerlingguy.ansible/.gitignore | 3 + roles/geerlingguy.ansible/.travis.yml | 32 ++++++++ roles/geerlingguy.ansible/LICENSE | 20 +++++ roles/geerlingguy.ansible/README.md | 51 +++++++++++++ roles/geerlingguy.ansible/defaults/main.yml | 8 ++ .../meta/.galaxy_install_info | 2 + roles/geerlingguy.ansible/meta/main.yml | 30 ++++++++ .../molecule/default/molecule.yml | 29 +++++++ .../molecule/default/playbook-pip.yml | 12 +++ .../molecule/default/playbook.yml | 7 ++ .../molecule/default/requirements.yml | 2 + .../molecule/default/tests/test_default.py | 14 ++++ .../molecule/default/yaml-lint.yml | 6 ++ roles/geerlingguy.ansible/tasks/main.yml | 27 +++++++ .../tasks/setup-Debian.yml | 23 ++++++ .../tasks/setup-Fedora.yml | 5 ++ .../tasks/setup-RedHat.yml | 6 ++ .../tasks/setup-Ubuntu.yml | 11 +++ roles/geerlingguy.ansible/tasks/setup-pip.yml | 5 ++ roles/geerlingguy.pip/.gitignore | 3 + roles/geerlingguy.pip/.travis.yml | 29 +++++++ roles/geerlingguy.pip/LICENSE | 20 +++++ roles/geerlingguy.pip/README.md | 76 +++++++++++++++++++ roles/geerlingguy.pip/defaults/main.yml | 6 ++ .../geerlingguy.pip/meta/.galaxy_install_info | 2 + roles/geerlingguy.pip/meta/main.yml | 30 ++++++++ .../molecule/default/molecule.yml | 29 +++++++ .../molecule/default/playbook.yml | 20 +++++ .../molecule/default/tests/test_default.py | 14 ++++ .../molecule/default/yaml-lint.yml | 6 ++ roles/geerlingguy.pip/tasks/main.yml | 14 ++++ templates/hosts.yml | 28 +++++++ 40 files changed, 657 insertions(+), 44 deletions(-) create mode 100644 dev.yml delete mode 100644 hosts.template delete mode 100644 jenkins/README.md delete mode 100644 jenkins/auto-securityupdates.yml create mode 100644 roles/geerlingguy.ansible/.gitignore create mode 100644 roles/geerlingguy.ansible/.travis.yml create mode 100644 roles/geerlingguy.ansible/LICENSE create mode 100644 roles/geerlingguy.ansible/README.md create mode 100644 roles/geerlingguy.ansible/defaults/main.yml create mode 100644 roles/geerlingguy.ansible/meta/.galaxy_install_info create mode 100644 roles/geerlingguy.ansible/meta/main.yml create mode 100644 roles/geerlingguy.ansible/molecule/default/molecule.yml create mode 100644 roles/geerlingguy.ansible/molecule/default/playbook-pip.yml create mode 100644 roles/geerlingguy.ansible/molecule/default/playbook.yml create mode 100644 roles/geerlingguy.ansible/molecule/default/requirements.yml create mode 100644 roles/geerlingguy.ansible/molecule/default/tests/test_default.py create mode 100644 roles/geerlingguy.ansible/molecule/default/yaml-lint.yml create mode 100644 roles/geerlingguy.ansible/tasks/main.yml create mode 100644 roles/geerlingguy.ansible/tasks/setup-Debian.yml create mode 100644 roles/geerlingguy.ansible/tasks/setup-Fedora.yml create mode 100644 roles/geerlingguy.ansible/tasks/setup-RedHat.yml create mode 100644 roles/geerlingguy.ansible/tasks/setup-Ubuntu.yml create mode 100644 roles/geerlingguy.ansible/tasks/setup-pip.yml create mode 100644 roles/geerlingguy.pip/.gitignore create mode 100644 roles/geerlingguy.pip/.travis.yml create mode 100644 roles/geerlingguy.pip/LICENSE create mode 100644 roles/geerlingguy.pip/README.md create mode 100644 roles/geerlingguy.pip/defaults/main.yml create mode 100644 roles/geerlingguy.pip/meta/.galaxy_install_info create mode 100644 roles/geerlingguy.pip/meta/main.yml create mode 100644 roles/geerlingguy.pip/molecule/default/molecule.yml create mode 100644 roles/geerlingguy.pip/molecule/default/playbook.yml create mode 100644 roles/geerlingguy.pip/molecule/default/tests/test_default.py create mode 100644 roles/geerlingguy.pip/molecule/default/yaml-lint.yml create mode 100644 roles/geerlingguy.pip/tasks/main.yml create mode 100644 templates/hosts.yml diff --git a/.gitignore b/.gitignore index 9660fda..2aeee11 100644 --- a/.gitignore +++ b/.gitignore @@ -12,5 +12,9 @@ hosts win_hosts hosts.d +# Ansible vault secret +# Used to decrypt the vault files +.ansible_vault + # Vagrant .vagrant diff --git a/README.md b/README.md index d07295a..cb3c907 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,14 @@ This repo contains my configuration and setup for my ansible use. Use at your own risk. +1. Install Ansible on development device +2. Clone this repository to work in +3. Configure devices to remote into +4. Configure Ansible's Inventory files +5. Set Vault Secret +6. Run Connection tests + + ## Installers ### New hosts @@ -29,10 +37,31 @@ I have a basic configuration file in place at the root of this folder for anisbl ## Inventory +Inventory files have been moved over to .yml format. The ansibe.cfg is looking for ./dev.yml as it's default inventory file. For Prod use make another file that will contain all servers that will be managed. + I have a template file in place that should only be used as a reference. Make a copy of that file and name it hosts then update that file. The configuration file that is active is looking for a directory that contains all of the inventory files. This way all files can be parted out rather then one big file. +## Vault Secret + +The configuration file is looking for ./.ansible_vault file to contain the secret for vault entries. Git is already configured to ignore this file. You will need to make this file and place your key in it so ansible +can decrypt vaults as needed. + +Run the following command and replace secret with your password. Once that is done move on to generating the encrypted strings. + +```shell +echo 'secret' > ./.ansible_vault +``` + +To generate secure strings for the inventory file run the following command. + +```shell +ansible-vault encrypt_string 'secret' +``` + +This will output the value that needs to be placed + ### Testing Linux devices ```bash diff --git a/ansible.cfg b/ansible.cfg index 890b065..8afe035 100644 --- a/ansible.cfg +++ b/ansible.cfg @@ -11,7 +11,7 @@ # some basic default values... -inventory = ./hosts.d/ +inventory = ./test.yml library = /usr/share/my_modules/ module_utils = /usr/share/my_module_utils/ remote_tmp = ~/.ansible/tmp @@ -137,7 +137,7 @@ roles_path = ./roles/:/etc/ansible/roles # If set, configures the path to the Vault password file as an alternative to # specifying --vault-password-file on the command line. -#vault_password_file = /path/to/vault_password_file +vault_password_file = ./.ansible_vault # format of string {{ ansible_managed }} available within Jinja2 # templates indicates to users editing templates files will be replaced. diff --git a/dev.yml b/dev.yml new file mode 100644 index 0000000..7296023 --- /dev/null +++ b/dev.yml @@ -0,0 +1,46 @@ +--- +all: + hosts: + dmi-dev01: + children: + linux: + hosts: + 172.20.0.142: + vars: + ansible_user: ansible + ansible_connection: ssh + ansible_password: !vault | + $ANSIBLE_VAULT;1.1;AES256 + 33353561613531336536313335356236643530346538373638653330306636386435633965336136 + 3464366635373661383466333464663238663565343839310a376666386237313566386235633739 + 66323434636365303335326133396137393031396531313533326263363066636237313139353538 + 6134616232336365610a363033343639616563316330363966326330646162613034633532366230 + 3439 + ansible_become_method: sudo + ansible_become_pass: !vault | + $ANSIBLE_VAULT;1.1;AES256 + 33353561613531336536313335356236643530346538373638653330306636386435633965336136 + 3464366635373661383466333464663238663565343839310a376666386237313566386235633739 + 66323434636365303335326133396137393031396531313533326263363066636237313139353538 + 6134616232336365610a363033343639616563316330363966326330646162613034633532366230 + 3439 + ansible_python_interpreter: /usr/bin/python3 + windows: + hosts: + dmi-dev01: + vars: + ansible_user: svcansible + ansible_password: !vault | + $ANSIBLE_VAULT;1.1;AES256 + 33353561613531336536313335356236643530346538373638653330306636386435633965336136 + 3464366635373661383466333464663238663565343839310a376666386237313566386235633739 + 66323434636365303335326133396137393031396531313533326263363066636237313139353538 + 6134616232336365610a363033343639616563316330363966326330646162613034633532366230 + 3439 + ansible_connection: winrm + ansible_port: 5985 + ansible_winrm_scheme: http + ansible_winrm_transport: ntlm + ansible_winrm_server_cert_validation: ignore + + diff --git a/hosts.template b/hosts.template deleted file mode 100644 index a826df0..0000000 --- a/hosts.template +++ /dev/null @@ -1,21 +0,0 @@ -[linux] -192.168.0.60 - -[linux:vars] -ansible_user=ansible -ansible_connection=ssh -ansible_password= -ansible_become_method=sudo -ansible_become_pass= -#ansible_python_interpreter=/usr/bin/python3 - -[windows] -192.168.0.2 - -[windows:vars] -ansible_user=ansible -ansible_password= -ansible_connection=winrm -ansible_port=5986 -ansible_winrm_scheme=https -ansible_winrm_server_cert_validation=ignore diff --git a/jenkins/README.md b/jenkins/README.md deleted file mode 100644 index 11faea7..0000000 --- a/jenkins/README.md +++ /dev/null @@ -1,4 +0,0 @@ -# Jenkins - -This is a folder that contains playbooks that will executed with Jenkins when code is pushed to master. - diff --git a/jenkins/auto-securityupdates.yml b/jenkins/auto-securityupdates.yml deleted file mode 100644 index 122e948..0000000 --- a/jenkins/auto-securityupdates.yml +++ /dev/null @@ -1,17 +0,0 @@ - ---- - -- name: enable - hosts: linux - - tasks: - # https://galaxy.ansible.com/jnv/unattended-upgrades - - name: unattended-upgrades - become: true - include_role: - name: jnv.unattended-upgrades - vars: - #unattended_package_blacklist: [] - unattended_automatic_reboot: true - - diff --git a/playbook/linux/install-jenkins.yml b/playbook/linux/install-jenkins.yml index 41e81a4..7ae7c83 100644 --- a/playbook/linux/install-jenkins.yml +++ b/playbook/linux/install-jenkins.yml @@ -10,6 +10,12 @@ import_role: name: geerlingguy.java + - name: Backup Jenkins Jobs + become: yes + fetch: + src: /var/lib/jenkins/jobs/ + dest: ./jenkins/jobs/ + - name: Install Jenkins become: yes import_role: diff --git a/roles/geerlingguy.ansible/.gitignore b/roles/geerlingguy.ansible/.gitignore new file mode 100644 index 0000000..f56f5b5 --- /dev/null +++ b/roles/geerlingguy.ansible/.gitignore @@ -0,0 +1,3 @@ +*.retry +*/__pycache__ +*.pyc diff --git a/roles/geerlingguy.ansible/.travis.yml b/roles/geerlingguy.ansible/.travis.yml new file mode 100644 index 0000000..b9fdd0d --- /dev/null +++ b/roles/geerlingguy.ansible/.travis.yml @@ -0,0 +1,32 @@ +--- +language: python +services: docker + +env: + global: + - ROLE_NAME: ansible + matrix: + - MOLECULE_DISTRO: centos7 + - MOLECULE_DISTRO: fedora29 + - MOLECULE_DISTRO: ubuntu1804 + - MOLECULE_DISTRO: debian9 + + - MOLECULE_DISTRO: centos7 + MOLECULE_PLAYBOOK: playbook-pip.yml + +install: + # Install test dependencies. + - pip install molecule docker + +before_script: + # Use actual Ansible Galaxy role name for the project directory. + - cd ../ + - mv ansible-role-$ROLE_NAME geerlingguy.$ROLE_NAME + - cd geerlingguy.$ROLE_NAME + +script: + # Run tests. + - molecule test + +notifications: + webhooks: https://galaxy.ansible.com/api/v1/notifications/ diff --git a/roles/geerlingguy.ansible/LICENSE b/roles/geerlingguy.ansible/LICENSE new file mode 100644 index 0000000..4275cf3 --- /dev/null +++ b/roles/geerlingguy.ansible/LICENSE @@ -0,0 +1,20 @@ +The MIT License (MIT) + +Copyright (c) 2017 Jeff Geerling + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/roles/geerlingguy.ansible/README.md b/roles/geerlingguy.ansible/README.md new file mode 100644 index 0000000..c9bd504 --- /dev/null +++ b/roles/geerlingguy.ansible/README.md @@ -0,0 +1,51 @@ +# Ansible Role: Ansible + +[![Build Status](https://travis-ci.org/geerlingguy/ansible-role-ansible.svg?branch=master)](https://travis-ci.org/geerlingguy/ansible-role-ansible) + +An Ansible Role that installs Ansible on Linux servers. + +## Requirements + +If using on a RedHat/CentOS-based host, make sure you've added the EPEL repository (it can easily be installed by including the `geerlingguy.repo-epel` role on Ansible Galaxy). + +## Role Variables + +Available variables are listed below, along with default values (see `defaults/main.yml`): + + ansible_install_method: package + +Whether to install Ansible via the system `package` manager (`apt`, `yum`, `dnf`, etc.), or via `pip`. If set to `pip`, you need to make sure Pip is installed prior to running this role. You can use the `geerlingguy.pip` module to install Pip easily. + + ansible_install_version_pip: '' + +If `ansible_install_method` is set to `pip`, the specific Ansible version to be installed via Pip. If not set, the latest version of Ansible will be installed. + +## Dependencies + +None. + +## Example Playbook + +Install from the system package manager: + + - hosts: servers + roles: + - role: geerlingguy.ansible + +Install from pip: + + - hosts: servers + vars: + ansible_install_method: pip + ansible_install_version_pip: "2.7.0" + roles: + - role: geerlingguy.pip + - role: geerlingguy.ansible + +## License + +MIT / BSD + +## Author Information + +This role was created in 2014 by [Jeff Geerling](https://www.jeffgeerling.com/), author of [Ansible for DevOps](https://www.ansiblefordevops.com/). diff --git a/roles/geerlingguy.ansible/defaults/main.yml b/roles/geerlingguy.ansible/defaults/main.yml new file mode 100644 index 0000000..5898c33 --- /dev/null +++ b/roles/geerlingguy.ansible/defaults/main.yml @@ -0,0 +1,8 @@ +--- +ansible_default_release: "" + +# Valid options include: 'package' or 'pip'. +ansible_install_method: package + +# Used only if ansible_install_method is 'pip'. If empty, defaults to latest. +ansible_install_version_pip: '' diff --git a/roles/geerlingguy.ansible/meta/.galaxy_install_info b/roles/geerlingguy.ansible/meta/.galaxy_install_info new file mode 100644 index 0000000..97ece6d --- /dev/null +++ b/roles/geerlingguy.ansible/meta/.galaxy_install_info @@ -0,0 +1,2 @@ +install_date: Tue May 14 21:40:21 2019 +version: 2.0.1 diff --git a/roles/geerlingguy.ansible/meta/main.yml b/roles/geerlingguy.ansible/meta/main.yml new file mode 100644 index 0000000..ef118f4 --- /dev/null +++ b/roles/geerlingguy.ansible/meta/main.yml @@ -0,0 +1,30 @@ +--- +dependencies: [] + +galaxy_info: + author: geerlingguy + description: Ansible for RedHat/CentOS/Debian/Ubuntu. + company: "Midwestern Mac, LLC" + license: "license (BSD, MIT)" + min_ansible_version: 2.4 + platforms: + - name: EL + versions: + - 6 + - 7 + - name: Fedora + versions: + - all + - name: Debian + versions: + - all + - name: Ubuntu + versions: + - all + galaxy_tags: + - system + - packaging + - development + - cloud + - ansible + - automation diff --git a/roles/geerlingguy.ansible/molecule/default/molecule.yml b/roles/geerlingguy.ansible/molecule/default/molecule.yml new file mode 100644 index 0000000..2ca6fea --- /dev/null +++ b/roles/geerlingguy.ansible/molecule/default/molecule.yml @@ -0,0 +1,29 @@ +--- +dependency: + name: galaxy +driver: + name: docker +lint: + name: yamllint + options: + config-file: molecule/default/yaml-lint.yml +platforms: + - name: instance + image: "geerlingguy/docker-${MOLECULE_DISTRO:-centos7}-ansible:latest" + command: ${MOLECULE_DOCKER_COMMAND:-""} + volumes: + - /sys/fs/cgroup:/sys/fs/cgroup:ro + privileged: true + pre_build_image: true +provisioner: + name: ansible + lint: + name: ansible-lint + playbooks: + converge: ${MOLECULE_PLAYBOOK:-playbook.yml} +scenario: + name: default +verifier: + name: testinfra + lint: + name: flake8 diff --git a/roles/geerlingguy.ansible/molecule/default/playbook-pip.yml b/roles/geerlingguy.ansible/molecule/default/playbook-pip.yml new file mode 100644 index 0000000..60e298e --- /dev/null +++ b/roles/geerlingguy.ansible/molecule/default/playbook-pip.yml @@ -0,0 +1,12 @@ +--- +- name: Converge + hosts: all + become: true + + vars: + ansible_install_method: pip + ansible_install_version_pip: "2.6.2" + + roles: + - role: geerlingguy.pip + - role: geerlingguy.ansible diff --git a/roles/geerlingguy.ansible/molecule/default/playbook.yml b/roles/geerlingguy.ansible/molecule/default/playbook.yml new file mode 100644 index 0000000..4ceb781 --- /dev/null +++ b/roles/geerlingguy.ansible/molecule/default/playbook.yml @@ -0,0 +1,7 @@ +--- +- name: Converge + hosts: all + become: true + + roles: + - role: geerlingguy.ansible diff --git a/roles/geerlingguy.ansible/molecule/default/requirements.yml b/roles/geerlingguy.ansible/molecule/default/requirements.yml new file mode 100644 index 0000000..963f84b --- /dev/null +++ b/roles/geerlingguy.ansible/molecule/default/requirements.yml @@ -0,0 +1,2 @@ +--- +- role: geerlingguy.pip diff --git a/roles/geerlingguy.ansible/molecule/default/tests/test_default.py b/roles/geerlingguy.ansible/molecule/default/tests/test_default.py new file mode 100644 index 0000000..eedd64a --- /dev/null +++ b/roles/geerlingguy.ansible/molecule/default/tests/test_default.py @@ -0,0 +1,14 @@ +import os + +import testinfra.utils.ansible_runner + +testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( + os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all') + + +def test_hosts_file(host): + f = host.file('/etc/hosts') + + assert f.exists + assert f.user == 'root' + assert f.group == 'root' diff --git a/roles/geerlingguy.ansible/molecule/default/yaml-lint.yml b/roles/geerlingguy.ansible/molecule/default/yaml-lint.yml new file mode 100644 index 0000000..a3dbc38 --- /dev/null +++ b/roles/geerlingguy.ansible/molecule/default/yaml-lint.yml @@ -0,0 +1,6 @@ +--- +extends: default +rules: + line-length: + max: 120 + level: warning diff --git a/roles/geerlingguy.ansible/tasks/main.yml b/roles/geerlingguy.ansible/tasks/main.yml new file mode 100644 index 0000000..3fbc2c6 --- /dev/null +++ b/roles/geerlingguy.ansible/tasks/main.yml @@ -0,0 +1,27 @@ +--- +- name: Set the package state based on how Ansible is installed. + set_fact: + ansible_package_state: "{{ 'present' if ansible_install_method == 'package' else 'absent' }}" + +# Setup/install tasks. +- name: Set up Ansible on RedHat. + include_tasks: setup-RedHat.yml + when: + - ansible_os_family == 'RedHat' + - ansible_distribution != 'Fedora' + +- name: Set up Ansible on Fedora. + include_tasks: setup-Fedora.yml + when: ansible_distribution == 'Fedora' + +- name: Set up Ansible on Ubuntu. + include_tasks: setup-Ubuntu.yml + when: ansible_distribution == 'Ubuntu' + +- name: Set up Ansible on Debian. + include_tasks: setup-Debian.yml + when: ansible_distribution == 'Debian' + +- name: Set up Ansible using Pip. + include_tasks: setup-pip.yml + when: ansible_install_method == 'pip' diff --git a/roles/geerlingguy.ansible/tasks/setup-Debian.yml b/roles/geerlingguy.ansible/tasks/setup-Debian.yml new file mode 100644 index 0000000..b4e349f --- /dev/null +++ b/roles/geerlingguy.ansible/tasks/setup-Debian.yml @@ -0,0 +1,23 @@ +--- +- name: Enable Backports repository. + apt_repository: + repo: >- + deb http://ftp.debian.org/debian + {{ ansible_distribution_release }}-backports main' + state: present + filename: "{{ ansible_distribution_release }}_backports" + when: ansible_distribution_version | int < 9 + +- name: Update apt cache. + apt: update_cache=true cache_valid_time=86400 + +- name: Set the default_release option for older Debian versions. + set_fact: + ansible_default_release: "{{ ansible_distribution_release }}-backports" + when: ansible_distribution_version | int < 9 + +- name: Install Ansible. + apt: + name: ansible + state: "{{ ansible_package_state }}" + default_release: "{{ ansible_default_release }}" diff --git a/roles/geerlingguy.ansible/tasks/setup-Fedora.yml b/roles/geerlingguy.ansible/tasks/setup-Fedora.yml new file mode 100644 index 0000000..7d56261 --- /dev/null +++ b/roles/geerlingguy.ansible/tasks/setup-Fedora.yml @@ -0,0 +1,5 @@ +--- +- name: Install Ansible. + package: + name: ansible + state: "{{ ansible_package_state }}" diff --git a/roles/geerlingguy.ansible/tasks/setup-RedHat.yml b/roles/geerlingguy.ansible/tasks/setup-RedHat.yml new file mode 100644 index 0000000..2f1221d --- /dev/null +++ b/roles/geerlingguy.ansible/tasks/setup-RedHat.yml @@ -0,0 +1,6 @@ +--- +- name: Install Ansible. + yum: + name: ansible + state: "{{ ansible_package_state }}" + enablerepo: epel diff --git a/roles/geerlingguy.ansible/tasks/setup-Ubuntu.yml b/roles/geerlingguy.ansible/tasks/setup-Ubuntu.yml new file mode 100644 index 0000000..2c9b2fa --- /dev/null +++ b/roles/geerlingguy.ansible/tasks/setup-Ubuntu.yml @@ -0,0 +1,11 @@ +--- +- name: Add ansible repository. + apt_repository: repo='ppa:ansible/ansible' + +- name: Update apt cache. + apt: update_cache=true cache_valid_time=86400 + +- name: Install Ansible. + apt: + name: ansible + state: "{{ ansible_package_state }}" diff --git a/roles/geerlingguy.ansible/tasks/setup-pip.yml b/roles/geerlingguy.ansible/tasks/setup-pip.yml new file mode 100644 index 0000000..a91049b --- /dev/null +++ b/roles/geerlingguy.ansible/tasks/setup-pip.yml @@ -0,0 +1,5 @@ +--- +- name: Install Ansible via Pip. + pip: + name: ansible + version: "{{ ansible_install_version_pip | default(omit) }}" diff --git a/roles/geerlingguy.pip/.gitignore b/roles/geerlingguy.pip/.gitignore new file mode 100644 index 0000000..f56f5b5 --- /dev/null +++ b/roles/geerlingguy.pip/.gitignore @@ -0,0 +1,3 @@ +*.retry +*/__pycache__ +*.pyc diff --git a/roles/geerlingguy.pip/.travis.yml b/roles/geerlingguy.pip/.travis.yml new file mode 100644 index 0000000..906b30d --- /dev/null +++ b/roles/geerlingguy.pip/.travis.yml @@ -0,0 +1,29 @@ +--- +language: python +services: docker + +env: + global: + - ROLE_NAME: pip + matrix: + - MOLECULE_DISTRO: centos7 + - MOLECULE_DISTRO: fedora29 + - MOLECULE_DISTRO: ubuntu1804 + - MOLECULE_DISTRO: debian9 + +install: + # Install test dependencies. + - pip install molecule docker + +before_script: + # Use actual Ansible Galaxy role name for the project directory. + - cd ../ + - mv ansible-role-$ROLE_NAME geerlingguy.$ROLE_NAME + - cd geerlingguy.$ROLE_NAME + +script: + # Run tests. + - molecule test + +notifications: + webhooks: https://galaxy.ansible.com/api/v1/notifications/ diff --git a/roles/geerlingguy.pip/LICENSE b/roles/geerlingguy.pip/LICENSE new file mode 100644 index 0000000..4275cf3 --- /dev/null +++ b/roles/geerlingguy.pip/LICENSE @@ -0,0 +1,20 @@ +The MIT License (MIT) + +Copyright (c) 2017 Jeff Geerling + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/roles/geerlingguy.pip/README.md b/roles/geerlingguy.pip/README.md new file mode 100644 index 0000000..ec9beb9 --- /dev/null +++ b/roles/geerlingguy.pip/README.md @@ -0,0 +1,76 @@ +# Ansible Role: Pip (for Python) + +[![Build Status](https://travis-ci.org/geerlingguy/ansible-role-pip.svg?branch=master)](https://travis-ci.org/geerlingguy/ansible-role-pip) + +An Ansible Role that installs [Pip](https://pip.pypa.io) on Linux. + +## Requirements + +On RedHat/CentOS, you may need to have EPEL installed before running this role. You can use the `geerlingguy.repo-epel` role if you need a simple way to ensure it's installed. + +## Role Variables + +Available variables are listed below, along with default values (see `defaults/main.yml`): + + pip_package: python-pip + +The name of the packge to install to get `pip` on the system. You can set to `python3-pip`, for example, when using Python 3 on Ubuntu. + + pip_executable: pip + +The role will try to autodetect the pip executable based on the `pip_package` (e.g. `pip` for Python 2 and `pip3` for Python 3). You can also override this explicitly, e.g. `pip_executable: pip3.6`. + + pip_install_packages: [] + +A list of packages to install with pip. Examples below: + + pip_install_packages: + # Specify names and versions. + - name: docker + version: "1.2.3" + - name: awscli + version: "1.11.91" + + # Or specify bare packages to get the latest release. + - docker + - awscli + + # Or uninstall a package. + - name: docker + state: absent + + # Or update a package ot the latest version. + - name: docker + state: latest + + # Or force a reinstall. + - name: docker + state: forcereinstall + + # Or install a package in a particular virtualenv. + - name: docker + virtualenv: /my_app/venv + +## Dependencies + +None. + +## Example Playbook + + - hosts: all + + vars: + pip_install_packages: + - name: docker + - name: awscli + + roles: + - geerlingguy.pip + +## License + +MIT / BSD + +## Author Information + +This role was created in 2017 by [Jeff Geerling](https://www.jeffgeerling.com/), author of [Ansible for DevOps](https://www.ansiblefordevops.com/). diff --git a/roles/geerlingguy.pip/defaults/main.yml b/roles/geerlingguy.pip/defaults/main.yml new file mode 100644 index 0000000..8a70a1e --- /dev/null +++ b/roles/geerlingguy.pip/defaults/main.yml @@ -0,0 +1,6 @@ +--- +# For Python 3, use python3-pip. +pip_package: python-pip +pip_executable: "{{ 'pip3' if pip_package.startswith('python3') else 'pip' }}" + +pip_install_packages: [] diff --git a/roles/geerlingguy.pip/meta/.galaxy_install_info b/roles/geerlingguy.pip/meta/.galaxy_install_info new file mode 100644 index 0000000..ebd8011 --- /dev/null +++ b/roles/geerlingguy.pip/meta/.galaxy_install_info @@ -0,0 +1,2 @@ +install_date: Tue May 14 21:41:10 2019 +version: 1.3.0 diff --git a/roles/geerlingguy.pip/meta/main.yml b/roles/geerlingguy.pip/meta/main.yml new file mode 100644 index 0000000..eb4248b --- /dev/null +++ b/roles/geerlingguy.pip/meta/main.yml @@ -0,0 +1,30 @@ +--- +dependencies: [] + +galaxy_info: + author: geerlingguy + description: Pip (Python package manager) for Linux. + issue_tracker_url: https://github.com/geerlingguy/ansible-role-pip/issues + company: "Midwestern Mac, LLC" + license: "license (BSD, MIT)" + min_ansible_version: 2.0 + platforms: + - name: EL + versions: + - all + - name: Fedora + versions: + - all + - name: Debian + versions: + - all + - name: Ubuntu + versions: + - all + galaxy_tags: + - system + - server + - packaging + - python + - pip + - tools diff --git a/roles/geerlingguy.pip/molecule/default/molecule.yml b/roles/geerlingguy.pip/molecule/default/molecule.yml new file mode 100644 index 0000000..2ca6fea --- /dev/null +++ b/roles/geerlingguy.pip/molecule/default/molecule.yml @@ -0,0 +1,29 @@ +--- +dependency: + name: galaxy +driver: + name: docker +lint: + name: yamllint + options: + config-file: molecule/default/yaml-lint.yml +platforms: + - name: instance + image: "geerlingguy/docker-${MOLECULE_DISTRO:-centos7}-ansible:latest" + command: ${MOLECULE_DOCKER_COMMAND:-""} + volumes: + - /sys/fs/cgroup:/sys/fs/cgroup:ro + privileged: true + pre_build_image: true +provisioner: + name: ansible + lint: + name: ansible-lint + playbooks: + converge: ${MOLECULE_PLAYBOOK:-playbook.yml} +scenario: + name: default +verifier: + name: testinfra + lint: + name: flake8 diff --git a/roles/geerlingguy.pip/molecule/default/playbook.yml b/roles/geerlingguy.pip/molecule/default/playbook.yml new file mode 100644 index 0000000..6ffa90b --- /dev/null +++ b/roles/geerlingguy.pip/molecule/default/playbook.yml @@ -0,0 +1,20 @@ +--- +- name: Converge + hosts: all + become: true + + vars: + pip_install_packages: + # Test installing a specific version of a package. + - name: ipaddress + version: "1.0.18" + # Test installing a package by name. + - colorama + + pre_tasks: + - name: Update apt cache. + apt: update_cache=true cache_valid_time=600 + when: ansible_os_family == 'Debian' + + roles: + - role: geerlingguy.pip diff --git a/roles/geerlingguy.pip/molecule/default/tests/test_default.py b/roles/geerlingguy.pip/molecule/default/tests/test_default.py new file mode 100644 index 0000000..eedd64a --- /dev/null +++ b/roles/geerlingguy.pip/molecule/default/tests/test_default.py @@ -0,0 +1,14 @@ +import os + +import testinfra.utils.ansible_runner + +testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( + os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all') + + +def test_hosts_file(host): + f = host.file('/etc/hosts') + + assert f.exists + assert f.user == 'root' + assert f.group == 'root' diff --git a/roles/geerlingguy.pip/molecule/default/yaml-lint.yml b/roles/geerlingguy.pip/molecule/default/yaml-lint.yml new file mode 100644 index 0000000..a3dbc38 --- /dev/null +++ b/roles/geerlingguy.pip/molecule/default/yaml-lint.yml @@ -0,0 +1,6 @@ +--- +extends: default +rules: + line-length: + max: 120 + level: warning diff --git a/roles/geerlingguy.pip/tasks/main.yml b/roles/geerlingguy.pip/tasks/main.yml new file mode 100644 index 0000000..dda7fac --- /dev/null +++ b/roles/geerlingguy.pip/tasks/main.yml @@ -0,0 +1,14 @@ +--- +- name: Ensure Pip is installed. + package: + name: "{{ pip_package }}" + state: present + +- name: Ensure pip_install_packages are installed. + pip: + name: "{{ item.name | default(item) }}" + version: "{{ item.version | default(omit) }}" + virtualenv: "{{ item.virtualenv | default(omit) }}" + state: "{{ item.state | default(omit) }}" + executable: "{{ pip_executable }}" + with_items: "{{ pip_install_packages }}" diff --git a/templates/hosts.yml b/templates/hosts.yml new file mode 100644 index 0000000..f5d1bed --- /dev/null +++ b/templates/hosts.yml @@ -0,0 +1,28 @@ +--- +all: + hosts: + dmi-dev01: + children: + linux: + hosts: + 172.20.0.142: + vars: + ansible_user: ansible + ansible_connection: ssh + ansible_password: + ansible_become_method: sudo + ansible_become_pass: + ansible_python_interpreter: /usr/bin/python3 + windows: + hosts: + dev01: + vars: + ansible_user: ansible + ansible_password: + ansible_connection: winrm + ansible_port: 5985 + ansible_winrm_scheme: http + ansible_winrm_transport: ntlm + ansible_winrm_server_cert_validation: ignore + +