- 服务端要求
Python 2.6+
RedHat Debian CentOS OSX 等
不支持Windows
- 被管理端要求
OpenSSH
Python 2.6+
- #安装
yum install -y ansible -y
#pip install -y ansible
#https://github.com/ansible/ansible.git
vim /etc/ansible/hosts
#配置管理IP段
[webservers]
192.168.10.251 ansible_ssh_user=root ansible_ssh_pass=123.com
192.168.10.252 ansible_ssh_user=root ansible_ssh_pass=123.com
192.168.10.253 ansible_ssh_user=root ansible_ssh_pass=123.com
#测试联通
[root@k8s-master01 tmp]# ansible webservers -a "df -h"
192.168.10.251 | SUCCESS | rc=0 >>
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 37G 20G 18G 53% /
devtmpfs 3.9G 0 3.9G 0% /dev
tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs 3.9G 393M 3.5G 11% /run
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/sda1 1014M 142M 873M 14% /boot
tmpfs 3.9G 12K 3.9G 1% /var/lib/kubelet/pods/aee9f414-54e8-47f7-ab4c-ad7a8a6884b1/volumes/kubernetes.io~secret/flannel-token-k7wbl
tmpfs 783M 0 783M 0% /run/user/0
192.168.10.253 | SUCCESS | rc=0 >>
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 37G 19G 19G 49% /
devtmpfs 3.9G 0 3.9G 0% /dev
tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs 3.9G 131M 3.7G 4% /run
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/sda1 1014M 142M 873M 14% /boot
tmpfs 783M 0 783M 0% /run/user/0
192.168.10.252 | SUCCESS | rc=0 >>
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 37G 32G 5.7G 85% /
devtmpfs 3.9G 0 3.9G 0% /dev
tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs 3.9G 393M 3.5G 11% /run
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/sda1 1014M 142M 873M 14% /boot
tmpfs 783M 0 783M 0% /run/user/0
tmpfs 3.9G 12K 3.9G 1% /var/lib/kubelet/pods/214bcf7f-5703-4b1d-8049-7e8c5ba13382/volumes/kubernetes.io~secret/flannel-token-k7wbl
#查看内存
ansible webservers -a "free -m"
- #基本配置
vim /etc/ansible/ansible.cfg
[defaults]
inventory = /etc/ansible/hosts
forks = 5
sudo_user = root
remote_port = 22
host_key_checking = False
timeout = 10
log_path = /var/log/ansible.log
private_key_file = /root/.ssh/id_rsa
- Inventory 主机清单
/etc/ansible/hosts 中默认给提供了三种模板
Ex1:未分组的主机,默认在all组中。
ansible all -a "free -m"
Ex2:属于webservers的主机组。
ansible webservers -a "free -m"
主机名称有规律可以使用范围配置
Ex3:属于dbservers的主机组。
#IP 连接IP ssh用户 密码
[webservers]
192.168.10.251 ansible_ssh_user=root ansible_ssh_pass=123.com
192.168.10.252 ansible_ssh_user=root ansible_ssh_pass=123.com
192.168.10.253 ansible_ssh_user=root ansible_ssh_pass=123.com
#定义变量
#https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html
vim /etc/ansible/group_vars/webservers.yml
http_port: 8080
server_name: www.baidu.com
ansible webservers -a "echo {{http_port}}"