ansible常用模块汇总

ansible常用模块汇总

ansible官网模块自助

ansible命令模块

模块详解

官方模块

ping模块

功能:检测

测试连接可通性,没有参数。通的话返回pong

ansible all -m ping

setup模块

功能:获取主机信息(绿色,红色)

--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个数(只显示总的个数)

#取ip

[root@m01 ~]# ansible web01 -m setup -a 'filter=ansible_default_ipv4'|awk -F '["]' '/"address"/{print $4}'

10.0.0.7

[root@m01 ~]# ansible web_group -m setup -a 'filter=ansible_default_ipv4'|awk -F '["]' '/"address"/{print $4}'

10.0.0.7

10.0.0.9

10.0.0.8

#取主机名

[root@m01 ~]# ansible web01 -m setup -a 'filter=ansible_fqdn'|awk -F '["]' '/fqdn/{print $4}'

web01

command模块

command模块

功能:远程执行简单的命令

默认模块(可省略不指定)

作用:执行系统命令(linux windows),不支持变量,"<",">","|",";","&"等符号

[root@m01 ~]# ansible web_group -a 'ls'

[root@m01 ~]# ansible web_group -a 'df -h'

cron模块

cron模块

功能:添加定时任务

#使用crontab之前最好同步时间(注意单引号和双引号的作用)(*有时候需要加'')

[root@m01 ~]# ansible '*' -m cron -a "name=同步时间 minute=*/5 job='/usr/sbin/ntpdate time1.aliyun.com &>/dev/null'"

ansible-doc cron

ansible db -m cron -a 'minute="" hour="" day="" month="" weekday="" job="" name="(必须填写)" state='

1、定时设置指定值的写入即可,没有设置的要删除

2、name必须写(创建定时任务或者删除定时任务的 标志,不能修改注释)(这一点和yum仓库名一样)

3、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="*/5" job="/bin/echo hello" name="test cron job" state="present"'

#移除定时任务(根据name来删除)

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=yes"

ansible yml语法(playbook)

注意:yml语法很严格,不能多,漏空格,不能使用TAB

在不同的主机上,创建以主机名命名的目录

[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模块

copy模块

拷贝, 修改文件或目录, 修改已经存在的目录的权限

使用content可以直接在远端创建文件,同时指定文件内容

使用backup=yes备份配置文件,可以实现配置文件的修改或回滚

该模块可以直接拷贝链接

[root@m01 ~]# ansible-doc copy #查看copy模块用法

src: #源文件或目录,要复制到远程主机的文件在本地的地址,可以是绝对路径,也可以是相对路径。如果路径是一个目录,它将递归复制。在这种情况下,如果路径使用"/"来结尾,则只复制目录里的内容,如果没有使用"/"来结尾,则包含目录在内的整个内容全部复制,类似于rsync(#)。(src为空目录是时候是不会拷贝的,一致显示绿色)

content:用于替代"src",可以在命令行直接设定指定文件的值

dest: #目标目录

owner: foo #属主

group: foo #属组(不能使用gid指定)

mode: '0644' #权限,数字(3位|4位),字母,UGO

backup: yes #当出现同名的文件,直接覆盖,因为默认是no,指定yes后(文件内容不同会以当前时间戳备份该文件)

force:如果目标主机包含该文件,但内容不同,如果设置为yes,则强制覆盖,如果为no,则只有当目标主机的目标位置不存在该文件时,才复制。默认为yes(force=no和state=backup可以二选一)

remote_src:

yes #受控端自己玩(可以做批量回滚)

no #默认

--------------------------------------------------

#远程批量拷贝,相当于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的时候可以不写(目的地文件1.同名 2.内容不同 才备份),当dest不存在时,源文件改名

[root@m01 /]# ansible 'web_group' -m copy -a 'src=/root/hosts dest=/root owner=www group=www mode=0644 backup=yes'

----------------------------------------------------------------

#简单的文件直接可以直接使用content在命令行copy,不需要copy文件(记得在末尾加\n)

[root@m01 ~]# ansible 'nfs_group' -m copy -a 'content="/data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)\n" dest=/etc/exports'

--------------------------------------------------------------------------

#远端的源拷贝到远端指定的位置

[root@m01 ~]# ansible 'web_group' -m copy -a 'src=/etc/passwd dest=/tmp remote_src=yes'

#配置文件回滚

[root@m01 ~]# ansible web01 -m copy -a 'src=/root/passwd.1811.2020-06-09@18:16:13~ dest=/root/passwd remote_src=yes'

file模块

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:定义文件/目录的权限( 1.一次创建多个目录的时候才会递归授权, 2.recurse=yes也会递归授权)

4 owner:定义文件/目录的属主

5 path:必选项,定义文件/目录的路径(不存在的话可以直接创建)#

6 recurse:'递归'的设置文件的属性,只对'目录'有效(指定recurce的话,可以修改已经存在的文件或者目录的权限,默认关闭)(需要配合mode owner group一起使用)

7 src:要被链接的源文件的路径,只应用于state=link的情况

8 dest:被链接到的路径,只应用于state=link的情况

9 state:

directory:如果目录不存在,创建目录

file:只能修改文件或目录的时间 (配合modification_time access_time)

link:创建软链接

hard:创建硬链接

touch:如果文件不存在,则会创建一个新的文件,如果文件或目录已存在,则更新其最后修改时间 (不能在不存在的目录下创建文件,这一点和touch命令一样)

absent:删除目录、文件或者取消链接文件

#远程批量创建目录

不指定属主属组,默认是root(当前用户),不指定mode,那么权限为默认

[root@lb01 /code]# ansible 'web_group' -m file -a 'path=/backup state=directory owner=adm group=adm mode=0000'

#创建目录,同时递归授权(不能针对文件)

[root@m01 ~]# ansible 'web01' -m file -a 'path=/1/2/3 state=directory mode=000 recurse=yes'

#当state=touch mode=000同时指定,创建的都是目录,只指定touch才会创建文件 3

[root@m01 ~]# ansible web01 -m file -a 'path=/1/2/3 state=touch mode=000'

#远程批量创建文件(上级目录必须存在)

[root@lb01 /code]# ansible 'web_group' -m file -a 'path=/backup state=touch owner=adm group=adm mode=0000'

--------------------------------------------------------------------

#远程批量删除目录或文件

[root@lb01 ~]# ansible 'web_group' -m file -a 'path=/backup state=absent'

-----------------------------------------------------------------------

#远程创建软链接,(不指定权限的话,属主属组为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'

#远程创建硬链接(可通过ll -i 查看inode号,判断是否为硬链接关系)

[root@lb01 /code]# ansible 'web_group' -m file -a 'src=/backup/a.txt dest=/b.txt state=hard owner=adm group=adm mode=0000'

yum模块

yum模块

功能:远程下载

name #包名,用等于号表示(指定要安装的软件包的名称)

file:// #指定本地安装路径,=yum localinstall -y

http:// #指定yum仓库

state #指定动作,用等于号表示

present #安装软件包(默认)(=install)

absent #删除软件包(=remove)

latest #安装最新版本的软件包或,升级

disable_gpg_check #默认值为 no,表示不禁用验证,设置为 yes 表示禁用验证,即不验证包,直接安装。

enablerepo #临时启用源(无论此源是否开启)

disablerepo #临时禁用某个源,这样设置后,在安装软件包时则不会从对应的源中选择安装包。

download_only=true #只下载不安装 yum install [d]

-------------------------------------------------------------

#下载安装,使用被控端的源安装软件包

[root@m01 ~]# ansible 'web_group' -m yum -a 'name=vsftpd'

[root@m01 ~]# ansible 'web_group' -m yum -a 'name=vsftpd state=present'

#下载安装,指定源的rpm包,相当于wget+localinstall,可以指定包名和协议

[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"

#升级所有软件包,排除httpd

[root@Ansible ~]# ansible all -m yum -a "state=latest name='*' exclude='httpd'"

shell模块

shell模块

功能:执行复杂的命令

尤其是用到复杂命令时(如带管道符等等),但是shell模块不能做mysql的主从复制,但是nginx编译安装的时候只能使用shell模块(一般不用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

script模块

功能:作用:将本地脚本复制到远程主机,并执行。(不需要给脚本添加执行权限)

#编辑脚本(只需要放在m01就好)

vim /root/dir.sh

mkdir syy

#执行

absible 'web01' -m script -a '/root/dir.sh'

synchronize模块

功能:基于rsync命令工具同步目录和文件

由于synchronize模块会调用rsync命令,因此首先要记得提前安装好rsync软件包

1 archive: 归档,相当于同时开启recursive(递归)、links、perms、times、owner、group、-D选项都为yes ,默认该项为开启(#保证源文件和目标文件属性一致)

2 checksum: 是否检测sum值,默认关闭

3 compress:是否开启压缩(默认开启)

4 copy_links:同步的时候是否复制链接,默认为no ,注意后面还有一个links参数

links:同步链接文件

5 delete: 删除不存在的文件,delete=yes 使两边的内容一样(即以推送方为主),默认no

6 dest:目录路径(绝对路径或者相对路径)

7 dest_port:目标主机上的端口 ,默认是22,走的ssh协议

8 dirs:传送目录不进行递归,默认为no,即进行目录递归(#一般不用指定)

9 rsync_opts:通过传递数组来指定其他rsync选项。 #

--exclude=*.txt #排除

10 set_remote_user:主要用于ansible默认使用的用户与rsync使用的'用户不同的情况

11 mode: push(默认)或pull 模块,push模的话,'一般用于从本机向远程主机上传文件,pull 模式用于从远程主机上取文件'

12 src: 要同步到目的地的源主机上的路径; 路径可以是绝对的或相对的。如果路径使用”/”来结尾,则只复制目录里的内容,如果没有使用”/”来结尾,则包含目录在内的整个内容全部复制

ansible 172.25.70.2 -m synchronize -a ' '

#同步目录(前提是远程服务器上有rsync这个命令)

src=some/relative/path dest=/some/absolute/path rsync_path="sudo rsync"

#排除(#由于这个是rsync命令的参数,所以必须和rsync_opts一起使用)

src=/tmp/helloworld dest=/var/www/helloword rsync_opts=--exclude=.log

mount模块

功能:挂载

mount模块控制/etc/fstab中的活动和配置挂载点

文件系统的类型:C7(xfs),C6(ext4), C5(ext3)

1 fstype:必选项,挂载文件的类型 (nfs文件共享 fstype=nfs)

2 name:必选项,'哪个文件挂载' (可以使用path指定)

3 opts:传递给mount命令的参数(rw,ro)

4 src:必选项,'挂载到哪的目录' (nfs)

5 state:必选项

present:把指定的挂载写入到fastab中,不会立即挂载,(绿色)

mounted :即写入文件,又直接挂载(自动创建挂载点并挂载之)(#推荐)

absent:删除挂载点 (会清理fastab中的开机挂载)(#推荐)

unmounted:卸载(不会清理fastab中的开机挂载)

--------------------------------------------------------------------

1.#服务端和客户端安装nfs

[root@m01 ~]# ansible web_group -a 'yum -y install nfs-utils'

[root@m01 ~]# ansible nfs_group -a 'yum -y install nfs-utils'

2.#编辑nfs配置文件

[root@m01 ~]# ansible 'nfs_group' -m copy -a 'content="/data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)\n" dest=/etc/exports'

3.创建

[root@m01 ~]# ansible nfs_group -a 'mkdir /data'

[root@m01 ~]# ansible nfs_group -m systemd -a 'name=nfs state=started'

4.启动并设置开机自启

[root@m01 ~]# ansible web_group -m systemd -a 'name=nfs state=started'

[root@m01 ~]# ansible web_group -m systemd -a 'name=nfs enabled=yes'

[root@m01 ~]# ansible nfs_group -m systemd -a 'name=nfs enabled=yes'

5.挂载

[root@m01 ~]# ansible web01 -m mount -a ''

path=/mnt src=172.16.1.31:/data fstype=nfs state=mounted

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'

6.取消挂载

[root@m01 ~]# ansible web_group -m mount -a 'path=/mnt src=172.16.1.31:/data fstype=nfs state=unmounted'

7.清理/etc/fstab文件

#UUID就是磁盘的身份证号,不会重复(一般使用UUID的方式挂载)

[root@web ~]# blkid /dev/sda1 查看UUID(xfs文件系统)

/dev/sda1: UUID="12722517-ff26-416b-b535-073a95a7a00e" TYPE="xfs"

[root@web ~]# cat /etc/fstab

UUID=5bf16417-9cb6-40eb-80b9-8cc2f715f79f / (根目录的UUID) xfs defaults 0 0

UUID=12722517-ff26-416b-b535-073a95a7a00e /boot (/bbot目录的UUID) xfs defaults 0 0

#把本地的磁盘挂载到远程主机上

[root@Ansible ~]# ansible all -m mount -a "name=/mnt src=/dev/sda3 fstype=xfs state=mounted opts=rw"

#格式化磁盘:

ansible all -m filesystem -a "fstype=ext4 dev=/dev/sdb1"

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并下载(小心等于号,阿里云不使用md5sum校验,)

[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'

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: #指定仓库文件名(优先级高),不指定的话仓库文件名和仓库名一样,不能同时在同一个仓库文件内添加多个仓库名不同的仓库,同时指定的话file=仓库文件名,name=仓库名

gpgcheck #是否开启校验

yes

no

enabled #是否启用yum仓库

yes

no

state

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,gpg,enabled

[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脚本,可以实现更精确的操作

ansible服务模块--systemd模块

功能:远程启停,重载服务,

[root@m01 ~]# ansible-doc systemd

systemd: #模块

state: started #启动

name: httpd #指定服务名称

state: stopped #停止

state: restarted #重启

state: reloaded #重载配置文件

enabled=yes #设置开机自启

daemon_reload # 读取配置文件,每次修改了文件,最好都运行一次,确保应用了(systemd)

masked: #是否将服务设置为masked状态,被mask的服务是无法启动的(systemd),(yes|no)默认为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@Ansible ~]# ansible web -m systemd -a "name=httpd state=stopped"

#设置masked

[root@Ansible ~]# ansible web -m systemd -a "name=httpd masked=yes"

# 服务已无法启动

[root@Ansible ~]# ansible web -m systemd -a "name=httpd state=started"

# 撤销mask

[root@Ansible ~]# ansible web -m systemd -a "name=httpd masked=no"

# 可以启动成功

[root@Ansible ~]# ansible web -m systemd -a "name=httpd state=started"

service模块

service模块

远程启停,重载服务

service模块(可以跨平台,跨系统) 与systemd模块作用类似,使用命令类似

1 arguments:给命令行提供一些选项 #

2 enabled:是否开机启动 yes|no(默认是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'

ansible用户组管理模块

group模块

功能:创建用户组

[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'

ansible用户管理模块

user模块

使用yml语法修改用户密码

多种加密方式获取加密密码

功能:管理远程主机上的用户,比如创建用户、修改用户、删除用户、为用户创建密钥对等操作

user模块是请求的是useradd, userdel, usermod三个指令,goup模块请求的是groupadd, groupdel, groupmod 三个指令。

groups: #修改附加组为

append=no #默认,覆盖(相当于usermod -G)

append=yes #追加(相当于usermod -aG)

uid: 指定用的uid

group: #指定主组

password: #明文密码进行哈希后的字符串,你可以在 python 的命令提示符下输入如下命令,生成明文密码对应的加密字符串 "import crypt; crypt.crypt('666666')"

update_password:

always: 只有当密码不相同时才会生效,即修改密码(默认) #

on_create: 只为新用户设置密码

name: 指定用户名

system: 是否为系统用户 yes|no(默认是no)

remove: 当state=absent时,remove=yes则表示连同家目录,邮件目录一起删除,等价于userdel -r(默认是no)

state:

absent #删除用户

remove=no #默认,删除用户时,不会删除用户的家目录等信息

remove=yes #删除用户的同时,会删除用户的家目录,邮件目录

present #创建用户(默认)

shell: 指定用户的shell环境(默认是/bin/bash)

expires: 设置用户的过期时间,值是一个时间戳(转化命令:date -d 2018-12-31 +%s,命令行设置 expires=)

comment: #创建用户的时候添加一段注释

generate_ssh_key: yes #创建公钥(默认)

ssh_key_bits: 2048 #指定公钥长度

ssh_key_file: .ssh/id_rsa #创建私钥(创建用户的家目录下)

create_home=false #是否创建家目录(默认创建true)

------------------------------------------------------------------------

#创建用户,修改用户

ansible 'web_group' -m user -a ' '

name=ll

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 expires=1422403387

name=hh generate_ssh_key=yes ssh_key_bits=2048 ssh_key_file=.ssh/id_rsa

name="testops" password="$6$0lwTSmqKOkL.ktgl$OnBexXC7haBf0FRHVMIZM2edDeFWBbpKJ2r9cxVwNvY.vh3IIUzwFz8n7jFglc0CrtQSY12ziDonVL6e71Og2."

--------------------------------------------------------------

#生成密钥时,只会生成公钥文件和私钥文件,和直接使用ssh-keygen指令效果相同,不会生成authorized_keys文件。复制id_rsa.pub粘贴为authorized_keys文件即可使用

name=test generate_ssh_key=yes ssh_key_bits=2048 ssh_key_file=~/.ssh/id_rsa

-----------------------------------------------------------------------

#指定password参数时,不能使用后面这一串密码会被直接传送到被管理主机的/etc/shadow文件中,所以需要先将密码字符串进行加密处理。然后将得到的字符串放到password中即可。

1.加密

[root@ansible-manager ~]# python

>>> import crypt;crypt.crypt('666666')

'$6$ziT/sb5KRtUaxoq7$ulfHVLqVgXfmfFUYY7FppzqBQMUYd.2GLDyQwmKv4dYAd0zpgtt5JDheoO/OvvTvY53x9UShX.PtHykJEvsmG0'

2.创建用户指定密码 或 修改用户密码

------------------------------------------------------------------

#删除用户

注意该用户下不能有任何进程,否则会报错(但是能删除的都删除了)

ansible 'web_group' -m user -a ' '

name=ll state=absent remove=yes

fetch

fetch模块

[root@m01 ~]# ansible web01 -m fetch -a "src=/root/lol.txt dest=/root"

压缩解压unarchive模块

官方文档

功能:解压缩

1、解压ansible管理机上的压缩文件到远程主机:

ansible all -m unarchive -a "src=/tmp/install/zabbix-3.0.4.tar.gz dest=/tmp/ mode=0755 copy=yes"

2、解压远程主机上的文件到目录:

ansible all -m unarchive -a "src=/tmp/install/zabbix-3.0.4.tar.gz dest=/tmp/ mode=0755 copy=no"

copy:默认为yes,当copy=yes,那么拷贝的文件是从ansible主机复制到远程主机上的,如果设置为copy=no,那么会在远程主机上寻找src源文件

src:源路径,可以是ansible主机上的路径,也可以是远程主机上的路径,如果是远程主机上的路径,则需要设置copy=no

dest:远程主机上的目标路径

selinux

功能:管理selinux

C6想要使用这种方式关闭selinux的话,需要安装 libselinux-python(改过要重启生效)

不能使用systemd service模块来管理selinux systemd

state:

enforcing #永久开启

permissive #临时开启(默认)

disabled #禁用

#修改配置文件关闭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

如果修改了/etc/selinux/config文件的话,需要重启,如果你只是临时修改的话,不需要重启

使用setenforce 可以临时改变selinux的状态,不需要重启系统。但是系统重启后,配置失效,因为系统重启后,是根据/var/selinux/config的配置内容进行功能设置的

firewalld

功能:管理firewalld

打开才能关闭

service #指定开放或关闭的服务名称(http https)

port #指定开放或关闭的端口(-)

permanent #是否添加永久生效(permanent=no即为临时生效) #

immediate #临时生效

state

enabled #永久开启

disabled #永久关闭

zone #指定配置某个区域

rich_rule #配置辅规则

masquerade #开启地址伪装

source #指定来源IP

#service指定服务(需要提前开启防火墙)

[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=https permanent=yes state=enabled" -i ./hosts

#service指定端口(某些服务系统自动开启)

[root@m01 ~]# ansible web_group -m firewalld -a "port=8080-8090/tcp permanent=yes state=enabled" -i ./hosts

lineinfile

功能:lineinfile模块用于确保一个特定的行在一个文件中,或使用一个正则表达式替换现有的行

如果想要改变文件中相似的多行,可以使用replace模块。如果想要插入、更新、删除一个行块,可以使用blockinfile模块

(1)文本替换

将/etc/selinux/config文件中所有匹配^SELINUX=正则表达式的行中的最后一行使用SELINUX=disabled替换

如果regexp不匹配文件中的任何一行,则将line所指定的行插入到文件的末尾

[root@Ansible ~]# ansible web -m lineinfile -a "path=/etc/selinux/config regexp='^SELINUX=' line='SELINUX=disabled'"

(2)删除行

将/tmp/test.sh文件中所有匹配^pwd的行删除

[root@Ansible ~]# ansible web -m lineinfile -a "path=/tmp/test.sh regexp='^pwd' state=absent"

(3)替换行并设置文件权限

[root@Ansible ~]# ansible web -m lineinfile -a "path=/etc/hosts regexp='^127.0.0.1' line='127.0.0.1 localhost' owner=root group=root mode=0644"

stat模块

功能:stat模块获取远程文件状态信息,包括atime、ctime、mtime、md5、uid、gid等

(1)显示文件的所有信息

[root@Ansible ~]# ansible web -m stat -a "path=/etc/sysctl.conf"

(2)显示MD5值

[root@Ansible ~]# ansible web -m stat -a "path=/etc/sysctl.conf get_md5=yes"

mysql_user模块

mysql_user

功能:主控端对被控端mysql服务器 添加删除用户,授权远程用户登录,访问

mysql_user模块参数

login_host=“localhost” 指定本地root用户登录本机mysql

login_password=“123.com” root用户的登录密码 #登录密码

login_user=“root” 为root用户或者mysql用户 #登录用户

login_port=“3306” 数据库端口号

name="" 指定grant授权用户 #建立使用者的名字或是已存在的使用者

password="" grant授权用户密码 #给新用户设置密码,或者修改密码

priv="" 库名.SQL语句权限,GRANT #资料库.资料表:权限1,权限2(要用")

host="" 授权远程登录的IP地址,一般为 网段.% 或者直接 %

state=“present” 创建授权用户

state=“absent” 删除授权用户

ahdoc模式写法创建授权用户,验证模块正确时使用

[root@localhost ansible]# ansible mysql -m mysql_user -a "login_host=% login_password=123.com login_user=root login_port=3306 name=ty_user password=1 priv=".:ALL,GRANT" host='%' state=present"

playbook剧本写法创建授权用户,执行自动化部署时使用

- hosts: mysql_group

remote_user: root

tasks:

- name: grant mysql user

mysql_user:

login_host: "localhost"

login_user: "root"

login_password: "123.com"

login_port: "3306"

name: "ty"

password: "123.com"

host: "%"

priv: "*.*:ALL,GRANT"

state: "present"

mysql_db模块

mysql_db

功能:用于建立、删除、导入和导出数据库

#建立数据库

hosts: mysql_group

tasks:

- name: create a database

mysql_db:

login_host: "127.0.0.1"

login_user: "root"

login_password: "mysql@123"

login_port: "3306"

name: "mezz"

encoding: "utf8"

state: "present"

#删除数据库

hosts: mysql_group

tasks:

- name: delete a database

mysql_db:

login_host: "127.0.0.1"

login_user: "root"

login_password: "mysql@123"

login_port: "3306"

name: "mezz"

state: "absent"

#导出数据库

hosts: mysql_group

tasks:

- name: dump a database

mysql_db:

login_host: "127.0.0.1"

login_user: "root"

login_password: "mysql@123"

login_port: "3306"

name: "mezz"

target: "/tmp/mezz.gz"

state: "dump"

#导入数据库

hosts: mysql_group

tasks:

- name: import a database

mysql_db:

login_host: "127.0.0.1"

login_user: "root"

login_password: "mysql@123"

login_port: "3306"

name: "mezz"

target: "/tmp/mezz.gz"

state: "import"

template模块

该模块只能在playbook中运行,不能使用命令行

功能:该模块和copy模块作用基本一样,都是把某个文件复制到远端主机上,但是区别在于template模块可以获取变量的值,而copy则是原封不动的把文件内容复制过去

改变量的调用依赖于facts,关闭fascs的话,该模块作用将于copy模块相同

问题:由于每台服务器cpu数量不一样,没办法来灵活配置Nginx最大进程数

– backup: 如果原目标文件存在,则先备份目标文件

– src:在ansible控制器上的Jinja2格式化模板的路径。 这可以是相对或绝对的路径。

– dest:将模板渲染到远程机器上的位置。

force:是否强制覆盖,默认为yes

– owner:目标文件属主

– group:目标文件属组

– mode:目标文件的权限模式

# Example from Ansible Playbooks

- template:

src: /mytemplates/foo.j2

dest: /etc/file.conf

owner: bin

group: wheel

mode: 0644

pam_limits模块

功能:修改文件描述符

#为用户joe添加或修改nofile软限制

- pam_limits:

domain: joe

limit_type: soft

limit_item: nofile

value: 64000

# 为用户smith添加或修改硬限制。保持或设置最大值。

- pam_limits:

domain: smith

limit_type: hard

limit_item: fsize

value: 1000000

use_max: yes

#为用户james添加或修改memlock,包括软硬限制和注释。

- pam_limits:

domain: james

limit_type: '-'

limit_item: memlock

value: unlimited

comment: unlimited memory lock for james

相关推荐

TFT显示屏的接口连接方式
365bet网投官网

TFT显示屏的接口连接方式

📅 12-02 👁️ 8525
电池测评:松下VS南孚,谁才是你的“电量充沛”之选?
如何下載與安裝 Dreamweaver
365bet网投官网

如何下載與安裝 Dreamweaver

📅 12-06 👁️ 4120
淘宝如何添加旺旺子账号
365买球平台下载苹果

淘宝如何添加旺旺子账号

📅 10-01 👁️ 3733
英雄联盟符文系统全指南,一文搞懂符文在哪找、怎么配
365买球平台下载苹果

英雄联盟符文系统全指南,一文搞懂符文在哪找、怎么配

📅 06-27 👁️ 180
野战蓝鲫和蓝鲫x5有什么区别?优缺点是什么?如何选择?
365买球平台下载苹果

野战蓝鲫和蓝鲫x5有什么区别?优缺点是什么?如何选择?

📅 06-22 👁️ 3336