Ansible 系列之 Ad-Hoc介绍及使用

Ad-Hoc 介绍html

1、什么是ad-hoc 命令?node

ad-hoc 命令是一种能够快速输入的命令,并且不须要保存起来的命令。就至关于bash中的一句话shell。这也是一个好的地方,在学习ansible playbooks时能够先了解另一种ansible基本的快速用法,不必定非要写一个palybook文件。python

通常来讲,ansible的强大之处在于它的playbook 剧本。但为何咱们还要使用这种临时的命令呢?git

临时命令适用于下面相似的场景,若是你想在圣诞节到来之时,关掉实验室的电脑,只须要ansible 的一行命令便可,而没必要编写一个playbook文件来完成这个工做。github

不过,对于配置管理和应用部署这种工做,仍是须要使用“/usr/bin/ansible-playbook”命令。web

一、并行和Shell 命令docker

接上文,ansible 服务器已经配置好使用密钥进行认证,管理主机,若是不想使用密钥的话,那么可使用--ask-pass (-k) 来用密码管理。可是最好仍是用密钥的方式。shell

以下:使用如下命令来查看webserver 组内主机的端口开放情况:vim

[root@docker ~]# ansible webserver -a 'netstat -ulntp'      
172.17.0.3 | SUCCESS | rc=0 >>
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      -                   
tcp6       0      0 :::22                   :::*                    LISTEN      -                   

web1 | SUCCESS | rc=0 >>
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:222             0.0.0.0:*               LISTEN      -                   
tcp6       0      0 :::222                  :::*                    LISTEN      -         

命令的最后也能够加 -f number ,表示使用的并发进程数目,默认是5个,以下:bash

ansible webserver -a 'netstat -ulntp' -f 15

/usr/bin/ansible 默认使用当前ansible 服务器登录的用户来进行管理,若是你不喜欢这样,也可使用 -u username 的方式来指定用户,以下:

注:(zhangsan 这个用户必须是被管理主机上真实存在的)

[root@docker ~]# ansible webserver -a "w" -u zhangsan -k

若是你不想使用当前的用户来管理运行命令,也可使用 --become -K 命令提高权限.

 

以上是关于ansible 的基础,ansible 有许多的模块,以上的栗子中,没有指定模块,由于 默认的模块是 command ,若是要想使用其它模块,能够用-m 模块名 来指定。

注:command 模块不支持扩展的shell语法,如使用管道和重定向。固然若是须要特殊的shell 语法,可使用shell模块来完成任务。像下面这样:

[root@docker ~]# ansible webserver -m shell -a 'echo $TERM'
web1 | SUCCESS | rc=0 >>
xterm-256color

172.17.0.3 | SUCCESS | rc=0 >>
xterm-256color

 

二、文件传输管理

这里是/usr/bin/ansible 命令行的另一个用例,Ansible 能够将多个文件并发的拷贝到多台机器上。使用 copy 模块,将文件直接传输到多个服务器上,以下:

[root@docker ~]# ansible webserver -m copy -a "src=/etc/hosts dest=/tmp/hosts"
172.17.0.3 | SUCCESS => {
    "changed": true, 
    "checksum": "ba0ed35ca3f16342b883784ec7928491d359b8ab", 
    "dest": "/tmp/hosts", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "9e979f3a6509f8d829209613343f90b9", 
    "mode": "0644", 
    "owner": "root", 
    "size": 117, 
    "src": "/root/.ansible/tmp/ansible-tmp-1487773694.97-103709947729677/source", 
    "state": "file", 
    "uid": 0
}
web1 | SUCCESS => {
    "changed": true, 
    "checksum": "ba0ed35ca3f16342b883784ec7928491d359b8ab", 
    "dest": "/tmp/hosts", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "9e979f3a6509f8d829209613343f90b9", 
    "mode": "0644", 
    "owner": "root", 
    "size": 117, 
    "src": "/root/.ansible/tmp/ansible-tmp-1487773694.94-149872215856203/source", 
    "state": "file", 
    "uid": 0
}

检查一下:

[root@docker ~]# ansible webserver -a 'stat /tmp/hosts'
web1 | SUCCESS | rc=0 >>
  File: '/tmp/hosts'
  Size: 117           Blocks: 8          IO Block: 4096   regular file
Device: fc03h/64515d    Inode: 25186117    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2017-02-22 22:28:58.946882890 +0800
Modify: 2017-02-22 22:28:15.001562188 +0800
Change: 2017-02-22 22:28:15.355564788 +0800
 Birth: -

172.17.0.3 | SUCCESS | rc=0 >>
  File: '/tmp/hosts'
  Size: 117           Blocks: 8          IO Block: 4096   regular file
Device: fc02h/64514d    Inode: 41950463    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2017-02-22 22:28:58.949882912 +0800
Modify: 2017-02-22 22:28:15.041562482 +0800
Change: 2017-02-22 22:28:15.349564744 +0800
 Birth: -

 

说下另一个模块 file ,它容许更改文件的宿主以及权限,这些相同的选项一样适用 copy 模块,以下:

[root@docker ~]# ansible webserver -m file -a "dest=/tmp/hosts mode=600"
web1 | SUCCESS => {
    "changed": true, 
    "gid": 0, 
    "group": "root", 
    "mode": "0600", 
    "owner": "root", 
    "path": "/tmp/hosts", 
    "size": 117, 
    "state": "file", 
    "uid": 0
}
172.17.0.3 | SUCCESS => {
    "changed": true, 
    "gid": 0, 
    "group": "root", 
    "mode": "0600", 
    "owner": "root", 
    "path": "/tmp/hosts", 
    "size": 117, 
    "state": "file", 
    "uid": 0
}

更改文件的宿主以及属组:

[root@docker ~]# ansible webserver -m file -a "dest=/tmp/hosts mode=600 owner=zhangsan group=zhangsan"
web1 | SUCCESS => {
    "changed": true, 
    "gid": 1000, 
    "group": "zhangsan", 
    "mode": "0600", 
    "owner": "zhangsan", 
    "path": "/tmp/hosts", 
    "size": 117, 
    "state": "file", 
    "uid": 1000
}
172.17.0.3 | SUCCESS => {
    "changed": true, 
    "gid": 1000, 
    "group": "zhangsan", 
    "mode": "0600", 
    "owner": "zhangsan", 
    "path": "/tmp/hosts", 
    "size": 117, 
    "state": "file", 
    "uid": 1000
}

 本文属于做者原创,转载请注明出处:飞走不可 :http://www.cnblogs.com/hanyifeng/p/6431450.html

使用file 模块来建立目录,相似于 mkdir -p,以下:

[root@docker ~]# ansible webserver -m file -a "dest=/tmp/zhangsan/pp/1 mode=755 owner=zhangsan group=zhangsan state=directory"
web1 | SUCCESS => {
    "changed": true, 
    "gid": 1000, 
    "group": "zhangsan", 
    "mode": "0755", 
    "owner": "zhangsan", 
    "path": "/tmp/zhangsan/pp/1", 
    "size": 6, 
    "state": "directory", 
    "uid": 1000
}
172.17.0.3 | SUCCESS => {
    "changed": true, 
    "gid": 1000, 
    "group": "zhangsan", 
    "mode": "0755", 
    "owner": "zhangsan", 
    "path": "/tmp/zhangsan/pp/1", 
    "size": 6, 
    "state": "directory", 
    "uid": 1000
}

以及删除目录(递归)和删除文件,以下:

[root@docker ~]# ansible webserver -m file -a "dest=/tmp/zhangsan/pp/1 state=absent"
172.17.0.3 | SUCCESS => {
    "changed": true, 
    "path": "/tmp/zhangsan/pp/1", 
    "state": "absent"
}
web1 | SUCCESS => {
    "changed": true, 
    "path": "/tmp/zhangsan/pp/1", 
    "state": "absent"
}

 

3.软件包管理

包括yum 和 apt,如下是一些yum 的示例。

确保该软件包已经安装,但不要更新它,至关于检查改软件是否安装:

[root@docker ~]# ansible webserver -m yum -a "name=vim state=present"
172.17.0.3 | SUCCESS => {
    "changed": false, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "vim-enhanced-2:7.4.160-1.el7_3.1.x86_64 providing vim is already installed"
    ]
}
web1 | SUCCESS => {
    "changed": false, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "vim-enhanced-2:7.4.160-1.el7_3.1.x86_64 providing vim is already installed"
    ]
}

 

确保软件安装的是最新的版本,以下:

[root@docker ~]# ansible webserver -m yum -a "name=vim state=latest"
172.17.0.3 | SUCCESS => {
    "changed": false, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "All packages providing vim are up to date", 
        ""
    ]
}
web1 | SUCCESS => {
    "changed": false, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "All packages providing vim are up to date", 
        ""
    ]
}

 

确保软件没有被安装:

[root@docker ~]# ansible webserver -m yum -a "name=vim state=absent"

 

4.用户和组管理

"user" 模块容许轻松的建立和管理现有的用户帐号,以及删除可能存在的用户帐号,以下:

建立一个用户,并设置密码(这里的密码必须是加密后的。这里有坑,若是你写成了明文的密码如如:123456,那么系统的root密码就是未知(/etc/shadow文件中,该用户的密码位置那就变成123456了,即误搞成加密后的密码是123456了!)

[root@docker ~]# ansible webserver -m user -a "name=xiaoming password=securitytext"
web1 | SUCCESS => {
    "changed": true,
    "comment": "",
    "createhome": true,
    "group": 1001,
    "home": "/home/xiaoming",
    "name": "xiaoming",
    "password": "NOT_LOGGING_PASSWORD",
    "shell": "/bin/bash",
    "state": "present",
    "system": false,
    "uid": 1001
}
172.17.0.3 | SUCCESS => {
    "changed": true,
    "comment": "",
    "createhome": true,
    "group": 1001,
    "home": "/home/xiaoming",
    "name": "xiaoming",
    "password": "NOT_LOGGING_PASSWORD",
    "shell": "/bin/bash",
    "state": "present",
    "system": false,
    "uid": 1001
}

 

本文属于做者原创,转载请注明出处:飞走不可 :http://www.cnblogs.com/hanyifeng/p/6431450.html

建立用户时使用加密后的密码来设置,其它方法可参考这里

先用python 的 crypt模块来对密码 进行加密,如:

[root@docker ~]# python -c 'import crypt; print crypt.crypt("123456", "hello")'
heepn6ZumUmSE

使用上述密码,建立用户:

[root@docker ~]# ansible webserver -m user -a "name=huahua shell=/bin/bash password=heepn6ZumUmSE update_password=always"
172.17.0.3 | SUCCESS => {
    "changed": true, 
    "comment": "", 
    "createhome": true, 
    "group": 1003, 
    "home": "/home/huahua", 
    "name": "huahua", 
    "password": "NOT_LOGGING_PASSWORD", 
    "shell": "/bin/bash", 
    "state": "present", 
    "system": false, 
    "uid": 1003
}
web1 | SUCCESS => {
    "changed": true, 
    "comment": "", 
    "createhome": true, 
    "group": 1003, 
    "home": "/home/huahua", 
    "name": "huahua", 
    "password": "NOT_LOGGING_PASSWORD", 
    "shell": "/bin/bash", 
    "state": "present", 
    "system": false, 
    "uid": 1003
}

 

删除用户并移除用户家目录(remove 要和 state 参数一块儿使用,至关于userdel -r):

[root@docker ~]# ansible webserver -m user -a "name=xiaoming state=absent remove=yes"
172.17.0.3 | SUCCESS => {
    "changed": true,
    "force": false,
    "name": "xiaoming",
    "remove": true,
    "state": "absent"
}
web1 | SUCCESS => {
    "changed": true,
    "force": false,
    "name": "xiaoming",
    "remove": true,
    "state": "absent"
}

 

 5.从版本控制中部署程序

直接从git 上部署web 应用

使用 git模块,要先保证远程主机上有git软件,以下所示,检查git 已被安装:

[root@docker ~]# ansible webserver -m yum -a "name=git state=present"
172.17.0.3 | SUCCESS => {
    "changed": false,
    "msg": "",
    "rc": 0,
    "results": [
        "git-1.8.3.1-6.el7_2.1.x86_64 providing git is already installed"
    ]
}
web1 | SUCCESS => {
    "changed": false,
    "msg": "",
    "rc": 0,
    "results": [
        "git-1.8.3.1-6.el7_2.1.x86_64 providing git is already installed"
    ]
}

确保已经安装以后,再来从git上拉取源码,以下:

[root@docker ~]# ansible webserver -m git -a "repo=git://github.com/aliasmee/hello.git dest=/usr/myapp version=HEAD"
web1 | SUCCESS => {
    "after": "f102d1927c4d42cfcca42aaa8e961be4c0b06e00",
    "before": null,
    "changed": true,
    "warnings": []
}
172.17.0.3 | SUCCESS => {
    "after": "f102d1927c4d42cfcca42aaa8e961be4c0b06e00",
    "before": null,
    "changed": true,
    "warnings": []
}

验证一下:

[root@docker ~]# ansible webserver -a "ls /usr/myapp"
172.17.0.3 | SUCCESS | rc=0 >>
README.md
cpu_load.sh
diyHttpServer.py
look_IP.sh
one.py
two.txt

web1 | SUCCESS | rc=0 >>
README.md
cpu_load.sh
diyHttpServer.py
look_IP.sh
one.py
two.txt

 

6.服务管理
 
确保http服务是打开的状态:
ansible webserver -m service -a "name=httpd state=started"

重启webserver组内的 web服务器:

ansible webserver -m service -a "name=httpd state=restarted"

很遗憾,个人测试环境中,由于被管理机器都是docker 容器,并且 ansible 的 service 模块,官方发文说如今还不支持容器的服务支持。详见此页面:https://github.com/ansible/ansible-modules-core/issues/4024

 

7.收集信息

Facts就是主机上已经发现的变量,在playbooks中有描述。能够用于实现指定的任务的条件或者获取特定的信息,能够经过下面来得到全部 facts:

[root@docker ~]# ansible all -m setup

 

8.脚本模块

scripts 脚本模块采用脚本名称,后面跟空格分隔的参数列表组成,以下所示:

[root@docker ~]# ansible webserver -m script -a "/tmp/myapp/cpu_load.sh"

上面栗子中,位于本地路径的脚本将被传输到远程主机上并执行,适合本地写好的安装程序脚本,或其它自定义脚本。

 好吧,模块还有不少不少,具体的只有等用到时仔细研究了,下一篇开始进入playbooks 的学习了。新手上路,文中若是有错误的地方,还请大牛们多多指教。

 

 

本文属于做者原创,转载请注明出处:飞走不可 :http://www.cnblogs.com/hanyifeng/p/6431450.html

 

参考资料连接:http://docs.ansible.com/ansible/intro_adhoc.html

相关文章
相关标签/搜索