1、相关的配置文件路径
/etc/ansible/ansible.cfg:主配置文件,配置ansible工作特性
/etc/ansible/hosts:主机清单
/etc/ansible/roles:存放角色的目录
2、相关执行程序
/usr/bin/ansible:主目录,临时命令执行工具
/usr/bin/ansible-doc:查看配置文档,模块功能查看工具
/usr/bin/ansible-galaxy:下载/上传代码或roles模块的官网平台
/usr/bin/ansible-playbook:定制自动化任务,编排剧本工具
/usr/bin/ansible-pull:执行远程命令工具
/usr/bin/ansible-console:基于console界面与用户交互的执行工具
3、主机清单inventory
- inventory主机清单,ansible的主要功能在于批量主机操作,为了方便快捷
- 默认的inventor file为/etc/ansible/hosts
- inventor file可以有多个
##/etc/ansible/hosts文件格式##
##中括号的字符为组名,可以将多个主机同时划分到多个不同的组##
##若主机使用了其他ssh端口,可在主机名称后用冒号加端口号##
ntp.nagedu.com
[webservers]
www1.magedu.com:2222
www2.magedu.com
[dbservers]
db1.magedu.com
db2.magedu.com
db3.magedu.com
如果主机名称遵循相似的命名模式,可以使用列表的方式标识各主机
[webservers]
www[a:f].com:2222
[dbservers]
db[01:100].magedu.com
4、Ansible配置文件
配置文件路径:/etc/ansible/ansible.cfg
#inventory=/etc/ansible/hosts #主机列表配置文件
#library=/usr/share/my_modules/ #库文件存放目录
#remote_tmp=$HOME/.ansible/tmp #本机的临时命令执行目录
#forks=5 #默认并发数
#sudo_user=root #默认sudo用户
#ask_sudo_pass=True #每次执行ansible命令是否询问ssh密码
#ask_pass=True
#remote_port=22
#host_key_checking=False #检查对应服务器的host_key,建议取消注释
#log_path=/var/log/ansible.log #日志文件
5、Ansible常用模块
注:使用ansible前先做ssh免密,以方便操作
Ansible有很多模块,包括云计算,命令行、包管理系统服务,用户管理等,可以通过官方网站查看相应的模块
http://docs.ansible.com/modules_by_category.html,也可以通过ansible-doc -l命令查看模块,或者通过ansible-doc -s模块名查看具体某个模块的使用方法
调用模块的语法如下
ansible 操作目标(主机IP/组名) -m 模块名 -a 模块参数
- Setup模块
该模块课用于获取Ansible客户端机器的详细信息,命令如下
ansible XXX(IP/组名) -m setup
- Copy模块
该模块用于anxible主机向客户机发送文件,类似于scp
ansible all -m copy -a "src=/root/test.sh dest=/tmp/ owner=root group=root mode=755 force=yes"
- Synchronize模块
此模块会调用rsync命令,所以要提前安装rsync包。此模块将用于将Ansible机器的制定目录推送(push)到客户机的制定目录下
#先安装rsync
ansible all -a 'yum install -y rsync'
#此模块命令
ansible 192.168.43.41 -m synchronize -a "src=/root/example/fabric/ dest=/root/ delete=yes compress=yes"
#delete=yes 用来实现使两边的内容一样(以push的方式为主),compress=yes用于开启压缩,默认为开启
- File模块
File模块主要用来设置文件或目录的属性
#例一:将客户端机器的/root/test.py软链接到/tmp/test.py
ansible 192.168.40.41 -m file -a "src=/root/test.py dest=/tmp.py state=link"
#查看软连接情况
ansible 192.168.40.41 -m command -a 'ls -l /tmp/test.py'
#例二:将刚刚建立的软链接文件删除
ansible 192.168.40.41 -m file -a "path=/tmp.py state=absent5"
- group模块
group模块可以在所有节点上创建自己定义的组,比如利用此模块创建一个组为test,gid为2016的组,如下
ansible office -m group -a 'name=test gid=2016'
通过如下命令查看前面只想而创建的组test:
ansible office -m shell -a 'cat /etc/group | grep test'
#shell模块支持管道符命令,command是不支持管道符
- User模块
该模块用于创建用户,在指定的节点上创建一个用户名为test、组为test的用户,命令如下
ansible office -m user -a 'name=test group=test'
- ping模块
就是用于Ping通测试
ansible all -m ping
- Script模块
用于在远程被控制端主机上执行本地ansible机器中的shell脚本文件,相当于scp+shell的组合命令
例如执行本地的脚本print_hello.sh
ansible office -m script -a "/root/print_hello.sh"
- get_url模块
用于在远程主机上下载url到本地,例被控端需要下载http://mirrors.aliyun.com/repo/Centos-7.repo
ansible office -m get_url -a 'url=http://mirrors.aliyun.com/repo/Centos-7.repo dest=/etc/yum.repos.d'
- yum模块
用于管理linux平台的软件包操作
config_file:yum的配置文件
disable_gpg_check:关闭gpg_check
disablerepo:不启用某个源
enablerepo:启用某个源
name:要操作的软件包的名字,也可以传递一个url或一个本地的rpm包的路径
state:present|absent|latest,present|absent表示安装,latest表示移除
如在远程端机器上用nginx的yum源安装nginx软件包,命令如下
ansible 192.168.43.41 -m yum -a 'name=nginx enablerepo=nginx state=present'
- cron模块
该模块就是创建计划任务,可以定义,被控端机器每天凌晨1点过1分ntpdate自动对时,命令如下
ansible office -m cron '"name=ntpdate time every day" minute="1" hour="1" job="/sbin/ntpdate ntp.api.bz">> /dev/null'
- service模块
远程端服务管理,例如开启、关闭、重启服务等
#例一:在webserver端开启nginx服务,命令如下:
ansible webserver -m service -a "name=nginx state=started"
#例二:将httpd加入webserver端的启动项,命令如下:
ansible mysql -m service -a 'name=mysqld state=started enabled=yes'