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"
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...