Linux初学者学习笔记

20171118 Ansible

2017-11-26  本文已影响116人  哈喽别样
  • ansible介绍
  • ansible常用模块使用
  • playbook
  • templates,模板
  • 条件测试和循环迭代
  • roles,角色

一、ansible介绍

二、ansible常用模块使用

(一)ansible使用语法:

// 调用模块执行操作
ansible  HOST-PATTERN  -m MOD_NAME  -a  MOD_ARGS -f FORKS -C -u USERNAME -c CONNECTION
// 列出所有可以调用的模块
ansible-doc  -l
// 列出指定模块的使用方法
ansible-doc -s MOD_NAME

(二)command模块:在远程主机运行命令

(三)shell模块:在远程主机的shell进程下运行命令,支持shell特性,如管道等

(四)group模块:管理组账号

(五)user模块:管理用户账号

(六)copy模块: Copies files to remote locations

(七)fetch模块:Fetches a file from remote nodes

从远程节点获取文件

(八)file模块: Sets attributes of files

(九)get_url模块: Downloads files from HTTP, HTTPS, or FTP to node

(十)cron 模块:Manage cron.d and crontab entries.

(十一)hostname模块:Manage hostname

(十二)包管理相关模块

(1)yum模块:Manages packages with the 'yum' package manager
(2)pip模块:Manages Python library dependencies
(3)npm模块:Manage node.js packages with npm

(十三)service模块:管理服务

(十四)其他模块

(1)git模块:Deploy software (or files) from git checkouts
(2)deploy_helper模块:Manages some of the steps common in deploying projects
(3)haproxy模块:Enable, disable, and set weights for HAProxy backend servers using socket commands.

三、playbook

(一)YAML介绍:

(二)playbook的运用:

(1)playbook的核心元素:
(2)playbook的基础组件:

(三)变量:variables

(1)直接调用
(2)用户自定义变量:
(3)测试playbook:

(四)实验1:playbook实现基本的远程配置

(1)实现过程:
[websrvs]     // web服务器组,命名为websrvs
192.168.136.230
192.168.136.130

[dbsrvs]     // web服务器组,命名为dbsrvs
192.168.136.131
- hosts: websrvs
  remote_user: root
  tasks:
  - name: install nginx package     // 安装nginx服务
    yum: name=nginx state=latest
  - name: start nginx service       // 启动nginx服务
    service: name=nginx enabled=true state=started

- hosts: dbsrvs
  remote_user: root
  tasks:
  - name: install redis package     // 安装redis服务
    yum: name=redis state=latest
  - name: install conf file         // 复制redis配置文件至远程主机
    copy: src=/root/redis.conf dest=/etc/redis.conf owner=redis group=root mode=644     
    // 提前准备好redis配置文件
  - name: start redis service       // 启动redis服务
    service: name=redis state=started
ansible-playbook --list-hosts nginx.yaml    // 列出playbook中的主机
ansible-playbook --list-tasks nginx.yaml    // 列出playbook中的任务
ansible-playbook --syntax-check nginx.yaml  // 检查YAML文件的语法
ansible-playbook -C nginx.yaml              // 预测执行playbook可能发生的变化,但实际不执行
ansible-playbook nginx.yaml                 // 执行playbook
ansible websrvs -m setup                    // 查看执行playbook时收集的信息

列出playbook中的主机

列出playbook中的任务

预测执行playbook可能发生的变化

(2)缺陷分析:
vim nginx.yaml
- hosts: websrvs
  remote_user: root
  tasks:
  - name: install nginx package
    yum: name=nginx state=latest
  - name: start nginx service
    service: name=nginx enabled=true state=started

- hosts: dbsrvs
  remote_user: root
  tasks:
  - name: install redis package
    yum: name=redis state=latest
  - name: install conf file
    copy: src=/root/redis.conf dest=/etc/redis.conf owner=redis group=root mode=644
    tags: instconf                   // 对install conf file任务打标签
    notify: restart redis service    // 当配置文件变更时,才执行名为restart redis service的handler
  - name: start redis service
    service: name=redis state=started
  handlers:                          // 定义handler,由notify语句在某些情况下被触发
  - name: restart redis service
    service: name=redis state=restarted

可以看到执行结果中只执行了tag处的任务,并且触发执行名为restart redis service 的handler

可以看到只执行了标签instconf的任务,但由于配置没有改变,故没有触发执行名为restart redis service 的handler

(五)实验2:playbook中定义变量

- hosts: websrvs
  remote_user: root
  vars:
  - pkgname: tree
  tasks:
  - name: install package
    yum: name={{ pkgname }} state=latest
- hosts: websrvs
  remote_user: root
  vars:
  - pkgname: tree
  tasks:
  - name: install package {{ pkgname }}     // 做修改处
    yum: name={{ pkgname }} state=latest

四、templates,模板

(一)template介绍:

(二)template配置:

(三)实验3:实现自动安装redis,之后修改配置文件/etc/redis.conf的maxmemory值为主机总内存大小的一半,并且自动重启服务使配置生效

vim /root/redis_install_and_conf.yml
- hosts: dbsrvs
  remote_user: root
  tasks:
// 安装
  - name: install redis
    yum: name=redis state=latest
// 复制配置文件模板
  - name: install redis conf
    template: src=/root/redis.conf.j2 dest=/etc/redis.conf owner=redis group=root mode=644
    notify: restart redis service
    tags: install_conf
// 启动服务
  - name: start redis service
    service: name=redis state=started
// 条件触发任务
  handlers:
  - name: restart redis service
    service: name=redis state=restarted
cp /etc/redis.conf /root/redis.conf.j2
vim /root/redis.conf.j2
maxmemory {{ ansible_memtotal_mb /2  }}mb     // 添加此行,自动根据安装主机的内存容量设置
ansible-playbook -C /root/redis_install_and_conf.yml 
ansible-playbook /root/redis_install_and_conf.yml 

可以看到install redis conf任务成功执行

查看被安装的主机上的/etc/redis.conf文件,maxmemory值根据主机内存情况动态设置

五、条件测试和循环迭代:

(一)条件测试:when

tasks: 
- name: install conf file to centos7
  template: src=/etc/nginx.conf.c7.j2 dest=/etc/nginx.conf owner=root group=root
  when: ansible_distribution_major_version == "7"
- name: install conf file to centos6
  template: src=/etc/nginx.conf.c6.j2 dest=/etc/nginx.conf owner=root group=root
  when: ansible_distribution_major_version == "6"

(二)循环:迭代,需要重复执行的任务

// 字符串列表方法                                  
- name: install some packages
  yum: name={{ item }} state=present
  with_items:
  - nginx
  - memcached
  - php-fpm
              
- name: add some groups
  group: name={{ item }} state=present
  with_items:
  - group11
  - group12
  - group13

// 字典列表方法
- name: add some users
  user: name={{ item.name }} group={{ item.group }} state=present
  with_items:
  - { name: 'user11', group: 'group11' }
  - { name: 'user12', group: 'group12' }
  - { name: 'user13', group: 'group13' }

六、roles,角色

(一)角色的目录结构

(二)playbook中角色的调用

- hosts: websrvs
  remote_user: root
  roles:
  - mysql
  - nginx
- hosts: 
  remote_user: root
  roles:
  - { role: nginx, username: nginx }
- hosts: 
  remote_user: root
  roles:
  - { role: nginx, when: "ansible_distribution_major_version == '7' " }

(三)实验4:

mkdir /etc/ansible/roles/nginx/{tasks,handlers,vars,files,templates} -pv
cd /etc/ansible/roles
vim nginx/tasks/main.yml
- name: install nginx package          // 安装nginx
  yum: name=nginx state=latest
- name: install conf file              // 复制配置模板文件
  template: src=web.conf.j2 dest=/etc/nginx/conf.d/web.conf
  notify: reload nginx service         // 触发服务重载
  tags: instconf
- name: create doc root                // 建立网页文件根目录
  file: path={{ ngx_doc_root }} state=directory
  tags: instconf
- name: start nginx service            // 启动nginx服务
  service: name=nginx enabled=true state=started
vim nginx/templates/web.conf.j2
server {
        listen {{ ngx_server_port }};
        server_name {{ ngx_server_name }};
        location / {
                root {{ ngx_doc_root }};
        }
}
vim nginx/vars/main.yml
ngx_server_port: 80
ngx_server_name: www.hellopeiyang.com
ngx_doc_root: /app/webdata
vim nginx/handlers/main.yml
- name: reload nginx service
  service: name=nginx state=reloaded
vim /root/mywebsrvs.yml
- hosts: websrvs
  remote_user: root
  roles:
  - nginx
ansible-playbook -C /root/mywebsrvs.yml
ansible-playbook /root/mywebsrvs.yml
vim /root/mywebsrvs.yml
- hosts: websrvs
  remote_user: root
  roles:
  - { role: nginx, ngx_server_port: 8090 }   // 指定传入的角色,变量名:变量值
ansible-playbook -t instconf /root/mywebsrvs.yml
上一篇 下一篇

猜你喜欢

热点阅读