第 5.1 章 Playbook基本使用
playbook特点:
• 易读的编排语言 - YAML
• 适合配置管理和应用部署
• 非常适合部署复杂的工作
通过一个Nginx安装示例来了解playbook用法:创建一个目录用来存放Nginx安装所用文件。(注意文件格式。缩进使用空格,不使用Tab键)
#nginx.yml - hosts: abc vars: hello: Ansible tasks: - name: Add repo yum_repository: name: nginx description: nginx repo baseurl: http://nginx.org/packages/centos/7/$basearch/ gpgcheck: no enabled: 1 - name: Install nginx yum: name: nginx state: latest - name: Copy nginx configuration file copy: src: ./site.conf dest: /etc/nginx/conf.d/site.conf - name: Start nginx service: name: nginx state: started - name: Create wwwroot directory file: dest: /var/www/html state: directory - name: Create test page index.html shell: echo "hello {{hello}}" > /var/www/html/index.html - name: firewalld 80 shell: firewall-cmd --add-port=80/tcp --permanent - name: firewall service restart service: name: firewalld state: restarted
创建Nginx配置文件:
#site.conf server { listen 80; server_name www.ctnrs.com; location / { root /var/www/html; index index.html; } }
保存后检查yml脚本语法:
yml文件语法检查: ansible-playbook nginx.yml --syntax-check
执行yml文件运行;ansible-playbook nginx.yml
检测abc主机组Nginx网站测试命令:
curl 142.4.123.13 -H "Host:www.ctnrs.com"
curl 47.241.90.203 -H "Host:www.ctnrs.com"
文章目录
关闭