自动化工具之ansible

发布于:2023-01-13 ⋅ 阅读:(803) ⋅ 点赞:(0)

Ansible:

Configuration、Command and Control

是什么 ? SSH-based configuration management, deployment, and task execution system

运维工具的分类:

        agent:基于专用的agent程序完成管理功能,puppet, func, zabbix, ...

        agentless:基于ssh服务完成管理,ansible, fabric, ...

架构:

Ansible Core

Modules:

        Core Modules

        Customed Modules

Host Iventory

        Files

        CMDB

PlayBooks

        Hosts

        roles

Connection Plugins:

特性:

        模块化:调用特定的模块,完成特定的任务;

        基于Python语言研发,由Paramiko, PyYAML和Jinja2三个核心库实现;

        部署简单:agentless;

        支持自定义模块,使用任意编程语言;

        强大的playbook机制;

        幂等性;

安装及程序环境:

程序:

        ansible

        ansible-playbook 

        ansible-doc

配置文件:

        /etc/ansible/ansible.cfg

主机清单:

        /etc/ansible/hosts 

插件目录:

        /usr/share/ansible_plugins/

基本使用入门:

ansible命令:

        Usage: ansible <host-pattern> [options]

常用选项:

        -m MOD_NAME  -a MOD_ARGS

配置Host Inventory:

        /etc/ansible/hosts

        [group_id]

        HOST_PATTERN1

        HOST_PATTERN2

模块:

        获取模块列表:ansible-doc -l

        获取指定模块的使用帮助:ansible-doc -s MOD_NAME

常用模块:

ping:探测目标主机是否存活;

command:在远程主机执行命令;

shell:在远程主机上调用shell解释器运行命令,支持shell的各种功能,例如管道等 ;

注意:command和shell模块的核心参数直接为命令本身;而其它模块的参数通常为“key=value”格式;

copy: Copies   files   to   remote   locations .

用法:

        (1) 复制文件

                -a "src=  dest=  "

        (2) 给定内容生成文件

                -a "content=  dest=  "

其它参数:mode, owner, group, ...

 

file:Sets  attributes  of  files

用法:

        (1) 创建目录:

                -a "path=  state=directory"

        (2) 创建链接文件:

                -a "path=  src=  state=link"

        (3) 删除文件:

                -a "path=  state=absent“

fetch:Fetches  a  file  from  remote  nodes

cron:Manage  cron.d  and  crontab  entries .

-a ""
minute=
hour=
day=
month=
weekday=
job=
name=
user=

state={present|absent}

hostname:Manage  hostname

name=

yum:Manages packages with the I(yum) package manager

-a ""

        (1) name=  state={present|latest}

        (2) name=  state=absent

service:Manage services.

​​​​​​​-a ""
name=
state=
started
stopped
restarted
enabled=
runlevel=

group: Add or remove groups

-a ""
name=
state=
system=
gid=

user:Manage user accounts

-a ""
name=
group=
groups=
comment=
uid=
system=
shell=
expires=
home=

setup:Gathers facts about  remote hosts

YAML:

YAML is a data serialization format designed for human readability and  interaction with scripting languages.

数据结构:

        key:value

                - item1

                - item2

                - item3

        {name:jerry, age:21}

PlayBook:

核心元素:

        Tasks:任务,由模块定义的操作的列表;

        Variables:变量

        Templates:模板,即使用了模板语法的文本文件;

        Handlers:由特定条件触发的Tasks;

        Roles:角色;

playbook的基础组件:

        Hosts:运行指定任务的目标主机;

        remote_user:在远程主机以哪个用户身份执行;

                sudo_user:非管理员需要拥有sudo权限;

        tasks:任务列表

                模块,模块参数:

                格式:

                        (1) action: module arguments

                        (2) module: arguments

示例1:

- hosts: all
  remote_user: root
  tasks:
  - name: install a group
  group: name=mygrp system=true 
  - name: install a user
  user: name=user1 group=mygrp system=true
- hosts: websrvs
 remote_user: root
 tasks:
 - name: install httpd package
   yum: name=httpd
 - name: start httpd service 
   service: name=httpd state=started

运行playbook,使用ansible-playbook命令

(1) 检测语法

ansible-playbook  --syntax-check  /path/to/playbook.yaml

(2) 测试运行

ansible-playbook -C /path/to/playbook.yaml
    --list-hosts
    --list-tasks
    --list-tags

(3) 运行

ansible-playbook  /path/to/playbook.yaml
    -t TAGS, --tags=TAGS
    --skip-tags=SKIP_TAGS
    --start-at-task=START_AT

handlers:由特定条件触发的Tasks;

调用及定义方式:

tasks:

        - name: TASK_NAME

         module: arguments

         notify: HANDLER_NAME

handlers:

        - name: HANDLER_NAME

         module: arguments

示例:

- hosts: websrvs
  remote_user: root
  tasks:
  - name: install httpd package
    yum: name=httpd state=latest
    - name: install conf file
    copy: src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf
    notify: restart httpd service
 - name: start httpd service
  service: name=httpd state=started
  handlers:
  - name: restart httpd service
  service: name=httpd state=restarted

tags:给指定的任务定义一个调用标识;

        - name: NAME

         module: arguments

         tags: TAG_ID

Variables:

类型:

        内建:

                (1) facts

        自定义:

                (1) 命令行传递;

                        -e VAR=VALUE

(2) 在hosts Inventory中为每个主机定义专用变量值;

        (a) 向不同的主机传递不同的变量 ;

                IP/HOSTNAME variable_name=value

   

        (b) 向组内的所有主机传递相同的变量 ;

                [groupname:vars]

                variable_name=value

  

(3) 在playbook中定义

        vars:

                - var_name: value

                - var_name: value

  

(4) Inventory还可以使用参数:

用于定义ansible远程连接目标主机时使用的属性,而非传递给playbook的变量;

        ansible_ssh_host

        ansible_ssh_port

        ansible_ssh_user

        ansible_ssh_pass

        ansible_sudo_pass

        ...

(5) 在角色调用时传递

roles:

- { role: ROLE_NAME, var: value, ...}

变量调用:

{{ var_name }}

Templates:模板

文本文件,内部嵌套有模板语言脚本(使用模板语言编写)

Jinja2 is a template engine written in pure Python.  It provides a  Django inspired non-XML syntax but supports inline expressions and an optional sandboxed environment.

语法:

字面量:

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

        数字:整数、浮点数;

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

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

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

        布尔型:true/false

算术运算:

        +, -, *, /, //, %, **

比较操作:

        ==, !=, >, <, >=, <=

逻辑运算:and, or, not 

执行模板文件中的脚本,并生成结果数据流,需要使用template模块;

template:

-a ""
     src=
     dest=
     mode=
     onwer=
     group=

注意:此模板不能在命令行使用,而只能用于playbook;

示例:

- hosts: ngxsrvs
  remote_user: root
  tasks:
  - name: install nginx package
  yum: name=nginx state=latest
  - name: install conf file
  template: src=/root/nginx.conf.j2 dest=/etc/nginx/nginx.conf
  tags: ngxconf
  notify: reload nginx service
  - name: start nginx service
  service: name=nginx state=started enabled=true
  handlers:
  - name: reload nginx service
  shell: /usr/sbin/nginx -s reload

条件测试:

when语句:在tasks中使用,Jinja2的语法格式;

- hosts: all
  remote_user: root
  tasks:
  - name: install nginx package
  yum: name=nginx state=latest
  - name: start nginx service on CentOS6
  shell: service nginx start
  when: ansible_distribution == "CentOS" and ansible_distribution_major_version == "6"
  - name: start nginx service
  shell: systemctl start nginx.service
  when: ansible_distribution == "CentOS" and ansible_distribution_major_version == "7"

循环:迭代,需要重复执行的任务;

对迭代项的引用,固定变量名为"item”,使用with_item属性给定要迭代的元素;

元素:列表

        字符串

        字典

基于字符串列表给出元素示例:

-hosts: websrvs
 remote_user: root
 tasks:
 - name: install packages
  yum: name={{ item }} state=latest
  with_items:
  - httpd
  - php
  - php-mysql
  - php-mbstring
  - php-gd

 

基于字典列表给元素示例:

- hosts: all
  remote_user: root
  tasks:
  - name: create groups
  group: name={{ item }} state=present
  with_items:
  - groupx1
  - groupx2
  - groupx3
  - name: create users
  user: name={{ item.name }} group={{ item.group }} state=present
  with_items:
  - {name: 'userx1', group: 'groupx1'}
  - {name: 'userx2', group: 'groupx2'}
  - {name: 'userx3', group: 'groupx3'}

角色:roles

以特定的层级目录结构进行组织的tasks、variables、handlers、templates、files等;

role_name/

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

        tasks/:此目录中至少应该有一个名为main.yml的文件,用于定义各task;其它的文件需要由main.yml进行“包含”调用;

        handlers/:此目录中至少应该有一个名为main.yml的文件,用于定义各handler;其它的文件需要由main.yml进行“包含”调用;

        vars/:此目录中至少应该有一个名为main.yml的文件,用于定义各variable;其它的文件需要由main.yml进行“包含”调用;

        templates/:存储由template模块调用的模板文本;

        meta/:此目录中至少应该有一个名为main.yml的文件,定义当前角色的特殊设定及其依赖关系;其它的文件需要由main.yml进行“包含”调用;

        default/:此目录中至少应该有一个名为main.yml的文件,用于设定默认变量;

在playbook中调用角色的方法:

- hosts: HOSTS
  remote_user: USERNAME
  roles:
  - ROLE1
  - ROLE2
  - { role: ROLE3, VARIABLE: VALUE, ...}
  - { role: ROLE4, when: CONDITION }

  

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到