文章目录
- 1、 常用模块-get url
- 2、常用模块-Archive、Unarchive
- 3、常用模块-selinux
- 4、常用模块-filewalld
- 5、常用模块-iptables
- 6、常用模块-yum_repository
- 7、常用模块-hostname
- 8、常用模块-sysctl
- 9、常用模块-lineinfile
1、 常用模块-get url
功能:通过互联网下载软件至本地
参数 | 选项 | 含义 |
url | HTTP,HTTPS | 下载地址 |
dest | 下载到本地的路径 | |
mode | 文件下载后的权限 | |
checksum | md5,sha256 | 对下载的资源进行校验 |
timeout | 10(defualt) | URL请求超时时间 |
示例1:下载互联网软件至本地
[root@jenkins-001 ~]# ansible webservers -m get_url -a 'url=https://mirrors.aliyun.com/zabbix/zabbix/6.3/rhel/7/x86_64/zabbix-js-6.4.0-alpha1.release1.el7.x86_64.rpm dest=/tmp mode=0666'
示例2: 下载互联网文件进行md5 校验
[root@web-002 tmp]# md5sum zabbix-js-6.4.0-alpha1.release1.el7.x86_64.rpm
55c5364b945da2add4a68c656bb79baa zabbix-js-6.4.0-alpha1.release1.el7.x86_64.rpm
[root@jenkins-001 ~]# ansible webservers -m get_url -a 'url=https://mirrors.aliyun.com/zabbix/zabbix/6.3/rhel/7/x86_64/zabbix-js-6.4.0-alpha1.release1.el7.x86_64.rpm dest=/tmp mode=0666 checksum=md5:55c5364b945da2add4a68c656bb79baa'
2、常用模块-Archive、Unarchive
功能:打包、解压
Archive
参数 | 选项 | 含义 |
path | 要压缩的文件或目录 | |
dest | 压缩后的文件 | |
fomat | bz2、gz、tar、xz、zip | 指定打包压缩的类型 |
示例1:将/var/log 目录压缩为tar格式,并存储到/opt目录下
[root@jenkins-001 ~]# ansible webservers -m archive -a 'path=/var/log dest=/opt/log.tar.gz format=gz'
Unarchive
参数 | 选项 | 含义 |
src | 控制的的源文件 | |
dest | 解压到被控端的路径 | |
remote_src: | yes/no | 源文件是否在被控端 |
示例1: 将控制端的压缩包,解压到被控端
[root@jenkins-001 ~]# ansible webservers -m unarchive -a 'src=./hello-world-war.tar.gz dest=/opt'
示例2: 将被控端的压缩包解压到被控端
[root@jenkins-001 ~]# ansible webservers -m unarchive -a 'src=/tmp/hello-world-war.tar.gz dest=/tmp remote_src=yes'
3、常用模块-selinux
示例1: 关闭selinux
[root@jenkins-001 ~]# ansible webservers -m selinux -a 'state=disabled'
4、常用模块-filewalld
参数 | 选项 | 含义 |
zone | 默认publuc | 要操作的区域 |
source | 来源地址 | |
service | http,https,sshd… | 服务名称 |
port | 端口 | |
permanent | 永久生效,但不会立即生效 | |
immediate | 临时生效 | |
state | disabled enabled | 启动 关闭 |
示例1: 让被控端放行80端口
[root@jenkins-001 ~]# ansible webservers -m systemd -a 'name=firewalld state=started'
[root@jenkins-001 ~]# ansible webservers -m firewalld -a 'port=80/tcp permanent=yes immediate=yes state=enabled'
[root@web-002 ihavecar]# firewall-cmd --list-all
public
target: default
icmp-block-inversion: no
interfaces:
sources:
services: ssh dhcpv6-client
ports: 80/tcp
5、常用模块-iptables
选项 | 参数 | 含义 |
table | 表 | |
chain | 链 | |
source | 源IP | |
destination | 目标IP | |
destination_port | 目标端口 | |
protocol | 协议 | |
jump | DROP | 动作 |
action | insert append | 插入 追加 |
示例1:源IP是1.1.1.1,目标地址 2.2.2.2,目标端口 80 ,协议tcp,动作拒绝,规则要写入第一行
[root@jenkins-001 ~]# ansible webservers -m iptables -a 'table=filter chain=INPUT source=1.1.1.1 destination=2.2.2.2 destination_port=80 protocol=tcp jump=DROP action=insert'
示例2: SNAT和DNAT
DNAT: 如果请求1.1.1.1:80端口,则转发到2.2.2.2:8080
[root@jenkins-001 ~]# ansible webservers -m iptables -a 'table=nat chain=PREROUTING protocol=tcp destination=1.1.1.1 destination_port=80 jump=DNAT to_destination="2.2.2.2:8080"'
SNAT:
[root@jenkins-001 ~]# ansible webservers -m iptables -a 'table=nat chain=POSTROUTING source=172.16.1.0/24 jump=SNAT to_source=172.16.1.1'
6、常用模块-yum_repository
功能: 建立yum仓库
参数 | 选项 | 含义 |
name | 名称,文件名称 | |
description | 描述信息,必须要填 | |
baseurl | 仓库的地址 | |
gpgcheck | 开启验证 | |
gpgkey |
示例1: 新建一个nginx 仓库
[root@jenkins-001 ~]# ansible webservers -m yum_repository -a 'name=ansible_nginx description=test baseurl="baseurl=http://x.org/packages/centos/$releasever/$basearch/" gpgcheck=yes gpgkey="https://nginx.org/keys/nginx_signing.key"'
[root@web-002 ihavecar]# ls /etc/yum.repos.d/
ansible_nginx.repo
7、常用模块-hostname
功能: 修改主机名称
示例1:
- hostname:
name: web01
8、常用模块-sysctl
功能:修改内存参数
# Set vm.swappiness to 5 in /etc/sysctl.conf
- sysctl:
name: vm.swappiness
value: '5'
state: present
# Remove kernel.panic entry from /etc/sysctl.conf
- sysctl:
name: kernel.panic
state: absent
sysctl_file: /etc/sysctl.conf
9、常用模块-lineinfile
功能: 与sed类似,对文件内容替换,删除,追加等
参数 | 选项 | 含义 |
path | 被控端的路径 | |
regexp | 正则匹配语法格式 | |
line | 填充内容 | |
state | present absent | |
insertafter | 将文本插入到“指定行”之后 | |
insertbefore | 将文本插入到“指定行之前” | |
backrefs | yes/no | 1.支持向后引用,2.当未匹配到内容不操作 |
backup | 是否在修改文件之前对文件备份 | |
create | 要操作的文件并不存在时,是否创建对应文件 |
示例1:替换https.conf文件^Listen 80 为Listen 8080
[root@jenkins-001 ~]# ansible webservers -m lineinfile -a 'path=/etc/httpd/conf/httpd.conf regexp="^Listen" line="Listen 8080'
示例2: 给主机添加网关
[root@jenkins-001 ~]# ansible webservers -m lineinfile -a 'path=/etc/sysconfig/network-scripts/if-eth0 line="GATEWAY=192.168.1.1'
示例3: 删除主机网关
[root@jenkins-001 ~]# ansible webservers -m lineinfile -a 'path=/etc/sysconfig/network-scripts/if-eth0 regexp="^GATEWAY" state=absent'
示例4:给主机增加一个网关,但是需要增加到ONBOOT下面:
[root@jenkins-001 ~]# ansible webservers -m lineinfile -a 'path=/sysconfig.../ifcfg-eth0 insertafter="ONBOOT=yes" line="GATEWYA=192.168.1.1"'