ansible基本模块

 

ansible-doc  -l    #列出所有模块

ansible-doc shell                    # 查看shell模块的帮助

 

command(命令模块,默认)

[root@L yum.repos.d]# ansible all -m command -a 'ls /root'

 

shell (最常用)

 

[root@ansible .ssh]# ansible cache -m shell -a 'chdir=/tmp touch f1'                       # 切换到/tmp下创建文件

 

 

lineinfile(类似于sed)

 

[root@ansible .ssh]# ansible all -m lineinfile -a 'path=/etc/resolv.conf regexp="^search:" line="search tedu.cn"'


# path : 要修改的行
# regexp : 正则,匹配要修改的行
# line : 修改后的样子

 

 

replace

[root@ansible .ssh]# ansible all -m replace -a 'path=/etc/resolv.conf regexp="\.cn" replace=".com"'

# regexp: 匹配要修改的部分
# replace : 修改后的样子


[root@ansible .ssh]# ansible all -m replace -a 'path=/etc/resolv.conf regexp="^(search).*" replace="\1 .com"' 

 

script(执行脚本)

cron

hostname

user

group

copy:从服务器复制文件到客户端

[root@L shells]# ansible all -m copy -a "src=heart.sh dest=/root"
[root@L shells]# ansible all -m copy -a "content='i am ansible\n' dest='/root/test'"         # 将content写入文件,文件存在覆盖,不存在则新建

fetch:将客户机的文件拷贝到本机器

[root@L ~]# ansible all -m fetch  -a 'src=/var/log/messages dest=/mnt'

file:指定文件属性

[root@L ~]# ansible all -m file -a 'path=/mnt/test state=touch'             # 创建文档   state=absent 删除
[root@L ~]# ansible all -m file -a 'path=/mnt/dir state=directory'          # 创建目录
[root@L ~]# ansible 192.168.2.100 -m file -a 'src=/usr/local/tomcat/bin/startup.sh dest=/sbin/cat_start state=link'    # 创建软链接

 

yum:

[root@L ~]# ansible all -m yum -a 'name=vsftpd,httpd'                             # 为所有客户机安装软件   state=remove 卸载    update_cache=yes 更新缓存
[root@L ~]# ansible 192.168.2.100 -m yum -a 'list=installed'                # 查看客户机安装的软件列表    

 

[root@ansible .ssh]# ansible db -m replace -a 'path=/etc/my.cnf regexp="^(\[mysqld\])" replace="\1\nlog-bin"'

 

 

 

service:

[root@L shells]# ansible all -m service -a 'name=mariadb state=restarted'
[root@L shells]# ansible all -m service -a 'name=mariadb state=started enabled=yes'

 

unarchive模块

功能:解压缩,这个模块有两种用法:

1、将ansible主机上的压缩包在本地解压缩后传到远程主机上,这种情况下,copy=yes.   本地解压缩,解压缩位置不是默认的目录,没找到或传完删了      后传到远程主机

2、将远程主机上的某个压缩包解压缩到指定路径下。这种情况下,需要设置copy=no     远程主机上面的操作,不涉及ansible服务端

 

参数:

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

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

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

mode:设置解压缩后的文件权限

posted @ 2018-12-24 16:54  Ray_chen  阅读(242)  评论(0编辑  收藏  举报