ad-hoc 使用场景
lAnsible提供两种方式去完成任务,一是 ad-hoc 命令,一是写 Ansible playbook,在学习了 playbooks 之后,你才能体会到 Ansible 真正的强大之处在哪里。
lad-hoc更注重于解决一些简单或者平时工作中临时遇到的任务,相当于Linux系统命令行下的Shell命令,后者更适合于解决复杂或需固化下来的任务,相当于Linux系统的Shell Scripts。
l使用场景:
关闭所有不必要的服务器
临时更新Apache或Nginx的配置文件
Ansible的并发特性
l
Ansible和Ansible-playbook默认会fork 5个线程并发执行命令
,如果同时操作的主机数比较多的话,可以调整到一个更大的值。
l
l
Ansible为我们提供了便捷的选项,-f指定线程数。
# ansible webserver -m ping -f 3
ansible有许多模块,默认是 command,也就是命令模块,我们可以通过 -m 选项来指定不同的模块。
Ansible-doc用法
l
ansible的模块非常之多,Ansible也提供了类似于man功能的help说明工具ansible-doc。
l
l
正式学习Ansible模块使用前,有必要先了解ansible-doc用法:
显示所有可用模块
# ansible-doc -l
获取yum模块帮助
# ansible-doc yum
Ansible常用模块
l
copy模块
# ansible webservers -m copy -a "src=/etc/hosts dest=/tmp/hosts"
l
l
file模块
# ansible webservers -m file -a "dest=/tmp/hosts mode=600 owner=root group=root"
//
修改文件权限和属性
# ansible webservers -m file -a "dest=/tmp/dir1/dir2 mode=755 owner=root group=root state=directory"
//
递归创建
# ansible webservers -m file -a "dest=/tmp/dir1/dir2 state=absent"
l
yum模块
# ansible webservers -m yum -a "name=httpd state=present"
# ansible server3 -m yum -a "name=http://172.25.0.250/rhel7.3/x86_64/dvd/Packages/vsftpd-3.0.2-21.el7.x86_64.rpm state=present"
//
在线安装
# ansible server3 -m yum -a "name=/mnt/vsftpd-3.0.2-21.el7.x86_64.rpm state=present"
//
本地安装
# ansible server3 -m yum -a "name=httpd state=absent"
//
卸载软件
l
service模块
# ansible webservers -m service -a "name=httpd state=started"
# ansible webservers -m service -a "name=httpd state=restarted"
# ansible webservers -m service -a "name=httpd state=stopped"
l
user模块
# ansible all -m user -a "name=wxh password=<加密密码>"
# ansible all -m user -a "name=wxh state=absent remove=yes"
# ansibledb -m user -a "name=wxh shell=/bin/bash groups=users,wheel append=yes state=present"
l
mysql_user模块
p
# ansible server3 -m mysql_user -a "name=wxh password=testpass priv=*.*:select host='%' state=present"
远程主机需要安装MySQL-python
复制文件:
查看文件属性:
安装Apache服务:
启动Apache服务:
防火墙允许Apache服务:
复制管理机的html文件到目的在主机:
修改文件属性:
创建用户:
安装MySQL数据库:
启动mariadb服务:
创建MySQL用户: