ansible role的使用方法

内容纲要

本节是对ansible一个收尾,在本节中,介绍一下ansible role的使用方法。

什么是ansible role?

回顾一下前几节,我介绍了ansible的架构,配置文件,常用模块,playbook,变量使用以及任务控制等。通常我在写playbook中也是包含了上面的多个任务。如果对他们进行按文件夹分类,是不是对后期的维护更有利。这时候就产生了ansible role。

官方定义:他可以根据已知的文件结构自动加载相关的变量、文件、任务、任务控制和其他ansible程序。将内容按角色分组。可以轻松地重复使用他们并与其他用户共享。

ansible role目录结构

role有他自己固定的目录结构,以及文件夹的名字也是固定的。参考以下示例:

我们可以用galaxy创建一个默认固定好的role目录

[root@ansible-01 role]# ansible-galaxy init nginx
- Role nginx was created successfully
[root@ansible-01 role]# tree
.
└── nginx
    ├── defaults
    │   └── main.yml
    ├── files
    ├── handlers
    │   └── main.yml
    ├── meta
    │   └── main.yml
    ├── README.md
    ├── tasks
    │   └── main.yml
    ├── templates
    ├── tests
    │   ├── inventory
    │   └── test.yml
    └── vars
        └── main.yml

9 directories, 8 files

这里默认会创建出9个文件夹,并不是每一个文件夹都需要用到,根据实际的需求在对应的文件夹里创建yaml文件就可以了。一些文件夹中有main.yml文件,这个文件是控制一个文件夹中的其他yaml文件用的,在编写完某个目录的yaml文件后,可以在main.yml中进行对本文件夹中其他的yaml文件的调用。我就根据上面创建出来的模板来写一个role

编写后的目录结构
[root@ansible-01 data]# tree
.
├── installnginx.yml
└── nginx
├── defaults
│   └── main.yml
├── files
├── handlers
│   └── main.yml
├── meta
│   └── main.yml
├── README.md
├── tasks
│   └── main.yml
├── templates
│   ├── index.html.j2
│   └── nginx.conf.j2
└── vars
└── main.yml

8 directories, 9 files

示例:安装配置并启动一个nginx。

[root@ansible-01 data]# cat installnginx.yml 
- hosts: test
  remote_user: root
  gather_facts: true
  roles:
    - nginx

[root@ansible-01 nginx]# cat handlers/main.yml 
---
- name: restart nginx server
  service: name=nginx state=restarted

[root@ansible-01 nginx]# cat tasks/main.yml 
---
- name: Install Nginx
  yum: name=nginx state=present

- name: Copy nginx.conf file
  template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
  notify: restart nginx server

- name: Copy index.html file
  template: src=index.html.j2 dest=/usr/share/nginx/html/index.html
  notify: restart nginx server

[root@ansible-01 nginx]# cat templates/index.html.j2 
website: {{ ansible_default_ipv4.address }}.wenjiangun.com

[root@ansible-01 nginx]# cat templates/nginx.conf.j2 
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes {{ ansible_processor_vcpus }};
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2;
#        listen       [::]:443 ssl http2;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

部署role nginx

ansible role
ansible role

相关链接:ansible任务控制
https://docs.ansible.com/ansible/latest/user_guide/playbooks_reuse_roles.html
上一篇:(ansible任务控制语句使用方法)
https://www.wenjiangun.com/blog/1276/

spacer

Leave a reply

评论审核已启用。您的评论可能需要一段时间后才能被显示。

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据