ansible 软件相关模块

一 yum 模块

1、yum配置源(/etc/yum.repos.d/epel.repo)

1 [epel]
2 name=Extra Packages for Enterprise Linux 7 - $basearch #名字
3 baseurl=http://mirrors.aliyun.com/epel/7/$basearch  #rpm源的地址,可以写http,https,ftp,Samba,file:
4 failovermethod=priority
5 enabled=1 # 是否开启,1代表开启,0表示关闭
6 gpgcheck=0  #是否校验签名,1代表校验,0表示校验
7 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

2、yum安装包组

1 yum grouplist # 查看包组信息
2 yum groupinstall # 安装包组

3、部分参数(ansible-doc yum)

1 disablerepo #禁用源
2 enablerepo #启用源
3 name #包名
4 state  install (`present' or `installed', `latest'), or remove (`absent' or `removed')

4、使用方式

1 ansible web -m yum -a 'name=wget' # 安装wget
2 ansible web -m yum -a 'name=python2-pip' # 安装python2-pip
3 ansible web -m yum -a 'name=wget state=absent' # 卸载软件包
4  ansible web -m yum -a 'name="@Development Tools"' # 安装包组

二、pip模块

1 pip install 安装包
2 pip freeze > a.txt 将python的环境打包到文件中
3 pip install -r a.txt 安装文件中的包
4 pip list 查看所有的以安装成功的包
在远程主机安装模块(如安装flask模块):

ansible web -m pip -a 'name=flask'

三、service服务控制命令(可控制远程主机)

1 ps -ef|grep nginx #查看进程
2 ss -tnlp # 查看端口信息
3 systemctl start nginx # centos7
4 service nginx start  # centos6
5 systemctl enabled nginx # centos7 开机自启动
6 chkconfig nginx on # centos6开机自启动
1 # 启动nginx
2 ansible web -m service -a 'name=nginx state=started'
3 # 关闭nginx
4 ansible web -mservice -a 'name=nginx state=stopped'

四、计划任务 (cron模块)

1 * * * * * job 
2 分 时 日 月 周 任务
3 0 */2 *  * *  job  每隔两个小时
4 0 12,13 * * * job 12点和13点
5 0 12-17 * * * job 12点到17点
6 0 12-17/2 * * 1,3,6,0 周1,周3,周6,周7 12点到17点每隔两个小时 
7 crontab -e # 编辑计划任务
8 crontab -l # 查看计划任务
9 crontab -r # 删除计划任务
1 ansible db -m cron -a 'minute=26 job="touch /tmp/xzmly.txt" name=touchfile' # 新建一个计划任务
2 ansible db -m cron -a 'name=touchfile state=absent' # 删除一个计划任务
3 ansible db -m cron -a 'minute=26 job="touch /tmp/xzmly.txt" name=touchfile disabled=yes'  # 禁用计划任务,以#表示禁用

五、与用户相关的模块

1、user模块

用户基本分类:

 1 用户:
 2     管理员  root 0
 3     普通用户
 4         系统用户  不能登录  1-999 centos7 1-499 centos6
 5         登录用户  可以登录  1000-65535 centos7 500-65535 centos6
 6 用户组:
 7     管理员组 root 0
 8     系统用户组 1-999 centos7 1-499 centos6
 9     登录用户组 1000-65535 centos7 500-65535 centos6 
10     
11  -d  指定用户的家目录
12  -g  指定用户的组
13  -G  执行用户的附加组
14  -s  指定登录后使用的shell
15  -r 创建一个系统组
16  useradd -r wusir  创建系统用户, 从999倒序
17  useradd -s /sbin/nologin alexsb 创建的是普通用户,从1000开始升序
18   useradd -d /opt/alexsb2 alexsb2 创建用户时指定用户的家目录
19    useradd -u 3000 alexsb6 # 创建用户并指定用户的uid
20   userdel alex 删除用户
21   userdel -r alexsb2 删除用户并删除用户的家目录
22   
23   groupadd yuchao 创建用户组
24   groupdel yuchao 删除用户组

使用user模块命令:

 1 group 组
 2 groups 附加组
 3 home 家目录
 4 name 用户名
 5 password 密码
 6 remove ?
 7 shell 用户登录后使用的shell
 8 system 创建一个系统用户
 9 uid 用来指定用户的id
10 state 状态
11 ansible db -m user -a 'name=wulaoshi uid=4000 home=/opt/wulaoshi groups=root shell=/sbin/nologin' #创建一个用户,并指定用户的id,用户的家目录,用户的附加组,用户的shell
12 ansible db -m user -a 'name=wulaoshi state=absent' #删除用户但是不删除用户的家目录
13 ansible db -m user -a 'name=wulaoshi3 state=absent remove=yes' # 删除用户并删除用户的家目录

2、group模块

基本使用:

1 gid 组的id
2 name 组名
3 system 系统组
4 state
5 ansible db -m group -a 'name=wulaoshi system=yes' #创建系统组
6 ansible db -m group -a 'name=group1' # 创建普通组
7 ansible db -m group -a 'name=wulaoshi state=absent' # 删除组

注:删除用户和用户组都有参数:state=absent ,删除用户家目录参数:remove=yes

原文地址:https://www.cnblogs.com/liaopeng123/p/10408665.html