Ansible

作者: 靖流 | 来源:发表于2018-11-13 15:48 被阅读0次

    优点

    一款自动运维工具,实现批量系统配置,批量程序部署,批量运行命令。且备操作的机器无需安装客户端。

    重点

    • 安装基本概念
    • playbook(剧本),批量执行大量人物

    安装

    部署客户机和控制端的本地解析

    vim /etc/hosts
    #编辑完成后将文件传给所有的机器
    scp /etc/hosts ipaddress:/etc
    

    yum 安装ansible管理端

    配置

    • /etc/hosts解析过的名字放入到ansible的hosts
      /etc/ansible/hosts
      
    • 测试连通性 ansible localhost -m ping
    • 简洁输出参数 -o 使输出的信息放在一行显示
    • 测试主机(ansible的本质是检测ssh能否连接,并不是真的在ping目标机器)

    组和密码

    组即是在/etc/ansible/hosts/中,想要放入一个组的几个客户机名字前写入一行[],在中括号内键入组名。

    组中可以省略写入的行数,如果客户端的机器是有一部分相同(以host为例)且后面的内容按照数字顺序排列的,则可以使用host[1:n]来表示.
    如果这一批的账号密码相同的话,以这种方式省略显示时,可以在一段的后面进行批量账密设置:

    ansible_ssh_user='*'
    ansible_ssh_pass='*'
    ansible_ssh_port='*'
    

    也可以在组的后面使用一下书写方式同意给予组的变量:

    [group:vars]
    ansible_ssh_user='*'
    ansible_ssh_pass='*'
    ansible_ssh_port='*'
    

    组嵌套,子组和父组的设置:

    [web1]
    
    [web2]
    
    [webserver:children]
    web1
    web2
    

    Ad-Hoc点对点模式 模块化使用

    信息收集模块 setup

    ansible host3 -m setup -a 'filter = ***'
    #不使用filter时会显示全部的信息
    

    文件模块 file

    ansible host3 -m file -a 'path=/tmp/text state=touch mode=777'
    #创建文件
    ansible host3 -m file -a 'path=/tmp/dir state=directory mode=777'
    #创建目录
    

    软件包管理 yum

    ansible host3 -m yum -a 'name="httpd" state=latest'
    

    服务模式 service

    ansible host3 -m service -a 'name="httpd" state=started enble=yes'
    #state中使用过去式  enble确认开机启动状态
    

    用户模式user

    ansible host3 -m user -a 'name=* state=present'
    #添加用户
    ansible host3 -m user -a 'name=* state=absent'
    #删除用户
    echo 1 | openssl passwd -1 stdin
    ansible -m user -a 'name=* password=* '
    #添加密码时要先使用加密之后才能使用,此处使用1作为密码
    ansible webserver -m user -a 'name=* shell=/sbin/noglogin append=yes'
    #控制登录的shell
    

    复制模块copy

    ansible -m copy -a 'src=*  dest=*  owner=*  group=*  mode=*  backup=*'
    #src源文件,dest目标文件,backup为是否备份,如果等于yes,如果两个文件完全相同不会有操作,但是略有不同则会创建一个新文件
    

    Shell模式

    ansible -m shell -a ' '
    #参数中可以输入任何的shell命令,但是打开如vim等软件的操作会不好使
    

    Role-角色扮演

    YAML 语言使用

    在ansible中,playbook使用yaml语言进行编写,实现对集群的大批量更改和大量命令一起顺序执行,其书写的yaml语言格式如下

    -hosts: webservers    #1
      tasts:              #2
      - name: install apache    #3
        yum: name=httpd state=present
      - name: transport conf
        copy: src=./httpd.conf dest=/etc/httpd/conf/httpd.conf
      - name: start apache
        service: name=httpd state=started
    
    #1 第一行是一对特殊的k-v对,用来确定备操作的机器
    #2 tasts,命令。执行的命令写于此字典内。前输入两个空格
    #3 -引导k-v对,每一个冒号后都要有一个空格的存在
    

    此实验是使用apache作为典型,在执行playbook之前,先对apache的配置文件在当前目录下进行了想要的修改,然后再通过ansible来将其传递给想要操作的集群中的每一台机器。

    触发器handlers

    识别判断条件,在判断条件触发时,执行handlers内的操作

    -hosts: webservers
      tasts:
      - name: install apache
        yum: name=httpd state=present
      - name: transport
        copy: src=./httpd.conf dest=/etc/httpd/conf/httpd.conf
        notify: restart     #1
      - name: start apache
        service: name=httpd state=started
      handlers:
      - name: restart       #2
        service: name=httpd state=restarted
    
    #1此处为handlers的判断条件,关键词为notify,其后接的名字要和handlers内的#2处的name相同
    

    执行playbook

    ansible-playbook apache.yaml 
    #其后可接参数实现不同的操作
    ansible-playbook apache.yaml --syntax-check   #语法校验
    ansible-playbook apache.yaml --list-hosts     #列出备操作的主机
    ansible-playbook apache.yaml --list-tasks     #查看执行的任务
    

    Role模块化

    • 使用Role模块化可以使代码组织结构可读,代码复用,层次清晰.以下实验使用nginx作为模型,来进行操作。

    创建目录结构

    tree roles
    roles
    ├── nginx
    │   ├── files
    │   │   └── index.html
    │   ├── handlers
    │   │   └── main.yaml
    │   ├── tasks
    │   │   └── main.yaml
    │   ├── templates
    │   │   └── nginx.conf.j2
    │   └── vars
    │       └── main.yaml
    └── site.yaml
    

    使用Roles的模块化必须拥有如上的目录结构,可以通过如下命令创建

    mkdir roles/nginx/{files,handlers,tasks,templates,vars} -p
    touch roles/nginx/site.yaml roles/nginx/{handlers,tasks,vars}/main.yaml
    echo 'balabalabala' > roles/nginx/files/index.html
    #在本机有nginx服务的前提下
    cp /etc/nginx/nginx.conf roles/nginx/templates/nginx.conf.j2
    

    编写

    tasks/main.yaml中写入自己想要执行的命令的全部,如下

    ---     #1
    - name: install nginx
      yum: name={{ item }} state=latest   #2
      with_items:
      - epel-release
      - nginx
    
    - name: copy index.html
      copy: src=index.html dest=/usr/share/nginx/html/index.html
    
    - name: copy nginx.conf template
      template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf    #3
      notify: restart nginx
    
    - name: make nginx run
      service: name=nginx state=start enabled=yes
    
      #1 在role的模块化中,使用---作为开始。
      #2 在ansible中调用变量要使用{{ }}将变量引用起来。
      #3 在ansible中,若配置文件存在变量,在不同机器上运行时想得到不同的结果,则可用template来进行类复制操作。
    

    对template(模板) roles/nginx/templates/nginx.conf.j2中的两个参数添加变量:

    worker_process {{ ansible_processor_cores }}    #调用内部变量
    workdr_connections {{ worker_connections }}     #调用自定变量
    

    将自定义的变量写入 roles/nginx/vars/main.yaml

    worker_connection: 10240             #键值对形式存在
    

    将触发器写入到roles/nginx/handlers/main.yaml

    ---
    - name: restart nginx
      service: name=nginx state=restarted
    

    将需要操作的机器和roles名写入roles/site.yaml

    - hosts: host1
      roles:
      - nginx
    

    执行的时候是将上述的文件进行操作

    ansible-playbook site.yaml  --syntax-check    #语法测试
    ansible_playbook site.yaml
    

    相关文章

      网友评论

          本文标题:Ansible

          本文链接:https://www.haomeiwen.com/subject/gsdlfqtx.html