Ansible - 03

ansible模块

ad-hoc常用模块
coansibleand        # 执行shell命令(不支持管道等特殊字符)
shell               # 执行shell命令
script              # 远程执行shell脚本
yum_repository      # 配置yum仓库
yum                 # 安装软件
copy                # 变更配置文件
file                # 创建目录或文件
service             # 启动与停止服务
systemd             # 启动与停止服务
mount               # 挂载设备
cron                # 定时任务
get_url             #下载软件
firewalld           #防火墙
selinux             #selinux
setup			   #获取主机信息
一、ansible 软件管理模块
1. yum_repository 模块
[root@m01 ~]#ansible-doc yum_repository
EXAMPLES:
- name: Add repository
  yum_repository:
    name: epel
    description: EPEL YUM repo
    baseurl: https://download.fedoraproject.org/pub/epel/$releasever/$basearch/
    file: external_repos
    enabled: no
    state: absent

#添加yum源
[root@m01 ~]# ansible web_group -m yum_repository -a 'name=nginx.repo description="nginx stable repo" baseurl="http://nginx.org/packages/centos/7/$basearch/" enabled=1 gpgcheck=1 file=nginx state=present'

#查看添加的yum源
[root@web01 ~]# cat /etc/yum.repos.d/nginx.repo
[nginx.repo]
name = nginx stable repo
baseurl = http://nginx.org/packages/centos/7/$basearch/
enabled = 1
gpgcheck = 0  #无需验证


#参数介绍
name			#yum源中 [] 里面的内容
description		#yum源中 name 部分的内容
baseurl			#yum源中 yum 仓库的地址
gpgcheck		#yum源中 gpgcheck,是否检查yum源
enabled			#yum源中的 enabled,是否开启yum源
file			#yum源的文件名字
mirrorlist       #源的列表
state             
                 absent    #删除
                 present   #添加


#注意:
	1.file参数不修改,name参数也不修改的前提下,是修改yum源
	2.file参数不修改,name参数修改的情况下,是添加yum源
2.yum模块
[root@m01 ~]# ansible-doc yum 
EXAMPLES: #直接去搜索 /EXAMPLES
- name: install the latest version of Apache
  yum:
    name: httpd
    state: latest
    
name:
	httpd					   #服务的名字
	file=/usr/local/		    #本地软件包
	http://nginx.org/...		#软件包在网上的地址
state:
	latest		#安装最新版本的包
	absent		#卸载软件包
	present		#安装软件包
2.案例
#直接yum安装服务tree
[root@m01 ~]# ansible web03 -m yum -a 'name=tree state=present'
相当于在远程机器上执行:yum install -y tree  #连续两次就ok了

#安装本地的rpm包(包一定先传到远程机器上)
[root@m01 ~]# ansible web_group -m yum -a 'name=/tmp/nginx-1.16.1-1.el7.ngx.x86_64.rpm state=present'
相当于在远程机器上执行:yum localinstall -y /tmp/nginx-1.16.1-1.el7.ngx.x86_64.rpm
 
#安装云上的服务
[root@m01 ~]# ansible web_group -m yum -a 'name=https://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-agent-4.0.0-2.el7.x86_64.rpm state=present'
相当于在远程机器上执行:yum install -y https://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-agent-4.0.0-2.el7.x86_64.rpm

#卸载服务包
[root@m01 ~]# ansible web_group -m yum -a 'name=httpd state=absent'
二、文件管理模块
1. copy 模块
# 1、语法
[root@m01 ~]# ansible-doc copy
EXAMPLES:
- name: Copy file with owner and permissions
  copy:
    src: /srv/myfiles/foo.conf
    dest: /etc/foo.conf
    owner: foo
    group: foo
    mode: '0644'
    backup: yes
    content: '# This file was moved to /etc/other.conf'
    follow: yes
    
src			#文件的源地址
dest		#目标地址或文件
owner		#文件属主
group		#文件属组
mode		#文件的权限
backup		#替换的文件是否备份
yes:        #备份
no:         #不备份
content		#直接将内容写入文件
follow		#处理软连接
2.案例
# 先在ansible写好,然后在推过去
[root@m01 yum.repos.d]# vim nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=1
enabled=1
#推送yum源文件到远端 --方式一
[root@m01 yum.repos.d]# ansible web_group -m copy -a 'src=/etc/yum.repos.d/nginx.repo dest=/etc/yum.repos.d/'
#推送yum源文件到远端 --方式一
[root@m01 yum.repos.d]# ansible web_group -m copy -a 'src=/etc/yum.repos.d/nginx.repo dest=/etc/'

#推送rsync密码文件授权
[root@m01 ~]# ansible web_group -m copy -a 'src=/opt/ansible.txt dest=/etc/rsync.password mode=600'

#推送站点文件并授权属主属组
[root@m01 ~]# ansible web_group -m copy -a 'src=/root/index.html dest=/root/ owner=adm group=adm'

#推送文件并备份
[root@m01 ~]# ansible web_group -m copy -a 'src=/root/index.html dest=/root/ owner=adm group=adm backup=yes'
注意:buckup参数不是用来回滚的,需要回滚的话要备份原来m01上推送的文件

#直接将内容写入文件
[root@m01 ~]# ansible web_group -m copy -a 'content="rsync_backup:123456" dest=/tmp/rsync.password mode=600'
2.file模块
2.1 语法和参数
[root@m01 ~]# ansible-doc file
EXAMPLES:
- name: Change file ownership, group and permissions
  file:
  	src: /file/to/link/to
    path: /etc/foo.conf
    owner: foo
    group: foo
    mode: '0644'
    state: link
    recurse: yes
    
path		    #创建的文件或目录的地址
owner	    	#文件或目录的属主
group		    #文件或目录的属组
mode		    #文件或目录的权限
state
	link		#创建软链接
	src		    #源文件
	dest	    #软链接的名字
	touch		#创建文件
	directory	#创建目录
	absent		#删除,目录,文件,软链接
recurse		    #递归授权
2.2、实例
#单纯的创建目录
[root@m01 ~]# ansible web01 -m file -a 'path=/ansible state=directory'
相当于在远程机器上执行:mkdir /ansible

#创建目录并授权
[root@m01 ~]# ansible web01 -m file -a 'path=/mm state=directory owner=nginx group=nginx mode=755'
相当于在远程机器上执行:mkdir /mm && chown nginx.nginx /ansible

#递归创建目录,不需要加任何参数
[root@m01 ~]# ansible web01 -m file -a 'path=/mm/wordpress/wp-content/pic state=directory'

#递归授权目录
[root@m01 ~]# ansible web01 -m file -a 'path=/mm/ state=directory owner=nginx group=nginx recurse=yes'

#注意:
	1.当创建的目录不存在时,递归创建会递归授权
	2.当创建的目录已经存在,递归授权只授权最后一层目录下的内容
	
#创建文件 
[root@m01 ~]# ansible web01 -m file -a 'path=/opt/1.txt state=touch'

#创建文件并授权
[root@m01 ~]# ansible web01 -m file -a 'path=/opt/a.txt state=touch owner=nginx group=nginx'

#创建软链接
[root@m01 ~]# ansible web01 -m file -a 'src=/opt/a.txt dest=/tmp/1.ln state=link'
#注意
1.创建文件时,
3 .get_url 模块
3.1、语法和参数
[root@m01 ~]# ansible-doc get_url
EXAMPLES:
- name: Download foo.conf
  get_url:
    url: http://example.com/path/file.conf
    dest: /etc/foo.conf
    mode: '0440'
    checksum: sha256:d61e0e722fdc2c2b0d659e4155279a18
 
url			  #文件下载地址
dest		  #文件存放路径
mode		  #文件下载后授权
checksum	   #验证文件
sha256 md5sum  #加密方式
3.2案例
#下载网站上的文件
[root@m01 ~]# ansible web01 -m get_url -a 'url=http://192.168.1.107/1.txt dest=/opt'

#下载时验证文件
[root@m01 ~]# ansible web01 -m get_url -a 'url=http://192.168.1.107/1.txt dest=/opt checksum=md5:d61e0e722fdc2c2b0d659e4155279a18
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FikL-09-19

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值