service模块
1)帮助语法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
[root@m01 ~]# ansible-doc service EXAMPLES: - name: Start service httpd, if not started service: name: httpd state: started enabled: yes name: httpd #服务的名字 state: started #启动服务 stopped #停止服务 restarted #重启服务 reloaded #重载服务 enabled: #开机自启 yes no |
2)service实例
1 2 3 4 5 |
#1.停止nginx服务 [root@m01 ~]# ansible web03 -m service -a 'name=nginx state=stopped' #2.启动httpd服务,并加入开机自启 [root@m01 ~]# ansible web03 -m service -a 'name=httpd state=started enabled=yes' |
Systemd模块
1)帮助语法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
[root@m01 ~]# ansible-doc systemd EXAMPLES: - name: Start service httpd, if not started systemd: name: httpd state: started enabled: yes daemon_reload: yes name: httpd #服务的名字 state: started #启动服务 stopped #停止服务 restarted #重启服务 reloaded #重载服务 enabled: #开机自启 yes no daemon_reload: #后台启动 |
2)systemd实例
1 2 3 4 5 |
#1.停止nginx服务 [root@m01 ~]# ansible web03 -m systemd -a 'name=nginx state=stopped' #2.启动httpd服务,并加入开机自启 [root@m01 ~]# ansible web03 -m systemd -a 'name=httpd state=started enabled=yes' |
Group模块
1)帮助语法
1 2 3 4 5 6 7 8 |
EXAMPLES: - name: Ensure group "somegroup" exists group: name: somegroup #组名字 state: present #创建用户组 absent #删除用户组 gid: 666 #用户组ID |
2)实例
1 2 3 4 5 |
#创建用户组 [root@m01 ~]# ansible web_group -m group -a 'name=www state=present gid=666' #删除用户组 [root@m01 ~]# ansible web_group -m group -a 'name=www state=absent' |
user模块
1)帮助语法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
- name: Add the user 'johnd' with a specific uid and a primary group of 'admin' user: name: johnd #用户名 comment: John Doe #用户的注释 uid: 1040 #用户id group: admin #用户的组 groups: admins,developers #指定附加组 shell: /bin/bash #指定登录脚本 append: yes #添加附加组时使用 remove: yes #移除家目录 generate_ssh_key: yes #是否生成密钥对 ssh_key_bits: 2048 #秘钥加密的位数 ssh_key_file: .ssh/id_rsa #秘钥文件 expires: 1422403387 #用户的有效时间 state: present #添加用户 absent #删除用户 create_home:yes/no #是否创建家目录 password #给用户添加密码(单引号) |
2)实践
1 2 3 4 5 6 7 8 9 10 11 12 |
#1.创建用户 [root@m01 ~]# ansible web_group -m user -a 'name=www uid=666 group=www shell=/sbin/nologin state=present' #2.仅删除用户 [root@m01 ~]# ansible web_group -m user -a 'name=www state=absent' #3.删除用户及用户组 [root@m01 ~]# ansible web_group -m user -a 'name=www state=absent remove=yes' #注意: 1.如果用户名字跟组名字相同,删除用户是会将组也删除 2.当组下面有多个用户,删除的与组同名的用户也不会删除组 |
cron模块
1)帮助语法
1 2 3 4 5 6 7 8 9 10 11 12 13 |
EXAMPLES: - name: Ensure a job that runs at 2 and 5 exists. Creates an entry like "0 5,2 * * ls -alh > /d cron: name: "check dirs" #定时任务的注释 minute: "0" #分钟 hour: "5,2" #小时 day: "2" #日 month: "2" #月 weekday: "2" #周 job: "ls -alh > /dev/null" #定时任务的内容 state: absent #删除定时任务 present #添加定时任务 |
2)实践
1 2 3 4 5 6 7 8 9 10 11 |
#1.添加定时任务 [root@m01 ~]# ansible web01 -m cron -a 'name="时间同步" minute=*/10 job="/usr/sbin/ntpdate time1.aliyun.com &> /dev/null"' #2.修改定时任务(不修改名字,只修改内容) [root@m01 ~]# ansible web01 -m cron -a 'name="时间同步" job="/usr/sbin/ntpdate time1.aliyun.com &> /dev/null"' #3.删除定时任务(一定要用name参数) [root@m01 ~]# ansible web01 -m cron -a 'name="时间同步" state=absent' #4.注释定时任务 [root@m01 ~]# ansible web01 -m cron -a 'name="时间同步" job="/usr/sbin/ntpdate time1.aliyun.com &> /dev/null" disabled=yes' |
mount模块
1)帮助语法
1 2 3 4 5 6 7 8 9 10 11 12 13 |
EXAMPLES: # Before 2.3, option 'name' was used instead of 'path' - name: Mount DVD read-only mount: path: /mnt/dvd #挂载的目录(nfs客户端) src: /dev/sr0 #远端被挂载的目录 (nfs服务端) fstype: nfs #挂在类型 opts: ro,noauto #自动挂载的参数 state: present #写入自动挂载,但实际没有挂咋,需要重启服务器 unmounted #取消临时挂载,但是没有清理自动挂载 mounted #写入自动挂载,并且直接挂载了(常用) absent #取消临时挂载,并且清理自动挂载(常用) |
2)准备挂载的服务端
1 2 3 4 5 6 7 8 9 10 11 |
1.安装nfs [root@m01 ~]# ansible nfs -m yum -a 'name=nfs-utils state=present' 2.配置nfs [root@m01 ~]# ansible nfs -m copy -a 'content="/data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)" dest=/etc/exports' 3.创建用户 [root@m01 ~]# ansible nfs -m group -a 'name=www gid=666' [root@m01 ~]# ansible nfs -m user -a 'name=www uid=666 group=www shell=/sbin/nologin create_home=no' 4.创建目录并授权 [root@m01 ~]# ansible nfs -m file -a 'path=/data state=directory owner=www group=www' 5.启动 [root@m01 ~]# ansible nfs -m systemd -a 'name=nfs state=started' |
3)客户端使用模块挂载
1 2 3 4 5 |
#挂载目录,并加入开机自动挂载 [root@m01 ~]# ansible web01 -m mount -a 'src=172.16.1.31:/data path=/code/wordpress fstype=nfs state=mounted' #取消挂载,并取消开机自动挂载 [root@m01 ~]# ansible web01 -m mount -a 'src=172.16.1.31:/data path=/code/wordpress fstype=nfs state=absent' |
selinux模块
1)帮助语法
1 2 3 4 5 6 7 |
EXAMPLES: - name: Enable SELinux selinux: policy: targeted state: enforcing #开启 disabled #关闭 |
2)关闭selinux
1 |
[root@m01 ~]# ansible web01 -m selinux -a 'state=disabled' |
Firewalld模块
1)帮助语法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
EXAMPLES: - firewalld: service: https #防火墙开启的服务 permanent: #永久生效 yes no state: #临时生效 enabled #开启 disable #关闭 port: 8081/tcp 161-162/udp #防火墙配置的端口 zone: dmz #指定配置空间 rich_rule: #富规则 source: 192.0.2.0/24 #防火墙配置的源ip masquerade: yes #开启ip伪装 no #关闭ip伪装 interface: eth2 #绑定网卡 |
2)防火墙配置实践
1 2 3 4 5 6 7 8 9 10 11 |
#1.允许访问http,永久生效 [root@m01 ~]# ansible web01 -m firewalld -a 'service=http permanent=yes state=enabled' #2.允许80端口被访问,临时生效 [root@m01 ~]# ansible web01 -m firewalld -a 'port=80/tcp state=enabled' #3.允许10.0.0.0/24网段访问22端口 [root@m01 ~]# ansible web01 -m firewalld -a 'rich_rule="rule family=ipv4 source address=10.0.0.0/24 service name=ssh accept" state=enabled' #4.允许10.0.0.0/24网段访问所有服务 [root@m01 ~]# ansible web01 -m firewalld -a 'source=10.0.0.0/24 zone=trusted state=enabled permanent=yes' |
unarchive 解压模块
1)帮助语法
1 2 3 4 5 6 7 |
- name: Unarchive a file that is already on the remote machine unarchive: src: /tmp/foo.zip #要解压的包 dest: /usr/local/bin #解压到目标位置 remote_src: yes #要解压的包在受控端 no #要解压的包在控制端 |
2)实例
1 2 3 4 5 |
#1.解压控制端的包到受控端 [root@m01 /package]# ansible web01 -m unarchive -a 'src=/package/php.tar.gz dest=/tmp/' #2.解压受控端的包到受控端 [root@m01 /package]# ansible web03 -m unarchive -a 'src=/package/php.tar.gz dest=/tmp/ remote_src=yes' |
archive 压缩模块
1)帮助语法
1 2 3 4 5 6 |
EXAMPLES: - name: Compress directory /path/to/foo/ into /path/to/foo.tgz archive: path: /path/to/foo #要压缩的文件或目录 dest: /path/to/foo.tgz #压缩后的文件 format:bz2, gz, tar, xz, zip #指定打包的类型 |
2)实例
1 2 |
#1.打包站点目录 [root@m01 /package]# ansible web01 -m archive -a 'path=/code dest=/tmp/code.tar.gz' |