模块详解html
官方模块mysql
#查看ansible模块的使用帮助 [root@m01 ~]# ansible-doc file [root@m01 ~]# ansible-doc #查看选项 [root@m01 ~]# ansible-doc -l #查看全部模块 1.ping模块 测试链接可通性,没有参数。通的话返回pong ansible all -m ping
2.setup模块 - name: Collect only facts returned by facter setup: gather_subset: - '!all' - '!any' - facter --tree :将全部主机的输出信息保存到/tmp/目录下,以/etc/ansible/hosts里的主机名为文件名 filter :过滤关键字 ansible all -m setup -a 'filter=ansible_distribution_version' --tree /tmp/xx #企业需求 1.根据不一样主机不一样IP建立对应IP的目录 2.根据不一样主机不一样主机名建立对应主机名的目录 3.自动化运维平台须要自动获取到主机的IP地址,内存信息,磁盘信息,主机名...等 4.若是安装数据库,分配内存为物理内存的80%,此时有3台不一样物理内存的机器2G、4G、16G 写一个playbook的状况下,我须要获取到对应主机的内存并做出计算,写判断。 #查看主机全部详细信息 [root@m01 ~]# ansible web01 -m setup #获取ip [root@m01 ~]# ansible web01 -m setup -a 'filter=ansible_default_ipv4' #获取主机名 [root@m01 ~]# ansible web01 -m setup -a 'filter=ansible_fqdn' #获取内存信息 [root@m01 ~]# ansible web01 -m setup -a 'filter=ansible_memory_mb' #获取磁盘信息 [root@m01 ~]# ansible web01 -m setup -a 'filter=ansible_devices' #其余信息参数 ansible_all_ipv4_addresses:仅显示ipv4的信息。 ansible_devices:仅显示磁盘设备信息。 ansible_distribution:显示是什么系统,例:centos,suse等。 ansible_distribution_major_version:显示是系统主版本。 ansible_distribution_version:仅显示系统版本。 ansible_machine:显示系统类型,例:32位,仍是64位。 ansible_eth0:仅显示eth0的信息。 ansible_hostname:仅显示主机名。 ansible_kernel:仅显示内核版本。 ansible_lvm:显示lvm相关信息。 ansible_memtotal_mb:显示系统总内存。 ansible_memfree_mb:显示可用系统内存。 ansible_memory_mb:详细显示内存状况。 ansible_swaptotal_mb:显示总的swap内存。 ansible_swapfree_mb:显示swap内存的可用内存。 ansible_mounts:显示系统磁盘挂载状况。 ansible_processor:显示cpu个数(具体显示每一个cpu的型号)。 ansible_processor_vcpus:显示cpu个数(只显示总的个数)
command模块linux
3.command模块 默认模块(可省略不指定) 做用:执行系统命令(linux windows),不支持变量,"<",">","|",";","&"等符号 [root@m01 ~]# ansible web_group -a 'ls' [root@m01 ~]# ansible web_group -a 'df -h'
cron模块nginx
4.cron模块 ansible-doc -s cron ansible db -m cron -a 'minute="" hour="" day="" month="" weekday="" job="" name="(必须填写)" state= 一、定时设置指定值的写入便可,没有设置的能够不写(默认是*) 二、name必须写 三、state有两个状态:present(添加(默认值))or absent(移除) #添加定时任务 ansible db -m cron -a 'minute="*/10" job="/bin/echo hello" name="test cron job" state="present"' 查看定时任务 ansible db -a "crontab -l" #移除定时任务 ansible db -m cron -a 'minute="*/10" job="/bin/echo hello" name="test cron job" state="absent"' ansible all -m cron -a "name=test state=absent" 查看定时任务 ansible db -a "crontab -l" # 注释相应定时任务,使定时任务失效 ansible web_group -m cron -a "name='ansible cron01' minute=0 hour=0 job='/bin/sh /server/scripts/test.sh' disabled=no"
5.user/group模块 user模块是请求的是useradd, userdel, usermod三个指令,goup模块请求的是groupadd, groupdel, groupmod 三个指令。 groups: 指定用户的附加组(必须是已经存在的组) uid: 指定用的uid password: 为用户设置登录密码,此密码是明文密码'加密后的密码' update_password:always/on_create always: 只有当密码不相同时才会修改密码(默认) # on_create: 只为新用户设置密码 name: 指定用户名 createhome: 是否建立家目录 yes|no(默认是yes) system: 是否为系统用户 yes|no(默认是no) remove: 当state=absent时,remove=yes则表示连同家目录一块儿删除,等价于userdel -r(默认是no) state: 是建立仍是删除 present(添加(默认值))or absent(移除) shell: 指定用户的shell环境(默认是/bin/bash) append:yes/no yes: 添加附加group(至关于usermod -aG) no: 修改附加组(默认)(至关于usermod -G) expires: 设置用户的过时时间,值是一个时间戳 comment: 注释 #建立用户,修改用户 ansible 'web_group' -m user -a ' ' name=ll name=gg comment=zs name=hh comment="zhushi" uid=1040 group=adm name=hh shell=/bin/bash groups=adm,lp append=yes name=hh state=absent remove=yes name=hh shell=/bin/bash groups=adm expires=1422403387 name="testops" password="$6$0lwTSmqKOkL.ktgl$OnBexXC7haBf0FRHVMIZM2edDeFWBbpKJ2r9cxVwNvY.vh3IIUzwFz8n7jFglc0CrtQSY12ziDonVL6e71Og2." #生成密钥时,只会生成公钥文件和私钥文件,和直接使用ssh-keygen指令效果相同,不会生成authorized_keys文件。 name=test generate_ssh_key=yes ssh_key_bits=2048 ssh_key_file=.ssh/id_rsa #指定password参数时,不能使用后面这一串密码会被直接传送到被管理主机的/etc/shadow文件中,因此须要先将密码字符串进行加密处理。而后将获得的字符串放到password中便可。 #删除用户 注意该用户下不能有任何进程,不然会报错(可是能删除的都删除了) ansible 'web_group' -m user -a ' ' name=ll state=absent remove=yes
生成加密的密码git
在不一样的主机上,建立以主机名命名的目录web
[root@m01 ~]# vim 2.yml - hosts: web_group tasks: - name: 233 file: path: /root/{{ ansible_fqdn }} state: directory owner: root group: root mode: 0755 [root@m01 ~]# ansible-playbook 2.yml [root@web01 ~]# ll total 8 drwxr-xr-x 2 root root 6 Jun 1 00:41 web01 #查看ansiable-playbook命令 [root@m01 ~]# ll /usr/bin/ansible #TAB ansible ansible-console-2.7
copy模块sql
6.copy模块 [root@m01 ~]# ansible-doc copy #查看copy模块用法 src: #源文件或目录,要复制到远程主机的文件在本地的地址,能够是绝对路径,也能够是相对路径。若是路径是一个目录,它将递归复制。在这种状况下,若是路径使用"/"来结尾,则只复制目录里的内容,若是没有使用"/"来结尾,则包含目录在内的整个内容所有复制,相似于rsync(#)。 content:用于替代"src",能够直接设定指定文件的值 dest: #目标目录 owner: foo #属主 group: foo #属组 mode: '0644' #权限,数字,字母,UGO backup: yes #当出现同名的文件,直接覆盖,由于默认是no force:若是目标主机包含该文件,但内容不一样,若是设置为yes,则强制覆盖,若是为no,则只有当目标主机的目标位置不存在该文件时,才复制。默认为yes 修改权限时候 须要加0 例如:chmod 0644 0755 -------------------------------------------------- #远程批量拷贝,至关于scp,rsync 目标目录下文件存在的话会报错,被控端主机用户不存在会报错(可是它把能作的都作了) [root@m01 ~]# ansible 'web_group' -m copy -a 'src=/root/hosts dest=/root owner=www group=www mode=0644' [root@web01 /]# yum install -y httpd [root@web01 ~]# systemctl start httpd [root@web01 ~]# echo 233 > /var/www/html/index.html #浏览器访问 ------------------------------------------------------------ #远程拷贝文件,而且将原来的同名文件备份,若是文件名和文件内容,属主属组和权限都同样就不作拷贝了(绿),backup=no能够省略,属主属组是root的时候能够不写 [root@m01 /]# ansible 'web_group' -m copy -a 'src=/root/hosts dest=/root owner=www group=www mode=0644 backup=yes' 当dest目录不存在时,自动建立,而且将源文件或目录放到目标目录下(不更名) ---------------------------------------------------------------- #简单的文件直接能够直接使用content在命令行copy,不须要copy文件 [root@m01 ~]# ansible 'web_group' -m copy -a 'content="/data 712.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)" dest=/etc/exports'
file模块docker
7.file模块 设定文件属性和建立文件的符号连接 file: path: /etc/foo.conf #指定建立的目录或文件 state: touch #建立文件 directory #建立目录 absent #删除目录或文件 link #作软连接 owner: foo #属主 group: foo #属组 mode: '0644' #权限 recurse #递归 1 force:须要在两种状况下强制建立软连接,一种是源文件不存在但以后会创建的状况下;另外一种是目标软连接已存在,须要先取消以前的软链,而后建立新的软链,有两个选项:yes|no 2 group:定义文件/目录的属组 3 mode:定义文件/目录的权限 4 owner:定义文件/目录的属主 5 path:必选项,定义文件/目录的路径(不存在的话能够直接建立)# 6 recurse:递归的设置文件的属性,只对目录有效(默认开启)(指定recurce的话,能够修改已经存在的文件或者目录的权限) 7 src:要被连接的源文件的路径,只应用于state=link的状况 8 dest:被连接到的路径,只应用于state=link的状况 9 state: directory:若是目录不存在,建立目录 file:即便文件不存在,也不会被建立 link:建立软连接 hard:建立硬连接 touch:若是文件不存在,则会建立一个新的文件,若是文件或目录已存在,则更新其最后修改时间 (不能在不存在的目录下建立文件,这一点和touch命令同样) absent:删除目录、文件或者取消连接文件 #远程批量建立目录 不指定属主属组,默认是root(当前用户),不指定mode,那么权限为0000 [root@lb01 /code]# ansible 'web_group' -m file -a 'path=/backup state=directory owner=adm group=adm mode=0000' #远程批量建立文件(上级目录必须存在) [root@lb01 /code]# ansible 'web_group' -m file -a 'path=/backup state=touch owner=adm group=adm mode=0000' #远程批量删除目录或文件 [root@lb01 /code]# ansible 'web_group' -m file -a 'path=/backup state=absent owner=adm group=adm mode=0000' #远程批量建立目录,(不指定权限的话,属主属组为root,连接文件权限是777) [root@lb01 /code]# ansible 'web_group' -m file -a 'src=/backup/a.txt dest=/b.txt state=link owner=adm group=adm mode=0000'
yum模块shell
8.yum模块 name #包名,用等于号表示(指定要安装的软件包的名称) file:// #指定本地安装路径,=yum localinstall -y http:// #指定yum仓库 state #指定动做,用等于号表示 installed.present #安装软件包 removed.absent #删除软件包 latest #安装最新版本的软件包 exclude=kernel*,foo* #排除某些包 list=ansible #相似于yum list查看是否能够安装 disablerepo="epel,ol7_latest" #禁用指定的yum仓库 download_only=true #只下载不安装 yum install d #下载安装,使用被控端的源安装软件包 [root@m01 ~]# ansible 'web_group' -m yum -a 'name=vsftpd state=present' #下载安装,指定源的rpm包,至关于wget,能够指定包名和协议 [root@m01 ~]# ansible 'web_group' -m yum -a 'name=https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-agent-5.0.0-1.el7.x86_64.rpm state=present' #安装,前提是客户端指定目录下有这个rpm包(至关于yum localinstall) [root@m01 ~]# ansible 'web_group' -m yum -a 'name=/root/zabbix-agent-5.0.0-1.el7.x86_64.rpm state=present' #卸载 [root@m01 ~]# ansible 'web_group' -m yum -a 'name=zabbix-agent-5.0.0-1.el7.x86_64 state=absent' [root@m01 ~]# ansible 'web_group' -m yum -a 'name=zabbix state=absent' #扩展 [root@localhost ~]# ansible all -m yum -a "name=httpd state=latest disable_gpg_check=yes enablerepo=epel" #state (Choices: present, installed, latest, absent, removed)[Default: present] #disable_gpg_check:禁止gpg检查 #enablerepo:只启动指定的repo
service模块数据库
9.service模块 1 arguments:给命令行提供一些选项 2 enabled:是否开机启动 yes|no 3 name:必选项,服务名称 4 pattern:定义一个模式,若是经过status指令来查看服务的状态时,没有响应,就会经过ps指令在进程中根据该模式进行查找,若是匹配到,则认为该服务依然在运行 5 runlevel:运行级别 6 sleep:若是执行了restarted,在则stop和start之间沉睡几秒钟 7 state:对当前服务执行启动,中止、重启、从新加载等操做(started,stopped,restarted,reloaded) #保持服务启动并设置为开机自启 ansible web_group -m service -a 'enabled=yes name=httpd state=started'
10.shell模块 尤为是用到复杂命令时(如带管道符等等),可是shell模块不能作mysql的主从复制,可是nginx编译安装的时候只能使用shell模块 #企业中通常不让使用shell模块 #使用ansible批量操做集群的主机 [root@m01 ~]# ansible 'web_group' -m shell -a 'free -m' [root@m01 ~]# ansible 'web_group' -m conmand -a 'free -m' [root@m01 ~]# ansible web01 -m shell -a "ps -ef|grep httpd" 命令的最后也能够加 -f number ,表示使用的并发进程数目,默认是5个 ansible webserver -a 'netstat -ulntp' -f 15 /usr/bin/ansible 默认使用当前ansible 服务器登录的用户来进行管理,若是你不喜欢这样,也可使用 -u username 的方式来指定用户 [root@docker ~]# ansible webserver -a "ls" -u zhangsan -f 9
11.script模块 做用:将本地脚本复制到远程主机,并执行。(不须要给脚本添加执行权限) #编辑脚本(只须要放在m01就好) vim /root/dir.sh mkdir syy #执行 absible 'web01' -m script -a '/root/dir.sh'
12.synchronize模块 1 archive: 归档,至关于同时开启recursive(递归)、links、perms、times、owner、group、-D选项都为yes ,默认该项为开启 2 checksum: 跳过检测sum值,默认关闭 3 compress:是否开启压缩 4 copy_links:复制连接文件,默认为no ,注意后面还有一个links参数 5 delete: 删除不存在的文件,delete=yes 使两边的内容同样(即以推送方为主),默认no 6 dest:目录路径(绝对路径或者相对路径) 7 dest_port:默认目录主机上的端口 ,默认是22,走的ssh协议 8 dirs:传速目录不进行递归,默认为no,即进行目录递归 9 rsync_opts:经过传递数组来指定其余rsync选项。 10 set_remote_user:主要用于/etc/ansible/hosts中定义或默认使用的用户与rsync使用的用户不一样的状况 11 mode: push或pull 模块,push模的话,通常用于从本机向远程主机上传文件,pull 模式用于从远程主机上取文件 12 src: 要同步到目的地的源主机上的路径; 路径能够是绝对的或相对的。若是路径使用”/”来结尾,则只复制目录里的内容,若是没有使用”/”来结尾,则包含目录在内的整个内容所有复制 src=some/relative/path dest=/some/absolute/path rsync_path="sudo rsync" src=some/relative/path dest=/some/absolute/path archive=no links=yes src=some/relative/path dest=/some/absolute/path checksum=yes times=no src=/tmp/helloworld dest=/var/www/helloword rsync_opts=--no-motd,--exclude=.git mode=pull
13.mount模块 1 dump fstype:必选项,挂载文件的类型 2 name:必选项,挂载点 3 opts:传递给mount命令的参数 4 src:必选项,要挂载的文件 5 state:必选项 present:只处理fstab中的配置 absent:删除挂载点 mounted:自动建立挂载点并挂载之 umounted:卸载 name=/mnt/dvd src=/dev/sr0 fstype=iso9660 opts=ro state=present name=/srv/disk src='LABEL=SOME_LABEL' state=present name=/home src='UUID=b3e48f45-f933-4c8e-a700-22a159ec9077' opts=noatime state=present ansible test -a 'dd if=/dev/zero of=/disk.img bs=4k count=1024' ansible test -a 'losetup /dev/loop0 /disk.img' ansible test -m filesystem 'fstype=ext4 force=yes opts=-F dev=/dev/loop0' ansible test -m mount 'name=/mnt src=/dev/loop0 fstype=ext4 state=mounted opts=rw'
14.get_url [root@m01 ~]# ansible-doc get_url url: http://example.com/path/file.conf #自定下载文件的URL dest: /etc/foo.conf #指定下载的目录 mode: '0440' #指定下载后的权限 owner group force_basic_auth: yes #文件名相同直接覆盖 checksum md5 #md5校验 sha256 #sha校验 #远程链接并下载(串行,速度慢) [root@m01 ~]# ansible 'web_group' -m get_url -a 'url=http://test.driverzeng.com/Nginx_Code/wordpress-4.9.4-zh_CN.tar.gz dest=/root mode=000' #校验MD5并下载 [root@m01 ~]# ansible 'web_group' -m get_url -a 'url=http://test.driverzeng.com/Nginx_Code/wordpress-4.9.4-zh_CN.tar.gz dest=/root mode=000 checksum= md5:b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c'
15.yum_repository,#更改yum源 [root@m01 ~]# ansible-doc yum_repository yum_repository: name: epel #不能省略,#指定仓库文件名和仓库名,自动添加.repo description: #至关于仓库内的name注释 baseurl: https://download.fedoraproject.org/pub/epel/$releasever/$basearch/ file: external_repos #指定仓库文件名(优先级高),不指定的话仓库文件名和仓库名同样,不能在同一个仓库文件内添加多个仓库名不一样的仓库,同时指定的话file=仓库文件名,name=仓库名 gpgcheck #是否开启校验 yes no enabled #是否启用yum仓库 yes no present #建立yum仓库(默认) absent #删除yum仓库 -------------------------------------------------------- #批量添加yum仓库,不能一次在某一主机添加多个仓库文件和仓库,功能有限 [root@m01 ~]# ansible 'web_group' -m yum_repository -a 'name=syy_add_epel description=EEE baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=no enabled=yes file=zdy' #在客户端已存在的仓库文件内添加仓库(1.指定仓库文件。2.指定仓库名,存在即修改,不存在即添加) [root@m01 ~]# ansible 'web_group' -m yum_repository -a 'name=syy2_add_epel description=EEE baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=no enabled=yes file=zdy' -------------------------------------------------------------- #批量删除yum仓库,只能一个一个删除仓库文件(也就是说不能只指定name来批量删除),file优先级高,能够只指定name,也能够同时指定(视状况而定) (仓库文件名和仓库名要对应,最好都写上) [root@m01 ~]# ansible 'web_group' -m yum_repository -a 'name=syy_add_epel file=zdy state=absent' -------------------------------------------------------------- #修改 不能修改仓库文件名,也不能修改仓库名,能够修改URL [root@m01 ~]# ansible 'web_group' -m yum_repository -a 'name=syy_add_epelllll description=EEE baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=no enabled=yes file=1' 也能够修改gpgcheck和enable [root@m01 ~]# ansible 'web_group' -m yum_repository -a 'name=syy_add_epel description=EEE baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=no enabled=no file=1' #使用playbook执行yml脚本,能够实现更精确的操做
systemd [root@m01 ~]# ansible-doc systemd systemd: #模块 state: started #启动 name: httpd #指定服务名称 state: stopped #中止 state: restarted #重启 state: reloaded #重载配置文件 enabled: yes #设置开机自启 masked: no # #远程中止服务 [root@m01 ~]# ansible 'web_group' -m systemd -a 'name=nginx state=stopped ' #远程启动服务(并设置开机自启动) [root@m01 ~]# ansible 'web_group' -m systemd -a 'name=nginx state=started enabled=yes '
[root@m01 ~]# ansible-doc group name: somegroup #指定组名 state: present #建立(默认,课省略) state: absent #删除 gid #指定建立的组的gid #远程建立组 [root@m01 ~]# ansible 'web_group' -m group -a 'name=dd gid=233 state=present' #远程删除组 [root@m01 ~]# ansible 'web_group' -m group -a 'name=dd gid=233 state=absent'
user: #模块 name: johnd #指定用户名 uid: 1040 #指定UID group: admin #指定主组 shell: /bin/bash(/sbin/nologin) #指定登陆shell groups: admins,developers #添加附加组(group=888) state: absent(present) #删除用户 comment #建立用户的时候添加一段注释 generate_ssh_key: yes #建立公钥 ssh_key_bits: 2048 #指定公钥长度 ssh_key_file: .ssh/id_rsa #建立私钥 create_home=false #是否建立家目录(默认建立true) ------------------------------------------------------ #远程建立用户,前提是用户组必须存在(必须带单引号) [root@web ~]# groupadd dd -g 233 [root@m01 ~]# ansible 'web_group' -m user -a 'name=hh uid=171 group=dd state=present shell=/sbin/nologin create_home=false' -------------------------------------------------------- #远程删除用户 [root@m01 ~]# ansible 'web_group' -m user -a 'name=hh state=absent' -------------------------------------------------------- #远程建立用户,并生成密钥对,也能够只给用户生成密钥对 [root@m01 ~]# ansible 'web_group' -m user -a 'name=hh uid=888 group=root shell=/bin/bash generate_ssh_key=yes ssh_key_bits=2048 ssh_key_file=.ssh/id_rsa' -i ./hosts ---------------------------------------------------------------- #将明文密码进行hash加密 [root@m01 ~]# ansible web_group -m debug -a "msg={{ 'zls' | password_hash('sha512', 'salt') }}" -i ./hosts #建立用户,并制定密码 [root@m01 ~]# ansible web_group -m user -a 'name=zls1 password=$6$salt$gaWhNcZweYlKQcLU1CqyY/UbYqIeUffVz6ESj87aMNfMX.xYBx0Z.67wzLN/hkkxmNut7SvkksPZ2Zlrse98m/ create_home=true shell=/bin/bash' -i ./hosts
[root@m01 ~]# ansible web01 -m fetch -a "src=/root/lol.txt dest=/root"
present # 开机挂载,即将挂载配置写入/etc/fstab mounted # 挂载设备,并将配置写入/etc/fstab unmounted # 卸载设备,不会清除/etc/fstab写入的配置 absent # 卸载设备,会清理/etc/fstab写入的配置 [root@m01 ~]# ansible web_group -m mount -a "src=172.16.1.31:/data path=/data fstype=nfs opts=defaults state=present" [root@m01 ~]# ansible web01 -m mount -a "src=172.16.1.31:/data path=/data fstype=nfs opts=defaults state=mounted"
功能: 获取远程文件的状态信息,包括atime,ctime,mtime,md5,uid,gid等信息。
[root@localhost ~]# ansible all -m stat -a "path=/etc/sysctl.conf"
#修改配置文件关闭selinux(永久关闭),必须重启 [root@m01 ~]# ansible web_group -m selinux -a 'state=disabled' -i ./hosts #临时关闭 [root@m01 ~]# ansible web_group -m shell -a 'setenforce 0' -i ./hosts #查看 [root@m01 ~]# ansible web_group -m shell -a 'getenforce' -i ./hosts
service #指定开放或关闭的服务名称 port #指定开放或关闭的端口 permanent #是否添加永久生效 # immediate #临时生效 state #永久开启或者关闭 enabled disabled zone #指定配置某个区域 rich_rule #配置辅规则 masquerade #开启地址假装 source #指定来源IP [root@m01 ~]# ansible web_group -m firewalld -a 'service=http permanent=yes state=enabled' -i ./hosts [root@m01 ~]# ansible web_group -m firewalld -a "service=http immediate=yes permanent=yes state=enabled" -i ./hosts [root@m01 ~]# ansible web_group -m firewalld -a "port=8080-8090/tcp immediate=yes permanent=yes state=enabled" -i ./hosts