其他分享
首页 > 其他分享> > playbook和roles

playbook和roles

作者:互联网

一、Playbook

playbook模块参考

- 自动部署 Nginx 案例
1、编写playbook
[root@slave-1 ansible]# vi nginx.yml
---
- hosts: webservers
  vars:
    hello: Ansible
  tasks:
  - name: 添加 YUM 仓库
    yum_repository:
      name: nginx
      description: nginx repo
      baseurl: http://nginx.org/packages/centos/7/$basearch/
      gpgcheck: no
      enabled: 1
  - name: 安装 Nginx
    yum:
      name: nginx
      state: latest
  - name: 拷贝虚拟主机文件
    copy:
      src: ./site.conf
      dest: /etc/nginx/conf.d/site.conf
  - name: 启动 Nginx 并设置开机启动
    systemd:
      name: nginx
      state: started
      enabled: yes
  - name: 创建网站根目录
    file:
      dest: /var/www/html
      state: directory
  - name: 创建首页
    shell: echo "hello {{hello}}" > /var/www/html/index.html

2、准备配置文件
[root@slave-1 ansible]# vi site.conf
server {
 listen 81;
 server_name localhost;
 location / {
 root /var/www/html;
 index index.html;
 } 
}

3、语法检查
[root@slave-1 ansible]# ansible-playbook nginx.yml --syntax-check

4、执行playbook
[root@slave-1 ansible]# ansible-playbook -i /etc/ansible/hosts nginx.yml

image

- 在变更时运行特定任务(handlers)

参考链接

- hosts: webservers
  tasks:
  - name: 拷贝虚拟主机文件
    copy:
     src: ./site.conf
     dest: /etc/nginx/conf.d/site.conf
    notify:
      - reload nginx
  - name: 确保 Nginx 在运行
    systemd:
      name: nginx
      state: started
 handlers:
 - name: reload nginx 
   systemd: 
     name: nginx
     state: reloaded
- 语法检查与 debug 调试

参考链接

ansible-playbook main.yml --syntax-check
- hosts: webservers
 tasks:
 - debug: msg="{{group_names}}"
- 任务控制(tags)

参考链接

- hosts: webservers
 gather_facts: no
 tasks:
 - name: 安装 Nginx
 yum: name=nginx state=present
 tags: install
 
 - name: 拷贝 Nginx 配置文件
 copy: src=nginx.conf dest=/etc/nginx/nginx.conf
 tags: config

ansible-playbook nginx.yml --tags "config,install"
ansible-playbook nginx.yml --skip-tags "install"
- Playbook 定义变量与使用

参考链接

1、在 Inventory 中定义
[groupname:vars]
variable_name=value

2、在 playbook 中定义
- hosts: webservers
  vars:
  - var_name: value
  tasks:
  - name: hello
    shell: "echo {{var_name}}"
 
3、在 Role 中定义
roles:
   - role: apache
     vars:
        http_port: 8080

4、register(注册变量)
- hosts: web_servers
  tasks:
  - shell: /usr/bin/foo
    register: foo_result
    ignore_errors: True
  - shell: /usr/bin/bar
    when: foo_result.rc == 5

5、facts(系统信息变量)
{{ ansible_hostname }}

6、内置主机变量
hostvars 主机变量
group_names 当前主机所在的组
groups 资产清单中所有组(和主机)的列表

- Playbook 流程控制
1、条件

参考链接

tasks:
  - command: echo {{ item }}
    loop: [ 0, 2, 4, 6, 8, 10 ]
    when: item > 5
2、循环

参考链接

- name: add several users
  user:
    name: "{{ item.name }}"
    state: present
    groups: "{{ item.groups }}"
  loop:
    - { name: 'testuser1', groups: 'wheel' }
    - { name: 'testuser2', groups: 'root' }
-自动部署tomcat
[root@slave-1 ansible]# cat tomcat.yml
---
- hosts: webservers
  gather_facts: no
  vars:
    tomcat_version: 8.5.75
    tomcat_install_dir: /usr/local

  tasks:
    - name: Install jdk1.8
      yum: name=java-1.8.0-openjdk state=present

    - name: Download tomcat
      get_url: url=https://dlcdn.apache.org/tomcat/tomcat-8/v{{ tomcat_version }}/src/apache-tomcat-{{ tomcat_version }}-src.tar.gz dest=/tmp validate_certs=no
      tags: download

    - name: Unarchive tomcat-{{ tomcat_version }}.tar.gz
      unarchive:
        src: /tmp/apache-tomcat-{{ tomcat_version }}-src.tar.gz
        dest: "{{ tomcat_install_dir }}"
        copy: no

    - name: Start tomcat
      shell: cd {{ tomcat_install_dir }} && mv apache-tomcat-{{ tomcat_version }}-src tomcat8 && cd tomcat8/bin && nohup ./startup.sh &
- Playbook 文件复用

参考链接

- import_playbook: webservers.yml
- import_playbook: databases.yml

# common_tasks.yml
- name: placeholder foo
  command: /bin/foo
- name: placeholder bar
  command: /bin/bar
tasks:
- import_tasks: common_tasks.yml
# or
- include_tasks: common_tasks.yml
- Playbook 模板(jinja2)
1、zabbix 监控新主机
# vi zabbix.yaml 
- hosts: webservers 
 tasks:
 - name: 安装
 yum: name=https://repo.zabbix.com/zabbix/4.4/rhel/7/x86_64/zabbix-agent-4.
4.4-1.el7.x86_64.rpm state=present
 - name: 下发配置文件
 template: src=zabbix_agentd.conf.j2 dest=/etc/zabbix/zabbix_agentd.conf
 - name: 启动
 systemd: name=zabbix-agent state=restarted enabled=yes daemon_reload=yes
 
# vi zabbix_agentd.conf.j2 
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
DebugLevel=3
Server=192.168.31.10
ListenPort=10050
ListenIP={{ ansible_eth0.ipv4.address }}
Hostname={{ ansible_eth0.ipv4.address }}
2、管理 Nginx 配置文件
- hosts: webservers
  gather_facts: no
  vars:
  http_port: 80
  server_name: www.****.cn
  tasks:
  - name: Copy nginx config file 
    template: src=site.conf.j2 dest=/usr/local/nginx/vhost/site.conf
    notify: reload nginx
  handlers:
  - name: reload nginx
    service: name=nginx state=reloaded

upstream webservers {
 {% for host in groups['webservers'] %}
 server {{ hostvars[host].inventory_hostname }}:80;
 {% endfor %}
}
server {
 listen {{ http_port }};
 server_name {{ server_name }};
 location / {
 proxy_pass http://webservers;
 } 
}
3、遍历字典
{% set dict={'aliang': '30'} %}
{% for key, value in dict.iteritems() %}
 {{key}} -> {{value}}
{% endfor %}

二、Roles(角色)

参考链接

示例参考

自动部署 Web 服务器
1、架构

image

2、工作流程

image

3、目录结构

image

标签:tasks,roles,tomcat,nginx,zabbix,ansible,playbook,name
来源: https://www.cnblogs.com/scau-zeng/p/15949383.html