|
二:ansible核心模块介绍
(一)ping (ping模块,用于确认和对象机器之间是否能够ping通,正常情况会返回pong )
ansible 172.16.19.114 -m ping (二)command (command 模块用于运行系统命令,比如echo hello, 你安装在系统里的python,或者make 一类)
Executes a command on a remote node
[iyunv@7 ansible]# ansible webservers -m command -a "ifconfig" (三)shell (这个是一个很神奇的模块,它也是ansible的核心模块之一。可以让另外一台主机执行命令)
ansible 172.16.19.249 -m shell -a "echo '123456' | passwd --stdin uplooking" (四)copy (copy模块在ansible里的角色就是把ansible执行机器上的文件拷贝到远程节点上。 )
ansible dbservers -m copy -a "src=/root/hello dest=/root/nihao"ansible 172.16.19.249 -m copy -a "src=/root/hello dest=/root/hello owner=uplooking group=uplooking mode=777" (五)cron (用于管理任务计划,在一段时间内把所得到的数据输入到 /dev/null)
ansible all -m cron -a "minute=*/30 job='/usr/sbin/ntpdate s2c.time.edu.cn &> /dev/null'" (六)fetch (从远程节点上拷贝文件到ansible执行机器上)
ansible 172.16.19.249 -m fetch -a "src=/etc/profile dest=/tmp" (七)file (改变一个文件的主,组 ,权限)
ansible 172.16.19.249 -m file -a "path=/root/nihao owner=uplooking group=uplooking mode=777"ansible 172.16.19.249 -m file -a "path=/root/ops state=directory" (八)hostname (更改远程节点的用户名)
ansible 172.16.19.248 -m hostname -a "name=node3" (九)pip (在远程节点上安装一个jinjia2的模块)
ansible 172.16.19.249 -m pip -a "name=jinja2" (十)yum (使用yum包管理器来管理软件包)
ansible webservers -m yum -a "name=httpd state=latest" (安装) ansible webservers -m yum -a "name=httpd state=absent" (=removed移除vb) (十一)service (管理服务器 ,enables=yes 是开机自启)
ansible webservers -m service -a "name=httpd state=started enabled=yes" (十二)user (user模块是请求的是useradd, userdel, usermod三个指令 createhome:创建home目录 ,system : 系统用户 , shell:不允许登录)
ansible 172.16.19.246 -m user -a "name=home1 createhome=no uid=4321 system=yes shell=/sbin/nologin" (十三)group (goup模块请求的是groupadd, groupdel, groupmod 三个指令)
ansible 172.16.19.246 -m group -a "name=uplooking gid=3120" (十四)setup (setup模块,主要用于获取主机信息,在playbooks里经常会用到的一个参数gather_facts就与该模块相关。)
ansible 172.16.19.246 -m setup (十五)script (scripts模块可以在本地写一个脚本,然后在远程服务器上执行)
ansible 172.16.19.246 -m script -a "/root/useradd.sh" (十六)template (template使用了Jinjia2格式作为文件模版,进行文档内变量的替换的模块)
把/mytemplates/foo.j2文件经过填写参数后,复制到远程节点的/etc/file.conf,文件权限相关略过- template: src=/mytemplates/foo.j2 dest=/etc/file.conf owner=bin group=wheel mode=0644 跟上面一样的效果,不一样的文件权限设置方式- template: src=/mytemplates/foo.j2 dest=/etc/file.conf owner=bin group=wheel mode="u=rw,g=r,o=r" (十七)unarchive (用于解压文件)
ansible 172.16.19.246 -m unarchive -a "src=/root/wordpress.zip dest=/tmp" (十八)mount (配置挂载点)
ansible 172.16.19.246 -m mount -a "path=/var/www/html src=172.16.19.246:/data/static fstype=nfs state=mounted" |
|
|