指南
场景
快速体验
如果您安装了 Ansible 镜像,参考如下步骤快速体验:
-
使用 SSH 连接到服务器,运行
ansible -h
命令,查看帮助信息usage: ansible [-h] [--version] [-v] [-b] [--become-method BECOME_METHOD]
[--become-user BECOME_USER] [-K] [-i INVENTORY] [--list-hosts]
[-l SUBSET] [-P POLL_INTERVAL] [-B SECONDS] [-o] [-t TREE] [-k]
[--private-key PRIVATE_KEY_FILE] [-u REMOTE_USER]
[-c CONNECTION] [-T TIMEOUT]
[--ssh-common-args SSH_COMMON_ARGS]
[--sftp-extra-args SFTP_EXTRA_ARGS]
[--scp-extra-args SCP_EXTRA_ARGS]
[--ssh-extra-args SSH_EXTRA_ARGS] [-C] [--syntax-check] [-D]
[-e EXTRA_VARS] [--vault-id VAULT_IDS]
[--ask-vault-pass | --vault-password-file VAULT_PASSWORD_FILES]
[-f FORKS] [-M MODULE_PATH] [--playbook-dir BASEDIR]
[-a MODULE_ARGS] [-m MODULE_NAME]
pattern
Define and run a single task 'playbook' against a set of hosts
positional arguments:
pattern host pattern -
运行
ansible localhost -m setup
命令,查看本机获取的系统信息localhost | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"172.23.0.1",
"172.27.0.1",
"172.22.0.1",
"172.18.0.1",
"172.28.0.1",
"172.17.0.1",
"172.19.0.11"
],
"ansible_all_ipv6_addresses": [
"fe80::42:9fff:fe11:a0f3",
"fe80::42:b5ff:feb0:13b3",
"fe80::42:1fff:fe68:d58b",
"fe80::8874:21ff:fe2a:cada",
"fe80::42:2cff:fefd:f576",
"fe80::688a:7ff:fea2:9a76",
"fe80::42:bdff:fe4e:d3ab",
"fe80::42:edff:fef4:5bd3",
"fe80::5054:ff:fe4a:8b63",
"fe80::14d0:b6ff:fecb:a383",
"fe80::2cae:e1ff:fee1:aa10"
],
"ansible_apparmor": {
"status": "disabled"
},
"ansible_architecture": "x86_64",
"ansible_bios_date": "04/01/2014",
"ansible_bios_version": "seabios-1.9.1-qemu-project.org",
... -
运行服务器上的 playbook 范例
cd /data/apps/ansible
ansible-playbook -i hosts test.yml -c local -
以上命令顺利运行,即表明 Ansible 验证通过
代码分析与测试
Ansible 的测试包括:静态代码分析和程序运行结果测试。
静态代码测试的主要目的是强制执行Ansible编码标准和要求。
Ansible 官网文档有一章专门的 Testing Strategies。
并发设置
安装 Role
Websoft9 提供基于 Ansible role 的基础组件(清单),包括:Apache, Nginx, PHP, MySQL, JDK 等数十种。
下面以安装 LAMP 环境为范例:
- SSH 连接云服务器,运行如下的脚本
# MySQL
git clone https://github.com/Websoft9/role_mysql.git
ansible-playbook role_mysql/tests/test.yml
# PHP
git clone https://github.com/Websoft9/role_php.git
ansible-playbook role_php/tests/test.yml
# Apache
git clone https://github.com/Websoft9/role_apache.git
ansible-playbook role_apache/tests/test.yml - 运行过程中,可以选择所需的版本