一:构建测试环境,如下
安装上一篇博客的方法配置3台服务器
我的地址分别是:
10.0.0.130(app1)
10.0.0.131(app2)
10.0.0.141(db)
配置资源文件
[example]
10.0.0.132
[app]
10.0.0.130
10.0.0.131
[db]
10.0.0.141
[multi:children]
app
db
二:AD-HOC命令使用
查看每个服务器的主机名
$ ansible multi -a "hostname"
使用一个线程执行命令,相当于顺序在每个服务器上运行(默认5个线程执行)
$ ansible multi -a "hostname" -f 1
查看你的环境情况:
查看磁盘使用情况
$ ansible multi -a "df -h"
查看内存使用情况
$ ansible multi -a "free -m"
查看时间是否准确
$ ansible multi -a "date"
如果时间不一致,可以使用ntpdate 同步一下
$ ansoble multi -a "ntpdate cn.pool.ntp.org"
三:配置两台应用服务器
前提是安装好epel源和centos base源(可以使用阿里云的镜像源)
$ ansible app -m yum -a "name=MySQL-python state=present"
$ ansible app -m yum -a "name=python-setuptools state=present"
$ ansible app -m easy_install -a "name=django"
测试django是否安装正确
root@~# ansible app -a "python -c 'import django; print django.get_version()'"
10.0.0.131 | success | rc=0 >>
1.10
10.0.0.130 | success | rc=0 >>
1.10
四:配置数据库服务器
$ ansible db -m yum -a "name=mysql-server state=present"
$ ansible db -m service -a "name=mysqld state=started enabled=yes"
配置数据库用户django,并且赋予权限
$ ansible db -m yum -a "name=MySQL-python state=present"
$ ansible db -m mysql_user -a "name=django host=% password=12345 \
priv=*.*:ALL state=present
五:限制命令只在一个服务器上生效
六:管理系统用户和组
$ ansible app -a "service ntpd restart" --limit "10.0.0.132"
# Limit hosts with a simple pattern (asterisk is a wildcard).
$ ansible app -a "service ntpd restart" --limit "*.4"
#以4结尾的ip地址,将会执行命令
# Limit hosts with a regular expression (prefix with a tilde).
$ ansible app -a "service ntpd restart" --limit ~".*\.4"
#使用正则表达式匹配主机
系统添加admin组
$ ansible app -m group -a "name=admin state=present"
系统添加jwh566用户
$ ansible app -m user -a "name=jwh5566 group=admin createhome=yes"
删除系统用户
七:管理文件和目录
$ ansible app -m user -a "name=jwh5566 state=absent remove=yes"
获取文件的信息,权限,所有者等
$ ansible multi -m stat -a "path=/etc/environment"
复制文件到服务器
$ ansible multi -m copy -a "src=/etc/hosts dest=/tmp/hosts"
从服务器接收文件(接收到控制机)
$ ansible multi -m fetch -a "src=/etc/hosts dest=/tmp"
创建目录
$ ansible multi -m file -a "dest=/tmp/test mode=644 state=directory"
创建符号链接
$ ansible multi -m file -a "src=/src/symlink dest=/dest/symlink \
owner=root group=root state=link"
删除目录和文件
$ ansible multi -m file -a "dest=/tmp/test state=absent"
八:运行后台任务
-B <seconds> 指定运行任务的最大时间
-P <seconds> 指定多久时间去一次服务器查看任务执行的状态
异步更新服务器(根据系统情况,可能需要很长时间)
如果说后台任务还在运行,使用下面的命令查看运行状态
$ ansible multi -B 3600 -a "yum -y update"
background launch...
10.0.0.132 | success >> {
"ansible_job_id": "763350539037",
"results_file": "/root/.ansible_async/763350539037",
"started": 1
九:检查日志文件
$ ansible multi -m async_status -a "jid=763350539037"
如果需要grep,需要使用shell模块
$ ansible multi -a "tail /var/log/messages"
root@~# ansible multi -m shell -a "tail /var/log/messages | \
grep ansible-command | wc -l"
10.0.0.131 | success | rc=0 >>
2
10.0.0.130 | success | rc=0 >>
2
10.0.0.141 | success | rc=0 >>
6
这个命令显示每台服务器分别执行了几次ansible命令
十:管理crontab 任务
$ ansible multi -m cron -a "name='daily-cron-all-servers' \
hour=4 job='/path/to/daily-script.sh'"
可以使用这个配置ntp 任务
删除crontab任务
$ ansible multi -m cron -a "name='daily-cron-all-servers' state=absent"
总结:
第三章,到此为止,你已经能够配置一个常用的基础架构,并且能够熟练使用常用的模块,
转载于:https://blog.51cto.com/jwh5566/1843477