• 用roles部署nginx


    1、初始化一个role
    
    [root@bogon ~]# ansible-galaxy init /etc/ansible/roles/websrvs
    
    查看已经创建的role
    [root@bogon ~]# ls /etc/ansible/roles/
    webservs
    
    
    
    2、配置role
    
    把初始化后 role里面没用的删除,只留下面四个目录
    
    [root@bogon ~]# cd /etc/ansible/roles/webservs/
    [root@bogon webservs]# ls
    handlers  README.md  tasks  templates  vars
    [root@bogon webservs]# ls templates/
    index.html.j2  nginx.conf.j2
    
    
    配置变量vars
    
    [root@bogon webservs]# cat vars/main.yml 
    ---
    # vars file for /etc/ansible/roles/webservs
    worker_processes: 4
    worker_connections: 768
    max_open_files: 65506
    
    
    tasks 文件内容
    
    [root@bogon webservs]# cat tasks/main.yml 
    ---
    # tasks file for /etc/ansible/roles/webservs
    - name: install nginx
      command: yum install nginx -y
    
    - name: copy nginx config file
      template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
      notify: restart nginx
    
    - name: copy index.html
      template:
        src: index.html.j2
        dest: /usr/share/nginx/www/index.html
        mode: 0644
      notify: restart nginx
    
    - name: see file
      command: ls /root
      notify: restart nginx
    
    
    handlers 文件内容:
    
    [root@bogon webservs]# cat handlers/main.yml 
    ---
    # handlers file for /etc/ansible/roles/webservs
    - name: restart nginx
      service: name=nginx state=restarted
    
    
    模板文化内容:
    
    [root@bogon webservs]# cat templates/nginx.conf.j2 
    worker_processes {{ worker_processes }};
    worker_rlimit_nofile {{ max_open_files }};
    
    events {
        worker_connections {{ worker_connections }};
    }
    
    http {
        server {
            listen       80;
            root  /usr/share/nginx/www;
            index index.html index.htm default.html index.php;
            server_name loclhost;
            location / {
                try_files  / =404;
            }
        }
        
    }
    
    
    [root@bogon webservs]# cat templates/index.html.j2 
    <html>
      <head>
        <title>welcome to american</title>
      </head>
      <body>
      <h1>nginx, confitured by ansible</h1>
      <p>if you can see this, ansible successfully installed nginx.</p>
      
      <p>{{ ansible_hostname }}</p>
      </body>
    </html>
    
    
    3、配置playbook,把role添加进来
    
    [root@bogon ~]# cat nginx_role.yaml 
    ---
    - hosts: webservers
      become: yes
      become_method: sudo
      roles:
        - role: webservs
    
    4、开始执行Playbook
    
    [root@bogon ~]# ansible-playbook nginx_role.yaml
  • 相关阅读:
    EF4.1中诡异的GUID为空问题
    asp.net mvc3 Razor引擎中@使用规则小记
    【推荐】JS面象对象编程视频教程
    【转】【More Effective C#】IEnumerable和IQueryable的那点事
    【你有更好的算法吗?】合并重叠时间段算法
    jquery处理textarea中的手动换行
    实例讲解遗传算法——基于遗传算法的自动组卷系统【理论篇】
    架设自己的NuGet服务器
    C#中用SelectSingleNode方法解析带有多个命名空间的XML文件
    解析入口参数为实体的表达式树
  • 原文地址:https://www.cnblogs.com/effortsing/p/10284361.html
Copyright © 2020-2023  润新知