官方首页Ansible
官方文档Ansible Docs
中文文档Chinese Docs
安装ansible
yum -y install epel-release
yum clean all
yum makecache
yum -y install ansible
参数总结
--version # ansible版本信息
-v # 显示详细信息,用于排错
-vvv # 显示更多详细的
-vvvv # 调用debug模式
-i # 主机清单默认在/etc/ansible/hosts
-m # 使用模块的名称,默认使用command模块
-a # 使用模块的参数,模块的具体动作
-k # 提示输入ssh密码,而不使用基于ssh的密钥认证
-C # 模拟执行测试,但不会真的执行
-T # 执行命令的超时
读取配置文件顺序
$ANSIBLE_CONFIG
当前目录下ansible.cfg
家目录下ansible.cfg
/etc/ansible/ansible.cfg
配置文件详解
#inventory = /etc/ansible/hosts # 主机列表配置文件
#library = /usr/share/my_modules/ # 库文件存放目录
#remote_tmp = ~/.ansible/tmp # 临时py文件存放在远程主机目录
#local_tmp = ~/.ansible/tmp # 本机的临时执行目录
#forks = 5 # 默认并发数
#poll_interval = 15 #
#sudo_user = root # 默认sudo用户
#ask_sudo_pass = True # 每次执行是否询问sudo的ssh密码
#ask_pass = True # 每次执行是否询问ssh密码
#transport = smart #
#remote_port = 22 # 远程主机端口
host_key_checking = False # 跳过检查主机指数
log_path = /var/log/ansible.log # ansible日志
[privilege_escalation] # 普通用户需要配置提权
#become=True
#become_merhod=sudo
#become_user=root
#become_ask_pass=Flase
主机清单写法
### 1 主机+端口+用户+用户密码
[webservers] # webservers可变
192.168.1.2 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='123456'
192.168.1.3 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='123456'
# ansible_host ansible_port ansible_user ansible_password
### 2 域名
[webservers]
web[1:2].test.com ansible_ssh_pass='123456'
### 3 群组
[webservers]
web[1:2].test.com # [1:2] 范围,[01:50:2]每次增加2,[a:f]字母
[webservers:vars] # vars公用变量
ansible_ssh_pass='123456'
### 合并多个主机组
[servers:children] # 可以包括两个子组(servers可变)
webservers1
webservers2
ansible all -i hosts --list-hosts # 获取所有地址
ansible webservers(组名) -i hosts --list-hosts # 获取单个组下的地址
ansible webservers -i hosts -a 'hostname' # 显示名字
ansible “主机名称” -m指定模块 “模块名称” -a模块动作 “具体命令”
ansible-doc -l # 查询所有模块名称
ansible-doc -s ping # 查询单个模块的使用及介绍
常用模块
command # 执行shell命令(不支持管道等特殊字符)
shell # 执行shell命令
scipt # 执行shell脚本
yum_repository # 配置yum仓库配置yum源
yum # 安装软件
copy # 变更配置文件复制
file # 建立目录或文件
service # 启动与停止服务
mount # 挂载设备
cron # 定时任务
firewalld # 防火墙
iptables # 防火墙
get_url # 下载软件wget