ansible常用的一些模块

ansible简介绍

1.ansible是什么?

      l  ansible是一个基于Python开发的自动化运维工具 !

      l  其功能实现基于SSH远程连接服务!

      l  ansible可以实现批量系统配置、批量软件部署、批量文件拷贝、批量运行命令等功能

2.ansible软件特性

      l  不需要单独安装客户端(no agents),基于系统自带的sshd服务,sshd就相当于ansible的客户端。

      l  不需要服务端(no servers)。

      l  需要依靠大量的模块实现批量管理。? 

      l  配置文件/etc/ansible/ansible.cfg,不用配置

3.ansible软件执行结果

     l  输出内容显示绿色:表示执行成功,当没有任何改变

     l  输出内容显示黄色:表示执行成功,蛋对被管理主机进行了改变

     l  输出内容显示红色:表示执行失败!!!

4.ansible的官方文档

http://docs.ansible.com/

ansible的部署:

1.因为ansible基于ssh所以需要密钥对可参考http://www.cnblogs.com/ExzaiTin/p/7687410.html

2.被管理端需要安装libselinux-python

yum install libselinux-python -y

3.管理端安装ansible软件配置ansible的hosts文件(管理机和被管理机可以基于ssh免密钥,也可以在ansible的hosts文件中写入被管理机的密码)配置格式如下(仅供参考,可以不写密码和用户,[]里表示自己定义的主机组,如果域名可以解析的话ip也可以写为域名)

4.ansible命令格式

ansible <host-pattern> [-m module_name] [-a args] [options]
<host-pattern> 自己定义的主机组
[-m module_name] 指定模块
[-a args] 指定要执行的动作

ansible常用模块:
1.ping-测试模块
ansible test -m ping
172.16.1.8 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
##以上为可以正常链接字体会显示为绿色
172.16.1.41 | UNREACHABLE! => {
    "changed": false, 
    "msg": "Failed to connect to the host via ssh: ssh: connect to host 172.16.1.41 port 22: Connection timed out
", 
    "unreachable": true
}
##以上为无法链接字体会显示为红色

2.setup-远程查看主机的配置信息

ansible test -m setup

3.command-远程执行命令

4.shell-远程节点执行模块

[root@m01 ~]# ansible test -m shell -a "date"
172.16.1.222 | SUCCESS | rc=0 >>
Sat Oct 21 12:00:38 CST 2017

5.script-在远程执行本地的脚本

[root@m01 ~]# ansible test -m script -a "/sh/test.sh"
172.16.1.222 | SUCCESS => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to 172.16.1.222 closed.
", 
    "stdout": "hello word
", 
    "stdout_lines": [
        "hello word"
    ]
}

6.copy-复制模块

##管理主机数据--远程主机
[root@m01 ~]# ansible test -m copy -a "src=/sh/test.sh dest=/server/scripts mode=0755"
172.16.1.222 | SUCCESS => {
    "changed": true, 
    "checksum": "d4dfddcc1b1a2b92599a6b498b1dbc0b3dee9128", 
    "dest": "/server/scripts/test.sh", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "e87e71d1c410c5863aadc5b9a3fe209b", 
    "mode": "0755", 
    "owner": "root", 
    "size": 30, 
    "src": "/root/.ansible/tmp/ansible-tmp-1508560437.19-268270019190063/source", 
    "state": "file", 
    "uid": 0
}
##远程主机数据进行复制
[root@m01 ~]# ansible test -m copy -a "remote_src=true src=/server/scripts/test.sh dest=/tmp/"
172.16.1.222 | SUCCESS => {
    "changed": true, 
    "checksum": "d4dfddcc1b1a2b92599a6b498b1dbc0b3dee9128", 
    "dest": "/tmp/test.sh", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "e87e71d1c410c5863aadc5b9a3fe209b", 
    "mode": "0644", 
    "owner": "root", 
    "size": 30, 
    "src": "/server/scripts/test.sh", 
    "state": "file", 
    "uid": 0
}
##复制到目标后修改名称
[root@m01 ~]# ansible test -m copy -a "src=/etc/hosts dest=/tmp/hello.txt"
172.16.1.222 | SUCCESS => {
    "changed": true, 
    "checksum": "545f587595b5c60d66243fca48e052ed34eed782", 
    "dest": "/tmp/hello.txt", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "fe08440ffebed54cab7a9b4cb3c3beda", 
    "mode": "0644", 
    "owner": "root", 
    "size": 371, 
    "src": "/root/.ansible/tmp/ansible-tmp-1508560939.57-196389366869591/source", 
    "state": "file", 
    "uid": 0
}
##复制时会创建目录,包括多级目录
[root@m01 ~]# ansible test -m copy -a "src=/etc/hosts dest=/tmp/a/b/c/" 
172.16.1.222 | SUCCESS => {
    "changed": true, 
    "checksum": "545f587595b5c60d66243fca48e052ed34eed782", 
    "dest": "/tmp/a/b/c/hosts", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "fe08440ffebed54cab7a9b4cb3c3beda", 
    "mode": "0644", 
    "owner": "root", 
    "size": 371, 
    "src": "/root/.ansible/tmp/ansible-tmp-1508561086.8-245628104704955/source", 
    "state": "file", 
    "uid": 0
}

7.file-设置文件属性

创建目录(支持创建多级目录)
[root@m01 ~]# ansible test -m file -a "dest=/tmp/dir state=directory"
172.16.1.222 | SUCCESS => {
    "changed": true, 
    "gid": 0, 
    "group": "root", 
    "mode": "0755", 
    "owner": "root", 
    "path": "/tmp/dir", 
    "size": 4096, 
    "state": "directory", 
    "uid": 0
}
创建文件
[root@m01 ~]# ansible test -m file -a "dest=/tmp/hello state=touch"
172.16.1.222 | SUCCESS => {
    "changed": true, 
    "dest": "/tmp/hello", 
    "gid": 0, 
    "group": "root", 
    "mode": "0644", 
    "owner": "root", 
    "size": 0, 
    "state": "file", 
    "uid": 0
}
创建软连接
[root@m01 ~]# ansible test -m file -a "src=/tmp/test.sh dest=/root/test.link state=link "
172.16.1.222 | SUCCESS => {
    "changed": true, 
    "dest": "/root/test.link", 
    "gid": 0, 
    "group": "root", 
    "mode": "0777", 
    "owner": "root", 
    "size": 12, 
    "src": "/tmp/test.sh", 
    "state": "link", 
    "uid": 0
}
删除文件目录
[root@m01 ~]# ansible test -m file -a "dest=/root/test.link state=absent"
172.16.1.222 | SUCCESS => {
    "changed": true, 
    "path": "/root/test.link", 
    "state": "absent"
}

8.fetch-远程获取

##从远程主机拉取文件
[root@m01 ~]# ansible test -m fetch -a "src=/tmp/test.sh dest=/tmp"
172.16.1.222 | SUCCESS => {
    "changed": true, 
    "checksum": "d4dfddcc1b1a2b92599a6b498b1dbc0b3dee9128", 
    "dest": "/tmp/172.16.1.222/tmp/test.sh", 
    "md5sum": "e87e71d1c410c5863aadc5b9a3fe209b", 
    "remote_checksum": "d4dfddcc1b1a2b92599a6b498b1dbc0b3dee9128", 
    "remote_md5sum": null
}
[root@m01 ~]# tree /tmp
/tmp
└── 172.16.1.222
    └── tmp
        └── test.sh

2 directories, 1 file
##拉取时仅拉取目标文件或目录
[root@m01 tmp]# ansible test -m fetch -a "src=/tmp/test.sh dest=/tmp/ flat=yes"
172.16.1.222 | SUCCESS => {
    "changed": true, 
    "checksum": "d4dfddcc1b1a2b92599a6b498b1dbc0b3dee9128", 
    "dest": "/tmp/test.sh", 
    "md5sum": "e87e71d1c410c5863aadc5b9a3fe209b", 
    "remote_checksum": "d4dfddcc1b1a2b92599a6b498b1dbc0b3dee9128", 
    "remote_md5sum": null
}
[root@m01 tmp]# tree /tmp
/tmp
└── test.sh

0 directories, 1 file

9.mount-挂载相关模块

 

10.cron-定时任务模块

11.yum-软件安装模块

12.service-服务启动或管理模块

13.filesystem-文件系统模块

14.user-用户管理

15.synchronize-rsync相关

原文地址:https://www.cnblogs.com/ExzaiTin/p/7918415.html