Ansible 常用模块2

1、 常用模块-get url

功能:通过互联网下载软件至本地

参数选项含义
urlHTTP,HTTPS下载地址
dest下载到本地的路径
mode文件下载后的权限
checksummd5,sha256对下载的资源进行校验
timeout10(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压缩后的文件
fomatbz2、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来源地址
servicehttp,https,sshd…服务名称
port端口
permanent永久生效,但不会立即生效
immediate临时生效
statedisabled
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协议
jumpDROP动作
actioninsert
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填充内容
statepresent
absent
insertafter将文本插入到“指定行”之后
insertbefore将文本插入到“指定行之前”
backrefsyes/no1.支持向后引用,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"'
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值