12.18用户管理模块、其他模块

五、用户管理模块

1.group 模块

1)语法和参数

[root@m01 ~]# ansible-doc group
EXAMPLES:
- name: Ensure group "somegroup" exists
  group:
    name: somegroup			#组名字
    state: 
    	present				#创建组
        absent				#删除组
    gid: 666				#指定组id

2)实例

#创建用户组
[root@m01 ~]# ansible web01 -m group -a 'name=www gid=666 state=present'

#删除用户组
[root@m01 ~]# ansible web01 -m group -a 'name=www gid=666 state=absent'
2.user 模块

1)语法和参数

[root@m01 ~]# ansible-doc user
EXAMPLES:
- name: Add the user 'johnd' with a specific uid and a primary group of 'admin'
  user:
    name: johnd
    comment: John Doe
    uid: 1040
    group: admin
    shell: /bin/bash
    state: absent
    remove: yes
    create_home: false

name		#用户名字
comment		#用户备注
uid			#用户id
group		#用户所在的组名字
shell
	/bin/bash	#用户可以登录
	/sbin/nologin	#用户不需要登录
state
	absent		#删除用户
	present		#创建用户
remove			#移除家目录
create_home		#是否创建家目录
	true		#创建家目录
	false		#不创建家目录

2)实例

#创建用户,不需要登录,有家目录
[root@m01 ~]# ansible web01 -m user -a 'name=www uid=666 group=www shell=/sbin/nologin create_home=true'

#删除用户并移除家目录
[root@m01 ~]# ansible web01 -m user -a 'name=www state=absent remove=yes'

#注意:
	1.当组的名字与用户名字相同时,删除用户组也会被删除
	2.当组的名字与用户名字相同时,而组下面还有其他用户,则删除用户时不会删除同名组

六、其他模块

1.cron 定时任务模块

1)语法和参数

[root@m01 ~]# ansible-doc cron
EXAMPLES:
- name: Ensure a job that runs at 2 and 5 exists. Creates an entry like "0 5,2 * * ls -alh > /dev/null"
  cron:
    name: "check dirs"
    minute: "0"
    hour: "5,2"
    day: "*"
    month: "*"
    weekday: "*"
    job: "ls -alh > /dev/null"
    state: absent
    disabled: yes
   
name		#定时任务的备注
minute		#分钟
hour		#小时
day			#日
month		#月
weekday		#周
job			#指定的定时任务内容
state
	present	#新建定时任务
	absent	#删除定时任务
disabled
	yes		#注释定时任务
	no		#取消注释

2)实例

#添加定时任务,每五分钟执行一次时间同步(只配置分钟,其他不配置默认是 * )
[root@m01 ~]# ansible web01 -m cron -a 'name="测试ansible配置定时任务" minute=*/5 job="ntpdate time1.aliyun.com"'
#查看配置
[root@web01 html]# crontab -l
#Ansible: 测试ansible配置定时任务
*/5 * * * * ntpdate time1.aliyun.com

#注释定时任务
[root@m01 ~]# ansible web01 -m cron -a 'name="测试ansible配置定时任务" minute=*/5 job="ntpdate time1.aliyun.com" disabled=yes'

#删除定时任务
[root@m01 ~]# ansible web01 -m cron -a 'name="测试ansible配置定时任务" minute=*/5 job="ntpdate time1.aliyun.com" state=absent'

#注意:
	1.定时任务添加,是通过名字来区分是否一样的,如果不加name参数,则会添加重复的定时任务
	2.定时任务注释是通过 name 参数来注释的,所以一定要加 name 参数
	3.定时任务删除是通过 name 参数来删除的,所以一定要加 name 参数
2.mount 挂载模块

1)安装NFS

1.安装nfs
[root@m01 ~]# ansible nfs_server -m yum -a 'name=nfs-utils state=present'

2.配置nfs
[root@m01 ~]# ansible nfs_server -m copy -a 'content="/data 172.16.1.0/24(rw,sync,all_squash)" dest=/etc/exports'

3.创建目录
[root@m01 ~]# ansible nfs_server -m file -a 'path=/data state=directory owner=nfsnobody group=nfsnobody'

4.启动服务
[root@m01 ~]# ansible nfs_server -m systemd -a 'name=nfs state=started'

2)挂载模块语法和参数

[root@m01 ~]# ansible-doc mount
EXAMPLES:
- name: Mount DVD read-only
  mount:
    path: /mnt/dvd
    src: /dev/sr0
    fstype: iso9660
    opts: ro,noauto
    state: present
    
path		#本机准备挂载的目录
src			#远端挂载点
fstype		#指定挂载类型
opts		#挂载参数(/etc/fstab中的内容)
state
	present		#配置开机挂载,将配置写入自动挂载文件,并没有直接挂载
	unmounted	#取消挂载,但是没有删除自动挂载配置
	#常用配置
	mounted		#配置开机挂载,并且直接挂载上
	absent		#取消挂载,并且删除自动挂载配置

3)实例

#配置开机挂载,将配置写入自动挂载文件,并没有直接挂载
[root@m01 ~]# ansible web01 -m mount -a 'path=/data src=172.16.1.31:/data fstype=nfs opts=defaults state=present'

#配置开机挂载,并且直接挂载上
[root@m01 ~]# ansible web01 -m mount -a 'path=/data src=172.16.1.31:/data fstype=nfs opts=defaults state=mounted'

#取消挂载,但是没有删除自动挂载配置
[root@m01 ~]# ansible web01 -m mount -a 'path=/data src=172.16.1.31:/data fstype=nfs opts=defaults state=unmounted'

#取消挂载,并且删除自动挂载配置
[root@m01 ~]# ansible web01 -m mount -a 'path=/data src=172.16.1.31:/data fstype=nfs opts=defaults state=absent
3.selinux模块
- name: Disable SELinux
  selinux:
    state: disabled

#关闭selinux
[root@m01 ~]# ansible web02 -m selinux -a 'state=disabled'
web02 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "configfile": "/etc/selinux/config", 
    "msg": "", 
    "policy": "targeted", 
    "reboot_required": false, 
    "state": "disabled"
}
4.firewalld 模块

1)语法和参数

[root@m01 ~]# ansible-doc firewalld
EXAMPLES:
- firewalld:
    service: https			#指定服务
    permanent: 				#是否永久生效
    	yes					#永久生效
    	no					#临时生效
    state: 
    	enabled			    #允许通过
    port: 
    	8081/tcp			#指定端口
    zone: dmz				#指定区域
    rich_rule: rule service name="ftp" audit limit value="1/m" accept		#配置富规则
    source: 192.0.2.0/24	 #指定网段
    interface: eth2			#帮定网卡
    masquerade: yes			#开启IP伪装

2)实例

#允许访问http服务,永久生效
[root@m01 ~]# ansible web01 -m firewalld -a 'service=http permanent=yes state=enabled'
[root@m01 ~]# ansible web01 -m firewalld -a 'service=http state=enabled'

#允许访问80端口,临时生效
[root@m01 ~]# ansible web01 -m firewalld -a 'port=80/tcp state=enabled'

#配置允许10.0.0.0网段访问22端口
[root@m01 ~]# ansible web01 -m firewalld -a 'rich_rule="rule family=ipv4 source address=10.0.0.0/24 port port=22 protocol=tcp accept" state=enabled'

#配置网段白名单
[root@m01 ~]# ansible web01 -m firewalld -a 'source=10.0.0.0/24 zone=trusted state=enabled permanent=yes'
[root@m01 ~]# ansible web01 -m firewalld -a 'source=10.0.0.0/24 zone=trusted state=enabled permanent=no'
5.unarchive 解压模块

1)语法和参数

[root@m01 ~]# ansible-doc unarchive
EXAMPLES:
- name: Extract foo.tgz into /var/lib/foo
  unarchive:
    src: foo.tgz
    dest: /var/lib/foo
    remote_src: no			#默认是no
    
src			#包的路径
dest		#解压后的目标路径
remote_src
	yes		#包在受控端服务器上
	no		#包在控制端服务器上

2)实例

#解压包到受控端,包在控制端上
[root@m01 ~]# ansible web01 -m unarchive -a 'src=/root/php.tar.gz dest=/tmp'

#在受控端解压包,包在受控端
[root@m01 ~]# ansible web01 -m unarchive -a 'src=/tmp/php.tar.gz dest=/tmp remote_src=yes'
an
6.archive 压缩模块
[root@m01 ~]# ansible-doc archive
EXAMPLES:
- name: Compress directory /path/to/foo/ into /path/to/foo.tgz
  archive:
    path: /path/to/foo				#要打包的内容
    dest: /path/to/foo.tgz			#打好的包与存放位置
    format:gz					  #打包的类型 bz2, gz, tar, xz, zip

#打包实例
[root@m01 ~]# ansible web01 -m archive -a 'path=/tmp dest=/opt/php.tar.gz'

作业:
1.使用ansible搭建 lnmp 环境
2.搭建交作业系统

3.尝试搭建博客

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值