ansible常用模块介绍

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

file模块


file模块包含如下选项:

    force:需要在两种情况下强制创建软链接,一种是源文件不存在但之后会建立的情况下;另一种是目标软链接已存在,需要先取消之前的软链,然后创建新的软链,有两个选项:yes|no

    group:定义文件/目录的属组

    mode:定义文件/目录的权限

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

    path:必选项,定义文件/目录的路径

    recurse:递归的设置文件的属性,只对目录有效

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

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

    state:

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

            file:即使文件不存在,也不会被创建

            link:创建软链接

            hard:创建硬链接

            touch:如果文件不存在,则会创建一个新的文件,如果文件或目录已存在,则更新其最后修改时间

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


1.创建file1文件

[root@xuegod100 opt]# ansible -i hosts tomcatserver -m file -a 'path=/tmp/file1 state=touch'

2.删除file1 文件

ansible常用语句

[root@xuegod100 opt]# ansible -i hosts tomcatserver -m file -a 'path=/tmp/file1 state=absent'

3.创建d1目录

[root@xuegod100 opt]# ansible -i hosts tomcatserver -m file -a 'path=/tmp/d1 state=directory owner=root group=root mode=777'


copy模块

copy模块包含如下选项:

    backup:在覆盖之前将原文件备份,备份文件包含时间信息。有两个选项:yes|no

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

    dest:必选项。要将源文件复制到的远程主机的绝对路径,如果源文件是一个目录,那么该路径也必须是个目录

    directory_mode:递归的设定目录的权限,默认为系统默认权限

    force:如果目标主机包含该文件,但内容不同,如果设置为yes,则强制覆盖,如果为no,则只有当目标主机的目标位置不存在该文件时,才复制。默认为yes

    others:所有的file模块里的选项都可以在这里使用

    src:要复制到远程主机的文件在本地的地址,可以是绝对路径,也可以是相对路径。如果路径是一个目录,它将递归复制。在这种情况下,如果路径使用"/"来结尾,则只复制目录里的内容,如果没有使用"/"来结尾,则包含目录在内的整个内容全部复制,类似于rsync。

1.拷贝yum.log文件到root下的file2

[root@xuegod100 opt]# ansible -i hosts tomcatserver -m  copy -a 'src=/tmp/yum.log dest=/root/file2 mode=755 owner=root group=root'

2.cretes模块执行的时候  会做一个判断 前面的参数存在   后面的命令才不会继续执行下去

[root@xuegod100 opt]# ansible -i hosts tomcatserver -a 'creates=/root/file2 ls /home'


command模块


command模块包含如下选项:

    creates:一个文件名,当该文件存在,则该命令不执行

    free_form:要执行的Linux指令

    chdir:在执行指令之前,先切换到该指定的目录

    removes:一个文件名,当该文件不存在,则该选项不执行

    executable:切换shell来执行指令,该执行路径必须是一个绝对路径


1.查看硬盘使用情况 里面可以使用一些命令

[root@xuegod100 opt]# ansible -i hosts tomcatserver -m command -a 'df -hT'

2.查看文件内容

[root@xuegod100 opt]# ansible -i hotst  tomcatserver -m command -a 'cat /var/www/helloword'



chdir模块


.chdir  进入到指定的目录然后进行操作

进入到tmp 用户下面将passwd 文件压缩成 bbb.tar 文件

[root@xuegod100 opt]# ansible -i hosts tomcatserver -a 'chdir=/tmp/ tar zcf bbb.tar.gz passwd'


shell 模块


1.shell 模块使用

与command模块使用相似 但是shell支持管道

[root@xuegod100 opt]# ansible -i hosts tomcatserver -m shell -a 'ps -ef | grep httpd'

2.raw模块 与上面shell 相似

[root@xuegod100 opt]# ansible -i hosts tomcatserver -m raw -a 'ps -ef | grep httpd'


server模块


该模块包含如下选项:


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


    enabled:是否开机启动  yes|no


    name:必选项,服务名称


    pattern:定义一个模式,如果通过status指令来查看服务的状态时,没有响应,就会通过ps指令在进程中根据该模式进行查找,如果匹配到,则认为该服务依然在运行


    runlevel:运行级别


    sleep:如果执行了restarted,在则stop和start之间沉睡几秒钟


    state:对当前服务执行启动,停止、重启、重新加载等操作(started,stopped,restarted,reloaded)

1.启动httpd服务 并且开机启动


[root@xuegod100 opt]# ansible -i hosts tomcatserver -m service  -a 'name=httpd state=started enabled=yes'

2.关闭httpd服务

[root@xuegod100 opt]# ansible -i hosts tomcatserver -m service  -a 'name=httpd state=stop'

3.重启httpd服务 并且沉睡3秒钟

[root@xuegod100 opt]# ansible -i hosts tomcatserver -m service  -a 'name=httpd state=restarted sleep=3'

4.重启network 只重启eth0 网卡

[root@xuegod100 opt]# ansible -i hosts tomcatserver -m service  -a 'name=network state=restarted args=eth0'


corn模块


包含如下选项:

    backup:对远程主机上的原任务计划内容修改之前做备份

    cron_file:如果指定该选项,则用该文件替换远程主机上的cron.d目录下的用户的任务计划

    day:日(1-31,*,*/2,……)

    hour:小时(0-23,*,*/2,……)

    minute:分钟(0-59,*,*/2,……)

    month:月(1-12,*,*/2,……)

    weekday:周(0-7,*,……)

    job:要执行的任务,依赖于state=present

    name:该任务的描述

    special_time:指定什么时候执行,参数:reboot,yearly,annually,monthly,weekly,daily,hourly

    state:确认该任务计划是创建还是删除

    user:以哪个用户的身份执行


1.root用户下 重启系统计划任务   每天的两点进行重启系统

[root@xuegod100 opt]# ansible -i hosts tomcatserver -m cron -a 'name="reboot system" hour=2 user=root job="/sbin/reboot"'

2.删除计划任务 使用state=sbsent

[root@xuegod100 opt]# ansible -i hosts tomcatserver -m cron -a 'name="reboot system" hour=2 user=root job="/sbin/reboot" state=absent'

3.每三分钟检查一下home下的文件

[root@xuegod100 opt]# ansible -i hosts tomcatserver -m command -a "crontab -l"

4.special_time指定什么时候执行

[root@xuegod100 opt]# ansible -i hosts tomcatserver -m command -a "crontab -l"

5.删除计划任务(name 指定计划任务名称)

[root@xuegod100 opt]# ansible -i hosts tomcatserver -m cron -a 'name="list dir"  state=absent'


yum 模块


选项:


    config_file:yum的配置文件


    disable_gpg_check:关闭gpg_check


    disablerepo:不启用某个源


    enablerepo:启用某个源


    list


    name:要进行操作的软件包的名字,也可以传递一个url或者一个本地的rpm包的路径


    state:状态(present,absent,latest)

1.安装mysql服务

[root@xuegod100 opt]# ansible -i hosts  tomcatserver  -m yum -a 'name=mysql state=installed'

2.卸载mysql服务

[root@xuegod100 opt]# ansible -i hosts  tomcatserver  -m yum -a 'name=mysql state=absent'


 user模块


home:指定用户的家目录,需要与createhome配合使用

groups:指定用户的属组

uid:指定用的uid

password:指定用户的密码

name:指定用户名

createhome:是否创建家目录 yes|no

system:是否为系统用户

remove:当state=absent时,remove=yes则表示连同家目录一起删除,等价于userdel -r

state:是创建还是删除

shell:指定用户的shell环境

1.创建用户

[root@xuegod100 opt]# ansible -i hosts  tomcatserver  -m  command -a 'useradd -s /sbin/nologine -M user1'

2.使用user模块创建模块

createhome  是否创建家目录 指定home 指定密码  指定用户 指定shell  状态创建

[root@xuegod100 opt]# ansible -i hosts  tomcatserver  -m  user -a 'createhome=yes home=/home/user2 password=123456 name=user2 shell=/bin/bash state=present'

3.删除用户

[root@xuegod100 opt]# ansible -i hosts  tomcatserver  -m  user -a 'remove=yes name=user2 state=absent'


synchronize模块(同步文件)

使用rsync同步文件

archive #镜像rsync存档标志,支持递归、链接、perms、times、owner、group flags和- d,选项都为yes 默认该项为开启。

owner、group、-D选项都为yes ,默认该项为开启

checksum: 跳过检测sum值,默认关闭

compress:是否开启压缩

copy_links:复制链接文件,默认为no ,注意后面还有一个links参数

delete: 删除不存在的文件,默认no

dest:目录路径

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

dirs:传速目录不进行递归,默认为no,即进行目录递归

rsync_opts:rsync参数部分

set_remote_user:主要用于/etc/ansible/hosts中定义或默认使用的用户与rsync使用的用户不同的情况

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

1.同步文件,将、tmp下的helloword文件复制到、var、www 的目录里面 src 指定原地址  dest 目标地址

[root@xuegod100 opt]# ansible -i hosts tomcatserver -m synchronize -a 'src=/tmp/helloword dest=/var/www/'

2.查看文件内容

[root@xuegod100 opt]# ansible -i hotst  tomcatserver -m command -a 'cat /var/www/helloword'

3.同步目录

同步/var/log/下面所有的日志    mode 指定模式 rsync_path 指定远程主机的sync命令 sync_opts 指定额外的rsync选项

[root@xuegod100 opt]# ansible -i hosts tomcatserver -m synchronize -a 'src=/var/log/ dest=/var/www/ mode=push  rsync_path=/usr/bin/rsync rsync_opts="-avz,--exclude=.git"'


filesystem模块


设置文件的属性

file模块包含如下选项:

    force:需要在两种情况下强制创建软链接,一种是源文件不存在但之后会建立的情况下;另一种是目标软链接已存在,需要先取消之前的软链,然后创建新的软链,有两个选项:yes|no

    group:定义文件/目录的属组

    mode:定义文件/目录的权限

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

    path:必选项,定义文件/目录的路径

    recurse:递归的设置文件的属性,只对目录有效

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

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

    state:

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

            file:即使文件不存在,也不会被创建

            link:创建软链接

            hard:创建硬链接

            touch:如果文件不存在,则会创建一个新的文件,如果文件或目录已存在,则更新其最后修改时间

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

在块设备上创建文件系统

1.创建一个4M 的文件系统

[root@xuegod100 opt]# ansible -i hosts tomcatserver  -a 'dd if=/dev/zero of=/disk.img bs=4k count=1024'

2.把disk.img作为loop设备从而可以进行分区格式化

[root@xuegod100 opt]# ansible -i hosts tomcatserver -m command -a 'losetup /dev/loop0 /disk.img'

3.格式化硬盘分区

-F额外的参数 对照mkfs /dev/disk.img -F 强制格式化

[root@xuegod100 opt]# ansible -i hosts tomcatserver -m filesystem -a 'dev=/disk.img fstype=ext4 opts=-F'


mount挂载模块

选项:

    dump

    fstype:必选项,挂载文件的类型

    name:必选项,挂载点

    opts:传递给mount命令的参数

    passno

    src:必选项,要挂载的文件

    state:必选项

            present:只处理fstab中的配置

            absent:删除挂载点

            mounted:自动创建挂载点并挂载之

            umounted:卸载

1.挂载设备/dev/loop0 到aaa  state=mounted 挂载并且创建挂载点

[root@xuegod100 opt]# ansible -i hosts tomcatserver -m mount -a 'fstype=ext4 name=/aaa src=/dev/loop0 state=mounted'

2.查看硬盘挂载情况

[root@xuegod100 opt]# ansible -i hosts tomcatserver -m command -a 'df -hT'

3.在挂载的设备上面创建文件测试

[root@xuegod100 opt]# ansible -i hosts tomcatserver -m file -a 'path=/aaa/test state=touch'



本文转自 Innocence_caosm 51CTO博客,原文链接:http://blog.51cto.com/innocence/1961205,如需转载请自行联系原作者
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
6月前
|
运维 Shell Linux
Ansible自动化运维工具之常用模块使用实战(5)
Ansible自动化运维工具之常用模块使用实战(5)
|
8月前
|
网络协议 网络安全
Ansible模块介绍——防火墙模块
Ansible模块介绍——防火墙模块
144 0
|
6月前
|
运维 Linux
Ansible自动化运维工具之常用模块使用实战(6)
Ansible自动化运维工具之常用模块使用实战(6)
|
9月前
|
Shell
ansible模块大全上【建议收藏】
ansible模块大全上【建议收藏】
104 0
ansible模块大全上【建议收藏】
|
5月前
|
网络安全 数据安全/隐私保护
ansible的get_url模块
ansible的get_url模块
|
5月前
|
存储 Linux Python
ansible手动添加模块
ansible手动添加模块
47 0
|
8月前
Ansible模块管理——磁盘管理模块、mount模块
Ansible模块管理——磁盘管理模块、mount模块
249 0
|
8月前
|
网络协议 网络安全 数据安全/隐私保护
Ansible模块介绍——配置网络模块、上传下载文件模块
Ansible模块介绍——配置网络模块、上传下载文件模块
266 0
|
8月前
|
应用服务中间件 nginx
Ansible模块——软件包管理模块
Ansible模块——软件包管理模块
|
8月前
|
数据安全/隐私保护 Python
Ansible模块介绍——用户管理模块
Ansible模块介绍——用户管理模块