美文网首页
ansible入门

ansible入门

作者: PC_Repair | 来源:发表于2020-08-19 22:03 被阅读0次

    ansible 是一款自动化运维工具,由Red Hat公司出品的,能够解决我们在it工作中,一遍又一遍执行相同任务。利用它,我们可以只解决一次问题,然后自动化运行我们的解决方案。 目前,数以千计的公司正在使用简单但功能强大的it自动化引擎,我相信它可以帮我们加速完成DevOps计划。

    特性:
    • 模块化:调用特定的模块,完成特定的任务。
    • 有 Paramiko、PyYAML、Jinjia2(模板语言)三个关键模块。
    • 支持自定义模块。
    • 基于 Python 语言实现。
    • 部署简单,基于 python 和 SSH(默认已安装),agentless。
    • 安全,基于 OpenSSH。
    • 支持 playbook 编排任务。
    • 幂等性:一个任务执行1遍和执行n遍效果一样,不因重复执行带来意外情况。
    • 无需代理不依赖PKI(无需 ssl)
    • 可使用任何编程语言写模块。
    • YAML格式,编排任务,支持丰富的数据结构。
    • 较强大的多层解决方案。
    框架结构:
    ansible架构.png
    安装(此处为pip安装):
    # pip 升级
    ➜  ~ sudo pip3 install --upgrade pip
    # 安装 ansible
    ➜  ~ pip3 install ansible
    # 安装指定版本的 ansible
    ➜  ~ pip3 install ansible==2.1.1
    # 查看 pip 安装 ansible 的详情
    ➜  ~ pip3 show ansible
    Name: ansible
    Version: 2.9.10
    Summary: Radically simple IT automation
    Home-page: https://ansible.com/
    Author: Ansible, Inc.
    Author-email: info@ansible.com
    License: GPLv3+
    Location: /Users/jiaflu/Library/Python/3.7/lib/python/site-packages
    Requires: PyYAML, cryptography, jinja2
    Required-by: 
    # 为 ansible 建立软链接
    ➜  ~ ln -s /Users/jiaflu/Library/Python/3.7/bin/ansible /usr/local/bin/
    # 验证 ansible 是否安装成功
    ➜  ~ ansible --version 
    ansible 2.9.10
      config file = None
      configured module search path = ['/Users/jiaflu/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
      ansible python module location = /Users/jiaflu/Library/Python/3.7/lib/python/site-packages/ansible
      executable location = /usr/local/bin/ansible
      python version = 3.7.3 (default, Apr 24 2020, 18:51:23) [Clang 11.0.3 (clang-1103.0.32.62)]
    
    链接方式:

    (1)基于密码连接

    [root@ansible ~]# vim /etc/ansible/hosts
    # 方法一 主机+端口+密码
    [webserver]
    192.168.1.31 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456"
    192.168.1.32 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456"
    192.168.1.33 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456"
    192.168.1.36 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456"
    
    
    # 方法二 主机+端口+密码
    [webserver]
    192.168.1.3[1:3] ansible_ssh_user=root ansible_ssh_pass="123456"
    
    
    # 方法二 主机+端口+密码
    [webserver]
    192.168.1.3[1:3]
    [webserver:vars]
    ansible_ssh_pass="123456"
    

    (2)基于秘钥连接

    基于秘钥连接需要先创建公钥和私钥,并发送给被管理机器。

    • 生成公私钥
    [root@ansible ~]# ssh-keygen
    [root@ansible ~]# for i in {1,2,3,6}; do ssh-copy-id -i 192.168.1.3$i ; done
    
    • 配置连接
    [root@ansible ~]# vim /etc/ansible/hosts
    # 方法一 主机+端口+密钥
    [webserver]
    192.168.1.31:22
    192.168.1.32
    192.168.1.33
    192.168.1.36
    
    # 方法一 别名主机+端口+密钥
    [webserver]
    node1 ansible_ssh_host=192.168.1.31 ansible_ssh_port=22
    node2 ansible_ssh_host=192.168.1.32 ansible_ssh_port=22
    node3 ansible_ssh_host=192.168.1.33 ansible_ssh_port=22
    node6 ansible_ssh_host=192.168.1.36 ansible_ssh_port=22
    
    简单使用
    • 列出文件列表
    [root@localhost ~]# ansible test -a "ls "
    10.225.20.231 | CHANGED | rc=0 >>
    password
    
    10.225.20.237 | CHANGED | rc=0 >>
    password
    
    • 测试机器是否通
    [root@localhost ~]# ansible test -m ping
    10.225.20.237 | SUCCESS => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": false, 
        "ping": "pong"
    }
    10.225.20.231 | SUCCESS => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": false, 
        "ping": "pong"
    }
    
    • 其他命令
    # 执行远程命令
    # ansible test -m command -a 'uptime'
    
    # 执行主控端脚本
    # ansible test -m script -a '/etc/ansible/script/test.sh'
    
    # 执行远程主机的脚本
    # ansible test -m shell -a 'ps aux|grep zabbix'
    
    # 类似shell
    # ansible test -m raw -a "ps aux|grep zabbix|awk '{print \$2}'"
    
    # 创建软链接
    # ansible test -m file -a "src=/etc/resolv.conf dest=/tmp/resolv.conf state=link"
    
    # 删除软链接
    # ansible test -m file -a "path=/tmp/resolv.conf state=absent"
    
    # 复制文件到远程服务器
    # ansible test -m copy -a "src=/etc/ansible/ansible.cfg dest=/tmp/ansible.cfg owner=root group=root mode=0644"
    
    使用 playbook

    playbook 是一种与 adhoc 任务执行模式完全不同的方式,而且特别强大。 简单地说,playbook 是一个非常简单的配置管理和多机器部署系统的基础,不同于任何已经存在的配置系统,而且非常适合部署复杂应用程序。

    playbook的核心元素:

    • Hosts:主机

    • Tasks:任务列表

    • Variables

    • Templates:包含了模板语法的文本文件

    • Handlers:由特定条件出发的任务

    • 任务分为 service、command、shell等

    tasks:
      - name: make sure apache is running
        service: name=httpd state=started
        
    tasks:
      - name: enable selinux
        command: /sbin/setenforce 1
        
    tasks:
      - name: run this command and ignore the result
        shell: /usr/bin/somecommand || /bin/true
        
    tasks:
      - name: run this command and ignore the result
        shell: /usr/bin/somecommand
        ignore_errors: True
        
    tasks:
        - name: restart everything
          command: echo "this task will restart the web services"
          notify: "restart web services"
    
    • 例子:使用 playbook 安装 nginx

    写安装 nginx 的 sh 脚本 nginx-install.yml

    - hosts: test
      vars:
        http_port: 80
        max_clients: 200
      remote_user: root
      tasks:
      - name: copy nginx-install.sh to client
        copy: src=nginx-install.sh dest=/tmp/nginx-install.sh
      - name: chomd a+x
        shell: chmod +x /tmp/nginx-install.sh
      - name: install nginx
        shell: /tmp/nginx-install.sh
    

    执行 ansible-playbook

    [shaolei@localhost ~]# ansible-playbook nginx-install.yml 
    

    相关文章

      网友评论

          本文标题:ansible入门

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