diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index bf79ab7c..256a9750 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -5,34 +5,135 @@ name: CI push: branches: - master + schedule: + - cron: "10 2 * * 0" jobs: + scripts: + name: Scripts + runs-on: ubuntu-latest + strategy: + matrix: + include: + - test_script: collection + - test_script: molecule + - test_script: orchestration + + steps: + - uses: actions/checkout@v2 + + - uses: actions/setup-python@v2 + with: + python-version: '3.x' - test: - name: Molecule + - name: Run test script. + run: tests/${{ matrix.test_script }}.sh + + playbooks: + name: Playbooks runs-on: ubuntu-latest strategy: matrix: - distro: - - centos8 - - debian10 + include: + - playbook: deployments.yml + distro: ubuntu2004 + test_idempotence: false + + # TODO: Not easy to test in CI at this time. + # - playbook: deployments-balancer.yml + # distro: ubuntu2004 + + # TODO: This started failing on GitHub Actions. + # See: https://github.com/geerlingguy/ansible-for-devops/issues/359 + # - playbook: deployments-rolling.yml + # distro: ubuntu2004 + + - playbook: docker.yml + distro: ubuntu2004 + test_idempotence: false + + - playbook: docker-flask.yml + distro: ubuntu2004 + test_idempotence: false + + - playbook: docker-hubot.yml + distro: ubuntu2004 + test_idempotence: false + + - playbook: drupal.yml + distro: ubuntu2004 + + # TODO: Not easy to test in CI at this time. + # - playbook: dynamic-inventory.yml + # distro: ubuntu2004 + + - playbook: elk.yml + distro: ubuntu2004 + + - playbook: first-ansible-playbook.yml + distro: centos7 + + - playbook: galaxy-role-servers.yml + distro: ubuntu2004 + + # TODO: Not easy to test in CI at this time. + # - playbook: gluster.yml + # distro: ubuntu2004 + + - playbook: https-self-signed.yml + distro: ubuntu2004 + + - playbook: https-nginx-proxy.yml + distro: debian10 + + # TODO: This started failing on GitHub Actions. + # See: https://github.com/geerlingguy/ansible-for-devops/issues/359 + # - playbook: includes.yml + # distro: ubuntu2004 + + # TODO: This started failing on GitHub Actions. + # See: https://github.com/geerlingguy/ansible-for-devops/issues/359 + # - playbook: jenkins.yml + # distro: ubuntu2004 + + # TODO: Not easy to test in CI at this time. + # - playbook: kubernetes.yml + # distro: debian9 + + # TODO: Not easy to test in CI at this time. + # - playbook: lamp-infrastructure.yml + # distro: ubuntu2004 + + - playbook: nodejs.yml + distro: centos7 + + - playbook: nodejs-role.yml + distro: centos7 + + - playbook: security.yml + distro: centos8 + + - playbook: solr.yml + distro: ubuntu2004 + + - playbook: test-plugin.yml + distro: centos7 steps: - - name: Check out the codebase. - uses: actions/checkout@v2 + - uses: actions/checkout@v2 - - name: Set up Python 3. - uses: actions/setup-python@v2 + - uses: actions/setup-python@v2 with: python-version: '3.x' - - name: Install test dependencies. - run: pip3 install molecule docker yamllint ansible-lint + - name: Download test shim. + run: | + wget -O tests/test.sh https://gist.githubusercontent.com/geerlingguy/73ef1e5ee45d8694570f334be385e181/raw/ + chmod +x tests/test.sh - - name: Run Molecule tests. - run: molecule test + - name: Run playbook with test shim. + run: tests/test.sh env: - PY_COLORS: '1' - ANSIBLE_FORCE_COLOR: '1' - MOLECULE_DISTRO: ${{ matrix.distro }} - working-directory: molecule + playbook: ${{ matrix.playbook }} + distro: ${{ matrix.distro }} + test_idempotence: ${{ matrix.test_idempotence }} diff --git a/.github/workflows/molecule-ci.yml b/.github/workflows/molecule-ci.yml new file mode 100644 index 00000000..dbf100cd --- /dev/null +++ b/.github/workflows/molecule-ci.yml @@ -0,0 +1,38 @@ +--- +name: Molecule CI +'on': + pull_request: + push: + branches: + - master + +jobs: + + test: + name: Molecule + runs-on: ubuntu-latest + strategy: + matrix: + distro: + - centos8 + - debian10 + + steps: + - name: Check out the codebase. + uses: actions/checkout@v2 + + - name: Set up Python 3. + uses: actions/setup-python@v2 + with: + python-version: '3.x' + + - name: Install test dependencies. + run: pip3 install molecule[docker] yamllint ansible-lint + + - name: Run Molecule tests. + run: molecule test + env: + PY_COLORS: '1' + ANSIBLE_FORCE_COLOR: '1' + MOLECULE_DISTRO: ${{ matrix.distro }} + working-directory: molecule diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 85dee5ff..00000000 --- a/.travis.yml +++ /dev/null @@ -1,109 +0,0 @@ ---- -services: docker - -branches: - only: - - master - -env: - - shell_script: collection.sh - - # Run each test playbook in a separate environment. - - playbook: deployments.yml - distro: ubuntu2004 - test_idempotence: false - - # TODO: Not easy to test in CI at this time. - # - playbook: deployments-balancer.yml - # distro: ubuntu2004 - - - playbook: deployments-rolling.yml - distro: ubuntu2004 - - - playbook: docker.yml - distro: ubuntu2004 - test_idempotence: false - - - playbook: docker-flask.yml - distro: ubuntu2004 - test_idempotence: false - - - playbook: docker-hubot.yml - distro: ubuntu2004 - test_idempotence: false - - - playbook: drupal.yml - distro: ubuntu2004 - - # TODO: Not easy to test in CI at this time. - # - playbook: dynamic-inventory.yml - # distro: ubuntu2004 - - - playbook: elk.yml - distro: ubuntu2004 - - - playbook: first-ansible-playbook.yml - distro: centos7 - - - playbook: galaxy-role-servers.yml - distro: ubuntu2004 - - # TODO: Not easy to test in CI at this time. - # - playbook: gluster.yml - # distro: ubuntu2004 - - - playbook: https-self-signed.yml - distro: ubuntu2004 - - - playbook: https-nginx-proxy.yml - distro: debian10 - - - playbook: includes.yml - distro: ubuntu2004 - - - playbook: jenkins.yml - distro: ubuntu2004 - - # TODO: Not easy to test in CI at this time. - # - playbook: kubernetes.yml - # distro: debian9 - - # TODO: Not easy to test in CI at this time. - # - playbook: lamp-infrastructure.yml - # distro: ubuntu2004 - - - shell_script: molecule.sh - - - playbook: nodejs.yml - distro: centos7 - - - playbook: nodejs-role.yml - distro: centos7 - - - shell_script: orchestration.sh - - - playbook: security.yml - distro: centos8 - - - playbook: solr.yml - distro: ubuntu2004 - - - playbook: test-plugin.yml - distro: centos7 - -script: - # Use test shim and run playbook if playbook is provided. - - | - if [ ! -z "$playbook" ]; then - # Download test shim. - wget -O ${PWD}/tests/test.sh https://gist.githubusercontent.com/geerlingguy/73ef1e5ee45d8694570f334be385e181/raw/ - chmod +x ${PWD}/tests/test.sh - # Run tests. - ${PWD}/tests/test.sh - fi - - # Run script directly if script is provided. - - | - if [ ! -z "$shell_script" ]; then - ${PWD}/tests/${shell_script} - fi diff --git a/README.md b/README.md index 5a874a8f..fc0f33a2 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Ansible for DevOps Examples -[![Build Status](https://travis-ci.org/geerlingguy/ansible-for-devops.svg?branch=master)](https://travis-ci.org/geerlingguy/ansible-for-devops) [![GitHub CI](https://github.com/geerlingguy/ansible-for-devops/workflows/CI/badge.svg?branch=master&event=push)](https://github.com/geerlingguy/ansible-for-devops/actions) +[![CI](https://github.com/geerlingguy/ansible-for-devops/workflows/CI/badge.svg?event=push)](https://github.com/geerlingguy/ansible-for-devops/actions?query=workflow%3ACI) [![Molecule CI](https://github.com/geerlingguy/ansible-for-devops/workflows/Molecule%20CI/badge.svg?event=push)](https://github.com/geerlingguy/ansible-for-devops/actions?query=workflow%3A%22Molecule+CI%22) This repository contains Ansible examples developed to support different sections of [Ansible for DevOps](https://www.ansiblefordevops.com/), a book on [Ansible](http://www.ansible.com/) by [Jeff Geerling](https://www.jeffgeerling.com/). @@ -72,7 +72,7 @@ Here is an outline of all the examples contained in this repository, by chapter: ### Chapter 13 - [`molecule`](molecule/): A Molecule example used for testing and developing an Ansible playbook, or for testing in a Continuous Integration (CI) environment. - - [`ci.yml` GitHub Actions workflow](.github/workflows/ci.yml): A GitHub Actions workflow which runs the `molecule` example in a CI environment. + - [`molecule-ci.yml` GitHub Actions workflow](.github/workflows/molecule-ci.yml): A GitHub Actions workflow which runs the `molecule` example in a CI environment. ### Chapter 14 @@ -94,6 +94,12 @@ Here is an outline of all the examples contained in this repository, by chapter: MIT +## Sponsors + +* [TinyPilot](https://tinypilotkvm.com): An open-source, low-cost KVM over IP for managing your servers. + +The above sponsor is [sponsoring Jeff Geerling on GitHub Sponsors](https://github.com/sponsors/geerlingguy). You can sponsor Jeff's work too, to help him continue improving this book and Ansible open source work! + ## Buy the Book [![Ansible for DevOps Cover](https://s3.amazonaws.com/titlepages.leanpub.com/ansible-for-devops/medium)](https://www.ansiblefordevops.com/) diff --git a/elk/requirements.yml b/elk/requirements.yml index 86772909..99ccad88 100644 --- a/elk/requirements.yml +++ b/elk/requirements.yml @@ -4,7 +4,7 @@ roles: - name: geerlingguy.nginx - name: geerlingguy.pip - name: geerlingguy.elasticsearch - version: 4.1.1 + version: 5.0.0 - name: geerlingguy.elasticsearch-curator version: 2.1.0 - name: geerlingguy.kibana diff --git a/first-ansible-playbook/README_CN.md b/first-ansible-playbook/README_CN.md new file mode 100644 index 00000000..0d180cb7 --- /dev/null +++ b/first-ansible-playbook/README_CN.md @@ -0,0 +1,58 @@ +# 第一个 Ansible Playbook 演示虚拟机 + +这个项目创建一个虚拟机,并演示了一个非常简单的 Ansible Playbook。 + +## 快速入门指南 + +### 1 - 安装依赖关系(VirtualBox, Vagrant, Ansible)。 + + 1. 下载并安装【VirtualBox】(https://www.virtualbox.org/wiki/Downloads)。 + 2. 下载并安装 [Vagrant](http://www.vagrantup.com/downloads.html)。 + 3. [仅限 Mac/Linux]安装[Ansible](http://docs.ansible.com/intro_installation.html)。 + +Windows请用户注意。*本指南假设您使用的是Mac或Linux主机。目前不支持Windows主机。如报错请自行调试。* + +### 2 - 构建虚拟机 + + 1. 下载这个项目,并把它放在你想放的地方。 + 2. 打开终端,cd 到这个目录。 + 4. 4.输入`vagrant up`,让Vagrant发挥它的魔力。 + +注意: *如果在运行`vagrant up`的过程中出现任何错误,并且它让你回到命令提示符,只需运行`vagrant provision`继续从你离开的地方构建虚拟机。如果这样做了几次之后仍然有错误,请在GitHub上的项目问题队列中发布一个问题,并注明错误。* + +### 3 - 代码说明 + +```yml +--- +- hosts: all # 执行所有目标 host + become: yes # 提升管理员权限 + + tasks: + - name: Ensure chrony (for time synchronization) is installed. + yum: # 软件包管理 + name: chrony + state: present #存在 + + - name: Ensure chrony is running. + service: # 系统服务管理 + name: chronyd + state: started #启动服务 + enabled: yes # 开机启动 + +# The same as the above play, but in super-compact form! +# 这是超级简洁的无废话代码,功能和上面的代码一样 +- hosts: all + become: yes + tasks: + - yum: name=chrony state=present + - service: name=chronyd state=started enabled=yes +``` + + +## 注意 + + - 要关闭虚拟机,在终端中输入 "vagrant halt",在同一个文件夹中输入 "Vagrantfile"。要完全销毁它(如果你想节省一点磁盘空间,或者想用 "vagrant up "从头开始重建),输入 "vagrant destroy"。 + +## 关于作者 + +这个项目是由[Jeff Geerling](https://www.jeffgeerling.com/)创建的,作为[Ansible for DevOps](https://www.ansiblefordevops.com/)的一个例子。 diff --git a/molecule/main.yml b/molecule/main.yml index dddde73e..cdf75a80 100644 --- a/molecule/main.yml +++ b/molecule/main.yml @@ -34,6 +34,7 @@ Hello world! dest: "/var/www/html/index.html" + mode: 0664 notify: restart apache - name: Ensure Apache is running and starts at boot. diff --git a/orchestration/README_CN.md b/orchestration/README_CN.md new file mode 100644 index 00000000..3f9c7c7c --- /dev/null +++ b/orchestration/README_CN.md @@ -0,0 +1,225 @@ +# 简单的 Ansible 基础架构编排实例 + +Ansible Planybook 灵活而强大。但有时,你只需要在一组主机上运行一条命令。这时,简单的`ansible`命令就派上用场了。 + +就像《Ender's Game》中的设备一样,`ansible`命令允许你在一台或一百台服务器上立即运行命令或调用Ansible模块。 + +这个项目使用Vagrant和VirtualBox配置了三个虚拟机。`app1`、`app2`和`db`,以模拟一个小规模的真实世界基础设施(两个应用服务器和一个数据库服务器),因此您可以练习在它们之间运行`ansible`命令,并在一个灵活的Ansible清单上工作。 + +## 快速入门指南 + +### 1 - 安装依赖关系(VirtualBox, Vagrant, Ansible)。 + + 1. 下载并安装【VirtualBox】(https://www.virtualbox.org/wiki/Downloads)。 + 2. 下载并安装 [Vagrant](http://www.vagrantup.com/downloads.html)。 + 3. 仅限Mac/Linux]安装[Ansible](http://docs.ansible.com/intro_installation.html)。 + +Windows用户注意。*本指南假设您使用的是Mac或Linux主机。目前不支持Windows主机。 + +### 2 - 构建虚拟机 + + 1. 下载这个项目,并把它放在你想放的地方。 + 2. 打开终端,cd到这个目录(包含`Vagrantfile`和这个README文件)。 + 3. 输入`vagrant up`,让Vagrant发挥它的魔力。 + +注意: *如果在运行`vagrant up`的过程中出现任何错误,并且它将您丢回命令提示符,只需运行`vagrant provision`命令,它将继续从您报错的地方构建虚拟机。如果做了几次后仍然有错误,请在GitHub上的项目问题队列中发布一个问题并注明错误。 + + +#### ansible.cfg + +定义当前目录下执行 ansible 命令的配置参数, cfg 文件中调用了 当前目录下的 hosts.ini 清单文件。 + +``` +[defaults] +inventory = hosts.ini +nocows = True +``` + +#### hosts.ini + +被管理服务器定义清单文件 inventory 。 + + + +``` +# Application servers +# 应用服务器组 +[app] +192.168.60.4 +192.168.60.5 + +# Database server +# 数据库服务器组 +[db] +192.168.60.6 + +# Group 'multi' with all servers +# 名为 multi 的嵌套组 +[multi:children] +app +db + +# Variables that will be applied to all servers +# 给嵌套组定义变量,应用于所有服务器 +[multi:vars] +ansible_user=vagrant +ansible_ssh_private_key_file=~/.vagrant.d/insecure_private_key +ansible_ssh_common_args='-o StrictHostKeyChecking=no' +``` + + + + + +### 3 - 创建一个清单文件,并运行`ansible`命令。 + +详情请阅读【Ansible for DevOps】(https://www.ansiblefordevops.com/)第三章。 + + +### 运行 ad-hoc 命令 + +运行多次下面的命令,了解多线程并发的特性 + +ansible multi -a "hostname" +ansible multi -a "hostname" +ansible multi -a "hostname" + +ansible multi -a "hostname" -f 1 +ansible multi -a "hostname" -f 1 +ansible multi -a "hostname" -f 1 + +环境状况检查 + +ansible multi -a "df -h" + +ansible multi -a "free -m" + +ansible multi -a "date" + +使用 Ansible 模块做变更 + +ansible multi -b -m yum -a "name=chrony state=present" + +ansible multi -b -m service -a "name=chronyd state=started enabled=yes" + +ansible multi -b -a "chronyc tracking" + +ansible multi -a "date" + +配置服务器组和单一服务器 + +配置应用服务器:安装 python3 和 django + +ansible app -b -m yum -a "name=python3-pip state=present" + +ansible app -b -m pip -a "name=django<4 state=present" + + +ansible app -a "python -m django --version" + +配置数据库服务器 + +ansible db -b -m yum -a "name=mariadb-server state=present" + +ansible db -b -m service -a "name=mariadb state=started enabled=yes" + +ansible db -b -m yum -a "name=firewalld state=present" + +ansible db -b -m service -a "name=firewalld state=started enabled=yes" + +ansible db -b -m firewalld -a "zone=database state=present permanent=yes" + +ansible db -b -m firewalld -a "source=192.168.60.0/24 zone=database state=enabled permanent=yes" + +ansible db -b -m firewalld -a "port=3306/tcp zone=database state=enabled permanent=yes" + +ansible db -b -m yum -a "name=python3-PyMySQL state=present" + +ansible db -b -m mysql_user -a "name=django host=% password=12345 priv=*.*:ALL state=present" + +对服务器组中的某一个执行命令 + +ansible app -b -a "service chronyd restart" --limit "192.168.60.4" + +用星号匹配 + +ansible app -b -a "service ntpd restart" --limit "*.4" + +用正则表达式匹配 + +ansible app -b -a "service ntpd restart" --limit ~".*\.4" + + +管理用户和组 + +ansible app -b -m group -a "name=admin state=present" + +ansible app -b -m user -a "name=johndoe group=admin createhome=yes" + +ansible app -b -m user -a "name=johndoe state=absent remove=yes" + +管理软件包 + +ansible app -b -m package -a "name=git state=present" + +管理文件和目录 + +查看文件属性 + +ansible multi -m stat -a "path=/etc/environment" + +从本地复制文件到服务器 + +ansible multi -m copy -a "src=/etc/hosts dest=/tmp/hosts" + +从服务器上下载文件 + +ansible multi -b -m fetch -a "src=/etc/hosts dest=/tmp" + +创建目录和文件 + +ansible multi -m file -a "dest=/tmp/test mode=644 state=directory" + +ansible multi -m file -a "src=/src/file dest=/dest/symlink state=link" + +删除目录和文件 + +ansible multi -m file -a "dest=/tmp/test state=absent" + +用异步作业异步的更新服务器 + +ansible multi -b -B 3600 -P 0 -a "yum -y update" + +ansible multi -b -m async_status -a "jid=169825235950.3572" + +查看日志的方法 + +ansible multi -b -a "tail /var/log/messages" + +ansible multi -b -m shell -a "tail /var/log/messages | \ +grep ansible-command | wc -l" + +管理 cron 作业 + +ansible multi -b -m cron -a "name='daily-cron-all-servers' \ +hour=4 job='/path/to/daily-script.sh'" + +ansible multi -b -m cron -a "name='daily-cron-all-servers' \ +state=absent" + +部署版本控制的应用 + +ansible app -b -m git -a "repo=git://example.com/path/to/repo.git \ +dest=/opt/myapp update=yes version=1.2.4" + +ansible app -b -a "/opt/myapp/update.sh" + +Ansible 的 SSH 连接历史 + + + + + +## 关于作者 + +这个项目是由[Jeff Geerling](https://www.jeffgeerling.com/)创建的,作为[Ansible for DevOps](https://www.ansiblefordevops.com/)的一个例子。 \ No newline at end of file diff --git a/orchestration/ansible.cfg b/orchestration/ansible.cfg index 7ff255cd..22527ef0 100644 --- a/orchestration/ansible.cfg +++ b/orchestration/ansible.cfg @@ -1,3 +1,6 @@ [defaults] inventory = hosts.ini nocows = True +deprecation_warnings=False +command_warnings=False +pipelining=True \ No newline at end of file diff --git a/tests/collection.sh b/tests/collection.sh index 77cfa7b9..b65cd37a 100755 --- a/tests/collection.sh +++ b/tests/collection.sh @@ -3,13 +3,8 @@ # Collection playbook tests. set -e -# Make sure pip3 is available. -sudo apt-get update -sudo apt-get install -y python3-pip python3-setuptools -pip3 install --upgrade setuptools pip - # Install dependencies. -sudo pip3 install ansible +pip3 install ansible cd collection diff --git a/tests/deployments.yml b/tests/deployments.yml index e2f342b8..f8a67636 100644 --- a/tests/deployments.yml +++ b/tests/deployments.yml @@ -1,4 +1,10 @@ --- +- hosts: all + + pre_tasks: + - name: Update the apt cache. + apt: update_cache=yes cache_valid_time=600 + # Deployments test. - import_playbook: ../deployments/playbooks/provision.yml - import_playbook: ../deployments/playbooks/deploy.yml diff --git a/tests/elk.yml b/tests/elk.yml index 1806156d..387a9870 100644 --- a/tests/elk.yml +++ b/tests/elk.yml @@ -1,6 +1,10 @@ --- - hosts: all + pre_tasks: + - name: Update the apt cache. + apt: update_cache=yes cache_valid_time=600 + tasks: - add_host: name: localhost diff --git a/tests/molecule.sh b/tests/molecule.sh index 7c7e1500..57d0c697 100755 --- a/tests/molecule.sh +++ b/tests/molecule.sh @@ -3,13 +3,8 @@ # Molecule playbook tests. set -e -# Make sure pip3 is available. -sudo apt-get update -sudo apt-get install -y python3-pip python3-setuptools -pip3 install --upgrade setuptools pip - # Install dependencies. -sudo pip3 install ansible molecule ansible-lint yamllint docker +pip3 install ansible molecule[docker] ansible-lint yamllint docker cd molecule diff --git a/tests/orchestration.sh b/tests/orchestration.sh index e602bac2..b221b94a 100755 --- a/tests/orchestration.sh +++ b/tests/orchestration.sh @@ -3,13 +3,8 @@ # Orchestration tests. set -e -# Make sure pip3 is available. -sudo apt-get update -sudo apt-get install -y python3-pip python3-setuptools -pip3 install --upgrade setuptools pip - # Install dependencies. -sudo pip3 install ansible +pip3 install ansible cd orchestration/scripts diff --git a/tests/requirements.yml b/tests/requirements.yml index 54546552..77305aab 100644 --- a/tests/requirements.yml +++ b/tests/requirements.yml @@ -18,7 +18,7 @@ roles: - name: geerlingguy.solr - name: geerlingguy.elasticsearch - version: 4.1.1 + version: 5.0.0 - name: geerlingguy.elasticsearch-curator version: 2.1.0 - name: geerlingguy.kibana