优点:
缺点:
yum -y install epel-release yum -y install ansible
/etc/ansible/hosts — 主机清单 /etc/ansible/ansible.cfg — ansible服务配置文件 /etc/ansible/roles — 角色目录
编辑/etc/ansible/host
# 方法一 主机+端口+密码
[webserver]
10.1.1.11 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456" 10.1.1.12 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456" 10.1.1.13 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456"
# 方法二 主机+端口+密码
[webserver]
10.1.1.1[1:3] ansible_ssh_user=root ansible_ssh_pass="123456"
# 方法二 主机+端口+密码
[webserver]
10.1.1.1[1:3]
[webserver:vars]
ansible_ssh_pass="123456"
# 方法一 主机+端口+密钥
[webserver]
10.1.1.11:22
10.1.1.12
10.1.1.13
10.1.1.16
# 方法一 别名主机+端口+密钥
[webserver]
node1 ansible_ssh_host=10.1.1.11 ansible_ssh_port=22
node2 ansible_ssh_host=10.1.1.12 ansible_ssh_port=22
node3 ansible_ssh_host=10.1.1.13 ansible_ssh_port=22
node6 ansible_ssh_host=10.1.1.16 ansible_ssh_port=22
# 主机组变量名+主机+密码
[apache]
10.1.1.16
10.1.1.13
[apache.vars]
ansible_ssh_pass='123456'
# 主机组变量名+主机+密钥
[nginx]
10.1.1.1[1:2]
# 定义多个组,把一个组当另外一个组的组员
[webserver:children] #webserver组包括两个子组:apache nginx
apache
nginx
The authenticity of host ‘10.1.1.13 (0.1.1.13)’ can’t be established.
RSA key fingerprint is b7:2f:12:ff:a8:97:a3:09:13:41:c0:e8:ab:7a:6f:ed.
Are you sure you want to continue connecting (yes/no)?
RSA host key for 10.1.1.13 has changed and you have requested strict checking
ansible连接新的host时会报错
修改下ssh的配置就可以了,当然,必须确定远程主机是可信任的
vim /etc/ansible/ansible.cfg
# uncomment this to disable SSH key host checking
host_key_checking = False
完整的连接行为控制变量参见官方手册:How to build your inventory — Ansible Documentation。下面解释几个常见的行为变量。
Inventory变量名 | 含义 | 例子 |
---|---|---|
ansible_host | ansible连接节点时的IP地址 | ansible_host=10.1.1.60 |
ansible_port | 连接对方的端口号,ssh连接时默认为22 | ansible_host=22 |
ansible_user | 连接对方主机时使用的主机名。不指定时,将使用执行ansible或ansible-playbook命令的用户 | ansible_user=boysec |
ansible_password | 连接时的用户密码 | ansible_password=666666 |
ansible_connection | 连接类型,有效值包括smart、ssh、paramiko、local、docker、winrm,默认为smart。smart表示智能选择ssh和paramiko,当SSH支持ControlPersist(即持久连接)时使用ssh,否则使用paramiko。local和docker是非基于ssh连接的方式,winrm是连接windows的插件 | ansible_connection=ssh |
ansible_ssh_private_key_file | 指定密钥认证ssh连接时的私钥文件 | ansible_ssh_private_key_file=/home/boysec/.ssh/key |
ansible_become | 允许进行权限提升 | ansible_become=true |
ansible_become_method | 指定提升权限的方式,例如可使用sudo/su/runas等方式 | ansible_become_method=sudo |
ansible_become_user | 提升为哪个用户的权限,默认提升为root | ansible_become_user=boysec |
ansible_become_password | 提升为指定用户权限时的密码 | ansible_become_password=123456 |
ansible_become_exe | 提升用户程序的路径 | ansible_become_exe=/usr/bin/sudo |
[root@ansible ~]# ansible -h
Usage: ansible <host-pattern> [options]
-a MODULE_ARGS #模块参数
-C, --check #检查语法
-f FORKS #并发
--list-hosts #列出主机列表
-m MODULE_NAME #模块名字
-o 使用精简的输出
-i 使用指定的Inventory文件
演示
ansible webservers -m shell -a 'uptime' -o
10.1.1.11 | CHANGED | rc=0 | (stdout) 09:20:46 up 2 days, 20:53, 2 users, load average: 0.02, 0.02, 0.05
10.1.1.12 | CHANGED | rc=0 | (stdout) 09:20:46 up 18:58, 2 users, load average: 0.00, 0.01, 0.05
命令说明
总结: ansible就是用什么模块,让谁干什么事情。