1、Ansible安装
# 安装 dnf -y install ansible-core # 查看版本 ansible --version
2、修改配置文件
# 备份并从新生成配置文件 mv /etc/ansible/ansible.cfg /etc/ansible/ansible.cfg.org ansible-config init --disabled > /etc/ansible/ansible.cfg # 取消主机密钥检查 # vi /etc/ansible/ansible.cfg ... host_key_checking=False #318行 ...
3、配置管理目标主机
# 配置管理目标主机 # vim /etc/ansible/hosts 10.32.161.134 10.32.161.135 [bigdata] 10.32.161.131 10.32.161.132 10.32.161.133 # 查看所有主机情况 ansible all --list-hosts # 查看管理主机组情况 ansible bigdata --list-hosts
4、基础使用
# 已配置免密登录的主机 # ansible 10.32.161.134 -m ping 10.32.161.134 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python3" }, "changed": false, "ping": "pong" } # 未配置免密登录的主机或主机群组 # ansible bigdata -k -m command -a "uptime" -b --ask-become-pass SSH password: #输入密码 BECOME password[defaults to SSH password]: #输入密码 10.32.161.132 | CHANGED | rc=0 >> 13:25:32 up 12 days, 21:28, 1 user, load average: 0.00, 0.00, 0.00 10.32.161.131 | CHANGED | rc=0 >> 13:25:32 up 12 days, 21:28, 2 users, load average: 0.04, 0.01, 0.00 10.32.161.133 | CHANGED | rc=0 >> 13:25:32 up 12 days, 21:28, 1 user, load average: 0.08, 0.02, 0.01