ansible webserver -m yum -a 'name="*" stale=lastest' //升级所有包
ansible webserver -m yum -a 'name=vsftpd state=present' //安装vsftpd
ansible webserver -m yum -a 'name=vsftpd state=absent' //卸载vsftpd
ansible webserver -m yum -a 'name=vsftpd state=latest' //升级vsftpd
5.服务模块
ansible-doc service //查看帮助文档
ansible webserver -m service -a 'name=vsftpd state=started' //开启httpd服务
ansible webserver -m service -a 'name=vsftpd state=stopped' //关闭httpd服务
ansible webserver -m service -a 'name=vsftpd state=restarted' //重启httpd服务
ansible webserver -m service -a 'name=vsftpd state=started enabled=yes' //开启开机自启
ansible webserver -m service -a 'name=vsftpd state=started enabled=no' //关闭开机自启
6.文件模块
ansible-doc file //查看帮助文档
ansible webservice -m file -a 'path=/tmp/11.txt mode=771 state=touch' //创建文件
ansible webserver -m file -a 'path=/tmp/asb mode=770 state=directory' //创建目录
7.收集模块
ansible-doc setup //查看帮助文档
ansible host1 -m setup //列出常见常熟
ansible host1 -m setup -a 'filter=ansible_all_ipv4_addresses' //查询ip地址
7.YAML
YAML Ain’t Markup Language-非标记语言
示例:通过YAML编写一个简单的剧本,完成web的部署,配置,启动的全过程。
1.准备
ansible all -m yum -a 'name=httpd state=removed' -o //清理一下环境
yum install -y httpd //准备配置文件
mkdir apache
cd apache
cp -rf /etc/httpd/conf/httpd.conf .
grep '^Listen' httpd.conf
Listen 8080 //修改端口
2.编写剧本
编写
vim apache.yaml
- hosts: host2
tasks:
- name: install apache packages
yum: name=httpd state=present
- name: copy apache conf
copy: src=./httpd.conf dest=/etc/httpd/conf/httpd.conf
- name: ensure apache is running
service: name=httpd state=started enabled=yes
测试
ansible-playbook apache.yaml --syntax-check
ansible-playbook apache.yaml --list-tasks
ansible-playbook apache.yaml --list-hosts
执行
ansible-playbook apache.yaml
3.优化
如果我们在配置文件中修改了apache端口号为9000,那么我们再次重启会发现文件会被拷贝过去,但是服务器并没没有重启。
因为剧本中写的服务启动,并没有重启服务。需要优化一下剧本代码,如果执行copy语句那么notify会触发,接着会执行handlers中的语句重启服务。
- hosts: webserver
tasks:
- name: install apache packges for xux
yum: name=httpd state=latest
- name: copy apache conf file for xux
copy: src=./httpd.conf dest=/etc/httpd/conf/httpd.conf
notify: restart apache service for xux
- name: ensure apache si running
service: name=httpd state=started enabled=yes
handlers:
- name: restart apache service for xux
service: name=httpd state=restarted
8.Role-角色扮演
roles则是在ansible中,playbooks的目录组织结构。将代码或文件进行模块化,成为roles的文件目录组织结构,易读,代码可重用,层次清晰。
示例:通过role远程部署nginx并配置
1.准备目录
nginx 角色名
files 普通文件
handlers 触发器程序
tasks 主任务
templates 金甲模板(有变量的文件)
vars 自定义变量
[root@localhost ~]# mkdir roles/nginx/{files,handlers,tasks,templates,vars} -p
touch roles/site.yaml roles/nginx/{handlers,tasks,vars}/main.yaml
echo 1234 > roles/nginx/files/index.html
yum install -y nginx && cp /etc/nginx/nginx.conf roles/nginx/templates/nginx.conf.j2
[root@localhost ~]# tree roles/
roles/
├── nginx
│ ├── files
│ │ └── index.html
│ ├── handlers
│ │ └── main.yaml
│ ├── tasks
│ │ └── main.yaml
│ ├── templates
│ └── vars
│ └── main.yaml
└── site.yaml
2.编写任务
vim roles/nginx/tasks/main.yaml
- name: install epel-release packge
yum: name=epel-release state=latest
- name: install nginx packge
yum: n