五、ansible的常用模块(用ansible-doc -l可以显示)
(1)command模块:远程主机上运行命令
例如:ansible webservers -m command -a "ls /var"
ansible webservers -a "useradd user1",command模块可以省
ansible webservers -a "echo magedu |passwd –stdin user1" ,不成功,不支持管道
(2)shell模块:(管道可以用)远程主机在shell进程下运行命令,支持shell特性
例如:ansible webservers -m shell -a "echo cwj1111|passwd –stdin user1"
(3)copy模块:把文件复制到远程位置
例如:ansible all -m copy -a "src=/etc/fstab dest=/tmp/fstab" 可以指明mode(权限),group(组),owner(主)。
(4)cron:管理任务计划的
minute=,day=,month=,weekday=,hour=,job=,name=(必须要给),state=
例如:ansible all -m cron -a "minute=*/5 job='/sbin/ntpdate 192.168.1.109 &> /dev/null' name=Synctime "
在被管理主机上使用crontab -l便可以看到
ansible all -m cron -a "state=absent name=Synctime" 就可以删除
(5)fetch模块:拉取文件的(从远程主机上拉取文件到本地)
ansible-doc -s fetch 查看
(6)file模块:设定文件属性(属组,属主)
例如:ansible all -m file -a "src=/tmp/fstab path=/tmp/fstab.link state=link"
修改属性:path= ,owner= ,mode= ,group=
创建目录:ansible all -m file -a "path=/tmp/tmpdir state=directory "
(7)pip模块:管理python的模块
(8)yum模块:用yum包管理,管理包
例如:ansible all -m yum -a "name=httpd state=present"
上面显示的是我已经装过了,就没有改变,现在我们卸载了看看:ansible all -m yum -a "name=httpd state=absent"
在使用rpm看一下就没有了
(9)service模块:管理服务
name=,state=, started(启动),stopped(停止),restarted(重启), enabled=,runlevel=
例如:先查看两台主机的80端口,再启动
ansible all -m shell -a "ss -tnl |grep :80 "
ansible all -m service -a "name=httpd state=started"
启动之后:
(10)user模块:管理用户,账号,组
name=,system=,uid=,shell=,group=,groups=,home=,passwd=,remove=(state=absent,同时删除家目录)
例如:ansible all -m user -a "name=user2 system=yes state=present uid=306 "
(2)例子
先创建一个working文件夹,再cd到working里面,把yaml文件移到里面,创建一个files目录,把配置好的httpd.conf移到里面去,改一下监听端口,改成8080
ansible-playbook –check web.yaml
测试一下:
启动:ansible all -m service -a "name=httpd state=restarted"
ansible all -m shell -a "ss -tnl |grep :8080"