centos6.5安装ansible及相关主要模块的简单使用

 yum install ansible -y
[root@mfsclient ~]# cd /etc/ansible/
[root@mfsclient ansible]# tree
.
├── ansible.cfg
├── hosts
└── roles
安装完后cd /etc/ansible后,默认有3个文件夹,其中ansible.cfg为ansible的配置文件,hosts为主机信息,roles可以看成是你分配给一台或多台主机的某些事情,我理解roles可以看成一个大包,大包里又有些小包,小包里包涵配置文件或者yaml,一句话概括,roles是将playbook分割为多个文件的主要手段。

常用模块:

copy模快,file模块,cron模块,group模块,user模块,yum模块,service模块,script模块,ping模块,command模块,raw模块,get_url模块,synchronize模块,下面举几个工作可能常用的例子。

copy模块:

[root@LVS_Master shell]# ansible all -m copy -a 'src=/data/shell/new_modify_version.py dest=/data/x5online'
192.168.170.136 | SUCCESS => {
    "changed": true, 
    "checksum": "7a1ea4f249ff86f7c24e579d2ea218c6d89538be", 
    "dest": "/data/x5online/new_modify_version.py", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "ca19faabc532bb3edfb7fc1fd0387570", 
    "mode": "0644", 
    "owner": "root", 
    "size": 552, 
    "src": "/root/.ansible/tmp/ansible-tmp-1479717087.3-157757039356660/source", 
    "state": "file", 
    "uid": 0
}
192.168.170.135 | SUCCESS => {
    "changed": true, 
    "checksum": "7a1ea4f249ff86f7c24e579d2ea218c6d89538be", 
    "dest": "/data/x5online/new_modify_version.py", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "ca19faabc532bb3edfb7fc1fd0387570", 
    "mode": "0644", 
    "owner": "root", 
    "size": 552, 
    "src": "/root/.ansible/tmp/ansible-tmp-1479717087.42-230634641222257/source", 
    "state": "file", 
    "uid": 0
}
192.168.170.129 | SUCCESS => {
    "changed": true, 
    "checksum": "7a1ea4f249ff86f7c24e579d2ea218c6d89538be", 
    "dest": "/data/x5online/new_modify_version.py", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "ca19faabc532bb3edfb7fc1fd0387570", 
    "mode": "0644", 
    "owner": "root", 
    "size": 552, 
    "src": "/root/.ansible/tmp/ansible-tmp-1479717088.53-133714246908318/source", 
    "state": "file", 
    "uid": 0
}

其中src后面为要拷贝文件的源目的地址,dest后面接的是你要把文件拷贝的目的地址,看到有success字样就说明成功了。all表示全部主机,也可以单独指定某个IP,-m后面接的是模块名,这里显示是拷贝,所以是copy,-a表示模块的参数

yum模块:

[root@LVS_Master shell]# ansible 192.168.170.129 -m yum -a "state=present name=tree"
192.168.170.129 | SUCCESS => {
    "changed": true, 
    "msg": "http://repo.zabbix.com/zabbix/3.0/rhel/6/x86_64/repodata/repomd.xml: [Errno 14] PYCURL ERROR 7 - \"couldn't connect to host\"\nTrying other mirror.\nhttp://repo.zabbix.com/non-supported/rhel/6/x86_64/repodata/repomd.xml: [Errno 14] PYCURL ERROR 7 - \"couldn't connect to host\"\nTrying other mirror.\n", 
    "rc": 0, 
    "results": [
        "Loaded plugins: fastestmirror\nSetting up Install Process\nLoading mirror speeds from cached hostfile\n * base: mirrors.aliyun.com\n * epel: mirrors.aliyun.com\n * extras: mirrors.aliyun.com\n * updates: mirrors.aliyun.com\nResolving Dependencies\n--> Running transaction check\n---> Package tree.x86_64 0:1.5.3-3.el6 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package         Arch              Version                Repository       Size\n================================================================================\nInstalling:\n tree            x86_64            1.5.3-3.el6            base             36 k\n\nTransaction Summary\n================================================================================\nInstall       1 Package(s)\n\nTotal download size: 36 k\nInstalled size: 65 k\nDownloading Packages:\nRunning rpm_check_debug\nRunning Transaction Test\nTransaction Test Succeeded\nRunning Transaction\n\r  Installing : tree-1.5.3-3.el6.x86_64                                      1/1 \n\r  Verifying  : tree-1.5.3-3.el6.x86_64                                      1/1 \n\nInstalled:\n  tree.x86_64 0:1.5.3-3.el6                                                     \n\nComplete!\n"
    ]
}

state=present表示安装,state=absent表示卸载,name后面接你要安装的包名

script模块:

目的:在全部节点上执行/root/new_test.sh脚本(该脚本是在ansible控制节点上的,脚本的功能为打印hello world)

[root@LVS_Master ~]# ansible all -m script -a "/root/new_test.sh"
192.168.170.135 | SUCCESS => {
    "changed": true, 
    "rc": 0, 
    "stderr": "", 
    "stdout": "hello world\r\n", 
    "stdout_lines": [
        "hello world"
    ]
}
192.168.170.136 | SUCCESS => {
    "changed": true, 
    "rc": 0, 
    "stderr": "", 
    "stdout": "hello world\r\n", 
    "stdout_lines": [
        "hello world"
    ]
}
192.168.170.129 | SUCCESS => {
    "changed": true, 
    "rc": 0, 
    "stderr": "", 
    "stdout": "hello world\r\n", 
    "stdout_lines": [
        "hello world"
    ]
}

command模块:

作用:远程执行命令的模块

[root@LVS_Master ~]# ansible all -m command -a "df -h"
192.168.170.135 | SUCCESS | rc=0 >>
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda5        11G  3.5G  6.8G  35% /
tmpfs           495M     0  495M   0% /dev/shm
/dev/sda1       194M   27M  158M  15% /boot
/dev/sda2       4.9G  138M  4.5G   3% /home/data

192.168.170.136 | SUCCESS | rc=0 >>
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda5        11G  5.0G  5.3G  49% /
tmpfs           495M     0  495M   0% /dev/shm
/dev/sda1       194M   27M  158M  15% /boot
/dev/sda2       4.9G  138M  4.5G   3% /home/data

192.168.170.129 | SUCCESS | rc=0 >>
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda5        11G  6.4G  3.9G  63% /
tmpfs           495M  168K  495M   1% /dev/shm
/dev/sda1       194M   30M  155M  16% /boot
/dev/sda2       4.9G  496M  4.4G  10% /home/data
/dev/sdb1       2.0G   37M  1.9G   2% /mfsdata

友提:

/etc/ansible/hosts文件为:

[webserver]
192.168.170.135
192.168.170.136
192.168.170.129

现在是最简单的一种形式,这个文件可以配置的非常灵活。

posted @ 2016-11-21 16:51  梦轻尘  阅读(259)  评论(0)    收藏  举报