目录
- 常用模块
- fetch
- 知识点
- yum
- pip
- service
- cron
- crontab
- user
- group
- 练习:
常用模块
fetch
[fetʃ] # 将远程机器上的文件拉取到本地, 以 IP 或 hostname 生成目录, 并保留原目录的结构
dest # 本地主机的目标地址
src # 远程主机文件的源地址
ansible db -m fetch -a "dest=/tmp src=/var/log/cron"
知识点
1. yum 和 rpm的区别
yum会解决依赖关系
2. yum 源配置 : /etc/yum.repos.d/epel.repo
[epel] # 名称
name=Extra Packages for Enterprise Linux 7 - $basearch # 描述信息
baseurl=http://mirrors.aliyun.com/epel/7/$basearch # yum源的地址
failovermethod=priority
enabled=1 # 指定yum源是否可用, 1代表可用, 0代表不可用
gpgcheck=0 # 是否检查gpgkey文件, 0代表不检查, 1代表的检查
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
3. 包组
linux: lib windows: dll # 类似于提供API的文件, 想要调用, 需要存在lib文件
rpc # 远程过程调用
yum grouplist # 查看包组
yum groupinstall # 安装包组
4.查看包安装状态
yum list|grep redis # @代表安装成功
rpm -q redis # 查看redis包
rpm -qa # 查看所有的包
rpm -ql # 查看包安装生成的文件
yum
# 安装linux的软件包
disable_gpg_check # 是否要检查key
disablerepo # 禁用某个repo
enablerepo # 启用repo
name # 包名
state # 状态
- installed
- removed
ansible web -m copy -a "dest=/etc/yum.repos.d/ src=/etc/yum.repos.d/epel.repo"
ansible web -m yum -a "name=python2-pip" # 安装单个包
ansible web -m yum -a "name='@Development Tools'" # 安装包组
ansible web -m yum -a "name=redis,python2-pip" # 同时安装多个包
ansible web -m yum -a "name=redis state=absent" # 卸载安装包
pip
pip list # 查看所有的python第三方包
pip freeze > a.txt # 导出
pip install -r a.txt # 安装
# 安装python的第三方模块
requirements # 导出的文件
name # 要安装的Python库的名称或远程包的URL。
virtualenv # 虚拟环境
state # 状态
- present # 默认的,表示为安装
- absent # 删除
ansible web -m pip -a "name=django==1.11.18" # 安装django
ansible web -m pip -a "name=flask" # 默认为最新版本
service
`['sɜːvɪs]` # 管理远程主机上的服务
enabled # 设置开机自启动 -> [ɪ'nebld]
name # 指定需要操作的服务名称
state # 指定服务的状态
- started # 启动
- stopped # 关闭
- restarted # 重新启动
- reloaded # 重新加载
ansible web -m service -a "name=redis state=started" # 启动
ansible web -m service -a "name=redis state=stopped" # 关闭
ansible web -m service -a "name=redis state=started enabled=yes" # 启动并设置开机自启动
ps -ef|grep redis # 查看redis的进程
ss -tunlp # 查看端口信息
-t # tcp
-u # udp
-n # 显示端口号(数字)
-l # listening, 监听的端口
-p # processes, 进程
1. 启动服务
systemctl start redis # centos7
service redis start # centos6
2. 开机自启动
systemctl enable redis # centos7
chkconfig redis on # centos6
cron
`['krɔn]` # 管理远程主机中的计划任务
minute # 设置计划任务中分钟设定位的值
hour # 设置计划任务中小时设定位的值
day # 设置计划任务中日(天)设定位的值
month # 设置计划任务中月设定位的值
weekday # 设置计划任务中周几设定位的值
job # 指定计划的任务中需要实际执行的命令或者脚本
name # 设置计划任务的名称, 描述信息, 名称不可以重复, 不写名字, 会生成None
user # 设置当前计划任务属于哪个用户, 默认管理员用户
disabled # 禁用计划任务, 用 # 注释掉
backup # 在修改crontab之前创建一个备份
state # 状态
- absent # 删除计划任务 [ˈæbsənt;(for v.)əbˈsent]
ansible web -m cron -a "minute=21 job='touch /tmp/a.txt' name=touchfile" # 设置计划任务
ansible web -m cron -a "minute=23 job='touch /tmp/a.txt' name=touchfile disabled=yes"
ansible web -m cron -a "name=touchfile state=absent" # 删除计划任务
crontab
* * * * * job
0-59 0-23 1-31 1-12 0-7 job
分 时 日 月 周 任务
1 * * * * job # 代表每小时的第一个分钟
2/* * * * * job # 每隔2分钟执行job
1 10-19 * * * job # 代表10到19点的第一分钟
# 分钟不要用*,最好是指定时间
crontab -l # 查看计划任务
crontab -r # 删除所有的计划任务
crontab -e # 编辑计划任务
2. 计划任务 (定时任务)的用途:
- 收集日志
- 备份数据
- 同步时间
# 同步时间
yum install ntp
ntpdate time.windows.com
user
# 管理远程主机上的用户
group # 指定用户所在的基本主组
groups # 指定用户所在的附加组
home # 指定用户的家目录, 要指定一个不存在的目录
name # 指定要操作的用户名称
password # 指定用户的密码
shell # 设置用户的shell登录环境
remove # 删除用户并删除用户的家目录
system # 系统用户
uid # 指定用户的 uid 号
state # 状态
- present # 默认, 创建用户
- absent # 删除用户, 但不会删除用户家目录
ansible db -m user -a "name=alex2 shell=/sbin/nologin home=/opt/alex uid=2000 group=root" # 创建用户,并指定用户的家目录,不允许登陆shell, 设置uid, 以及组
ansible db -m user -a "name=alex system=yes" # 创建系统用户
ansible db -m user -a "name=alex state=absent" # 删除用户
ansible db -m user -a "name=alex state=absent remove=yes"
用户的分类: /etc/login.defs
- 超级管理员(root) # 0
- 其他用户
- 系统用户, 启动服务来专门设置的用户 # uid: 1-999-> centos7 1-499-> centos6
- 登录用户, 普通的登录用户 # 1000-65535-> centos7 500-65535-> centos6
useradd # 创建用户
-d # 指定家目录
-g # 组id
-G, --groups GROUPS # 附加组, 每次新建立用户, uid会在最大数加1
-r, --system # 创建系统用户
-s, --shell SHELL # 登陆shell
-u, --uid UID # 用户id
useradd -s /sbin/nologin -u 2000 -d /opt/wusir wusir # 创建用户, 指定用户的登陆shell
useradd -s /sbin/nologin -G root,wusir -d /opt/wusir wusir
useradd -r baoyuan # 创建系统用户, 从999倒序建立
userdel wusir # 删除用户, 不能删除家目录
userdel -r baoyuan4 # 删除用户并删除用户的家目录
group
# 在远程主机上创建用户组
gid # 组的id
name # 组名
system # 系统用户组
state # 状态
ansible db -m group -a "name=alex" # 创建普通组
ansible db -m group -a "name=wusir system=yes" # 创建系统组
ansible db -m group -a "name=wusir state=absent" # 删除组
用户组的分类: /etc/group
- 超级组(root) # 0
- 其他组
- 系统组 # 1-999-> centos7 1-499-> centos6
- 普通组 # 1000-65535-> centos7 500-65535-> centos6
groupadd # 创建用户组
-g # 指定组的id
-r # 指定系统组
groupdel # 删除用户组
练习:
- 创建alex10用户,指定用户的家目录为/opt/,指定用户的id为3000,指定用户的附加组为root
ansible db -m user -a "name=alex10 home=/opt/ uid=3000 groups=root"
- 创建wusir10用户组
ansible db -m gorup -a "name=wusir10"
- 将本地的/etc/fstab 复制到远程,并指定属主是alex10,属组是wusir10
ansible db -m copy -a "src=/etc/fstab dest=/opt/ owner=alex10 group=wusir10"
- 安装redis并启动,设置开机自启动
ansible web -m yum -a "name=redis"
ansible web -m service -a "name=redis state=started enabled=yes"
- 安装django
ansible web -m yum -a "name=python2-pip"
ansible web -m pip -a "name=django==1.11.18"
- 设置计划任务每天凌晨2点半备份/etc/目录,tar
ansible web -m cron -a "minute=30 hour=2 job='tar -cvf etc.tgz /etc/' name=backup"