ansible命令使用python
查看每一个服务器的主机名mysql
1
|
$ ansible multi -a
"hostname"
|
使用一个线程执行命令,至关于顺序在每一个服务器上运行(默认5个线程执行)正则表达式
1
|
$ ansible multi -a
"hostname"
-f 1
|
查看你的环境状况:sql
查看磁盘使用状况shell
1
|
$ ansible multi -a
"df -h"
|
查看内存使用状况数据库
1
|
$ ansible multi -a
"free -m"
|
查看时间是否准确express
1
|
$ ansible multi -a
"date"
|
若是时间不一致,能够使用ntpdate 同步一下django
$ ansoble multi -a "ntpdate cn.pool.ntp.org"centos
三:配置两台应用服务器bash
前提是安装好epel源和centos base源(能够使用阿里云的镜像源)
1
2
3
|
$ 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是否安装正确
1
2
3
4
5
6
|
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
|
四:配置数据库服务器
1
2
|
$ ansible db -m yum -a
"name=mysql-server state=present"
$ ansible db -m service -a
"name=mysqld state=started enabled=yes"
|
配置数据库用户django,而且赋予权限
1
2
3
|
$ 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
|
五:限制命令只在一个服务器上生效
1
|
$ ansible app -a
"service ntpd restart"
--limit
"10.0.0.132"
|
1
2
3
|
# Limit hosts with a simple pattern (asterisk is a wildcard).
$ ansible app -a
"service ntpd restart"
--limit
"*.4"
#以4结尾的ip地址,将会执行命令
|
1
2
3
|
# Limit hosts with a regular expression (prefix with a tilde).
$ ansible app -a
"service ntpd restart"
--limit ~
".*\.4"
#使用正则表达式匹配主机
|
六:管理系统用户和组
系统添加admin组
1
|
$ ansible app -m group -a
"name=admin state=present"
|
系统添加jwh566用户
1
|
$ ansible app -m user -a
"name=jwh5566 group=admin createhome=yes"
|
删除系统用户
1
|
$ ansible app -m user -a
"name=jwh5566 state=absent remove=yes"
|
七:管理文件和目录
获取文件的信息,权限,全部者等
1
|
$ ansible multi -m stat -a
"path=/etc/environment"
|
复制文件到服务器
1
|
$ ansible multi -m copy -a
"src=/etc/hosts dest=/tmp/hosts"
|
从服务器接收文件(接收到控制机)
1
|
$ ansible multi -m fetch -a
"src=/etc/hosts dest=/tmp"
|
建立目录
1
|
$ ansible multi -m
file
-a
"dest=/tmp/test mode=644 state=directory"
|
建立符号连接
1
2
|
$ ansible multi -m
file
-a "src=
/src/symlink
dest=
/dest/symlink
\
owner=root group=root state=link"
|
删除目录和文件
1
|
$ ansible multi -m
file
-a
"dest=/tmp/test state=absent"
|
八:运行后台任务
-B <seconds> 指定运行任务的最大时间
-P <seconds> 指定多久时间去一次服务器查看任务执行的状态
异步更新服务器(根据系统状况,可能须要很长时间)
1
2
3
4
5
6
7
|
$ 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
|
若是说后台任务还在运行,使用下面的命令查看运行状态
1
|
$ ansible multi -m async_status -a
"jid=763350539037"
|
九:检查日志文件
1
|
$ ansible multi -a
"tail /var/log/messages"
|
若是须要grep,须要使用shell模块
1
2
3
4
5
6
7
8
9
10
|
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 任务
1
2
|
$ ansible multi -m
cron
-a "name=
'daily-cron-all-servers'
\
hour=4 job=
'/path/to/daily-script.sh'
"
|
能够使用这个配置ntp 任务
删除crontab任务
1
|
$ ansible multi -m
cron
-a
"name='daily-cron-all-servers' state=absent"
|