设为首页 收藏本站
查看: 817|回复: 0

[经验分享] Ansible 系列之 Ad-Hoc介绍及使用

[复制链接]

尚未签到

发表于 2018-1-2 19:28:54 | 显示全部楼层 |阅读模式
  Ad-Hoc 介绍
  一、什么是ad-hoc 命令?
  ad-hoc 命令是一种可以快速输入的命令,而且不需要保存起来的命令。就相当于bash中的一句话shell。这也是一个好的地方,在学习ansible playbooks时可以先了解另外一种ansible基本的快速用法,不一定非要写一个palybook文件。
  一般来说,ansible的强大之处在于它的playbook 剧本。但为什么我们还要使用这种临时的命令呢?
  临时命令适用于下面类似的场景,如果你想在圣诞节到来之时,关掉实验室的电脑,只需要ansible 的一行命令即可,而不必编写一个playbook文件来完成这个工作。
  不过,对于配置管理和应用部署这种工作,还是需要使用“/usr/bin/ansible-playbook”命令。
  1、并行和Shell 命令
  接上文,ansible 服务器已经配置好使用密钥进行认证,管理主机,如果不想使用密钥的话,那么可以使用--ask-pass (-k) 来用密码管理。但是最好还是用密钥的方式。
  如下:使用以下命令来查看webserver 组内主机的端口开放状况:
  

[iyunv@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个,如下:
  

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

  /usr/bin/ansible 默认使用当前ansible 服务器登陆的用户来进行管理,如果你不喜欢这样,也可以使用 -u username 的方式来指定用户,如下:
  注:(zhangsan 这个用户必须是被管理主机上真实存在的)
  

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

  如果你不想使用当前的用户来管理运行命令,也可以使用 --become -K 命令提升权限.
  以上是关于ansible 的基础,ansible 有许多的模块,以上的栗子中,没有指定模块,因为 默认的模块是 command ,如果要想使用其它模块,可以用-m 模块名 来指定。
  注:command 模块不支持扩展的shell语法,如使用管道和重定向。当然如果需要特殊的shell 语法,可以使用shell模块来完成任务。像下面这样:
  

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

  

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

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

[iyunv@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
  
}
  

  检查一下:
  

[iyunv@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 模块,如下:
  

[iyunv@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  
}
  

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

[iyunv@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,如下:
  

[iyunv@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  
}
  

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

[iyunv@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 的示例。
  确保该软件包已经安装,但不要更新它,相当于检查改软件是否安装:
  

[iyunv@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"
  
]
  
}
  

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

[iyunv@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",
  
""
  
]
  
}
  

  确保软件没有被安装:
  

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

  4.用户和组管理
  "user" 模块允许轻松的创建和管理现有的用户账号,以及删除可能存在的用户账号,如下:
  创建一个用户,并设置密码(这里的密码必须是加密后的。这里有坑,如果你写成了明文的密码如如:123456,那么系统的root密码就是未知(/etc/shadow文件中,该用户的密码位置那就变成123456了,即误搞成加密后的密码是123456了!)
  

[iyunv@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模块来对密码 进行加密,如:
  

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

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

[iyunv@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):
  

[iyunv@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 已被安装:
  

[iyunv@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上拉取源码,如下:
  

[iyunv@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": []  
}
  

  验证一下:
  

[iyunv@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:
  

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

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

[iyunv@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

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-430911-1-1.html 上篇帖子: 《Ansible权威指南》笔记(1) 下篇帖子: ansible常用的一些模块
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表