ansible进阶模板和角色使用

使用场景  

    Ansible 由于采用ssh远程连接登录管理,虽然不需要额外安装agent,但是速度慢效率低.不适合管理大规模的主机一般最大规模在200-300台,超大规模的主机管理适合使用puppet

模板

     templates功能:根据模块文件动态生成对应的配置文件 

     templates文件必须存放于templates目录下,且命名为 .j2 结尾 

     yaml/yml 文件需和templates目录平级 

Jinja2语言

     字符串:使用单引号或双引号

     数字:整数,浮点数

     列表:[item1, item2, ...]

     元组:(item1, item2, ...)

     字典:{key1:value1,     key2:value2, ...}

     布尔型:true/false 

     算术运算:+,   -,   *,   /,   //,    %,   **         // 表示把相除得到的结果取整数

     比较操作: ==,    !=,    >,    >=,   <,    <= 

     逻辑运算:and,    or,     not 

     流表达式: for      if     when

   when条件判断语法
  tasks:
    - name: install nginx
      yum: name=nginx
    - name: copy config for 6
      template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
      when: ansible_distribution_major_version == "6"
      notify: restart nginx
    - name: copy config for 7
      template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
      when: ansible_distribution_major_version == "7"
when条件判断
迭代:with_items 

     迭代:当有需要重复性执行的任务时,可以使用迭代机制        如:同时安装多个软件包或者批量创建文件

---
- hosts: webservers
  remote_user: root

  tasks:
   - name: create some files
     file: name=/data/ {{ item }}  state=touch

     with_items:
       - file1
       - file2
       - file3

   - name: install package
     yum: name={{ item }}

     with_items:
      - htop
      - sl
      - hping3
迭代
- hosts:websrvs   
  remote_user: root   

  tasks:   
    - name: add some groups     
      group: name={{ item }} state=present     

     with_items:     
       - group1     
       - group2     
       - group3    

    - name: add some users    
      user: name={{ item.name }} group={{ item.group }} state=present     with_items:     
          - { name: 'user1', group: 'group1' }    
          - { name: 'user2', group: 'group2' }     
          - { name: ‘user3’, group: ‘group3’ }  
迭代嵌套
 模板文件j2语法
{% for vhost in  nginx_vhosts %} 
   server
    {    
        listen {{ vhost.listen }}    
        server_name {{ vhost.server_name }}    
        root  {{ vhost.root }}    
   } 

 {% endfor %} 

{% for vhost in  nginx_vhosts %} 
    server 
    {   
        listen {{ vhost.listen }}   
        {% if vhost.server_name is defined %}    
           server_name {{ vhost.server_name }}    
        {% endif %}    
      root  {{ vhost.root }} 
    } 
{% endfor %}  
模板语法

角色

     用于层次性,结构化地组织playbook.roles 能够根据层次型结构自动装载变量文件,tasks以及handlers等.要使用roles只需要在playbook中使用include指令即可

     Roles各目录作用 :

        files/ :存放由copy或script模块等调用的文件

        templates/:template模块查找所需要模板文件的目录 

        tasks/:定义task,role的基本元素,至少应该包含一个名为main.yml的文件;其它的文 件需要在此文件中通过include进行包含 

        handlers/:至少应该包含一个名为main.yml的文件;其它的文件需要在此文件中通过 include 进行包含 

        vars/:定义变量,至少应该包含一个名为main.yml的文件;其它的文件需要在此文件中通过include进行包含 

        meta/:定义当前角色的特殊设定及其依赖关系,至少应该包含一个名为main.yml的文 件,其它文件需在此文件中通过include进行包含 

        default/:设定默认变量时使用此目录中的main.yml文件 

   角色的基本示例

使用Role安装编译nginx的流程
1.创建group nginx
2.创建user nginx
3.yum install nginx
4.template nginx.conf.j2
5.service nginx

[root@localhost playbookbak]# ls
     roles       useroles.yaml
[root@localhost playbookbak]# ansible-playbook useroles.yaml

[root@localhost roles]# ls
    httpd     memcache      mysql     nginx
[root@localhost nginx]# ls
   files      tasks      templates
[root@localhost tasks]# ls
   group.yaml      main.yaml    restart.yaml     start.yaml     templ.yaml     user.yaml      yum.yaml 

 

---
- hosts: webservers
  remote_user: root
  roles:
    - role: nginx
useroles.yaml
- include: group.yaml
- include: user.yaml
- include: yum.yaml
- include: templ.yaml
- include: start.yaml
main.yaml
- name: add group
  group: name=nginx gid=80
group.yaml
- name: create user
  user: name=nginx uid=80 system=yes shell=/sbin/nologin
user.yaml
- name: install nginx
  yum: name=nginx
yum.yaml
- name: copy conf
  template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
templ.yaml
- name: start nginx
  service: name=nginx state=started enabled=yes
start.yaml
---
- hosts: webservers
  remote_user: root
  roles:
    - role: nginx
    - role: mysql
同时执行多个角色
- include: group.yaml
- include: user.yaml
- include: yum.yaml
- include: templ.yaml
- include: start.yaml
- include: /roles/mysql/tasks/copyfile.yaml
跨角色调用main.yaml
 角色标签

      roles:   

           - { role: nginx ,tags: [ 'nginx', 'web' ] ,when: ansible_distribution_major_version == "6" }     

           - { role: httpd ,tags: [ 'httpd', 'web' ]  }   

           - { role: mysql ,tags: [ 'mysql', 'db' ] }   

           - { role: marridb ,tags: [ 'mysql', 'db' ] }   

           - { role: php } 

   标签的调用方法(相当于在角色列表中挑选某些个角色单独执行):

         ansible-playbook   --tags="nginx,httpd,mysql"      nginx-role.yml 

 角色调用方法种类

 调用角色方法1:

         - hosts: websrvs

            remote_user: root

            roles:

                   - mysql

                   - memcached

                   - nginx

  调用角色方法2:   

        传递变量给角色   

           - hosts:       

              remote_user:       

             roles:

                    - mysql 

                   -  { role: nginx, username: nginx }          键role用于指定角色名称 后续的k/v用于传递变量给角色

   调用角色方法3:

          还可基于条件测试实现角色调用 

           roles: 

                - { role: nginx, username: nginx, when: ansible_distribution_major_version == '7'  } 

   handlers , tasks, vars  的入口文件都是通过main.yaml文件来执行的

模块源码分析

from ansible.module_utils.basic import AnsibleModule


def main():
    module = AnsibleModule(
        #如果没有传递-a选项参数,返回pong做为默认返回值
        argument_spec=dict(
            data=dict(type='str', default='pong'),
        ),
        supports_check_mode=True
    )
    #ansible all -m ping  -a "data=haha"
    #ansible all -m ping  -a "data=crash"
    #module.params['data']  接收的是 -a 后面的参数
    if module.params['data'] == 'crash':
        raise Exception("boom")

    result = dict(
        ping=module.params['data'],
    )

    module.exit_json(**result)
ping模块

猜你喜欢

转载自www.cnblogs.com/yxh168/p/9123455.html