• 搜索
  • 夜间模式
    ©2015-2025  Ethan's Blog Theme by OneBlog
    搜索
    标签
    # Amule # Gargoyle # LUCI # VIM # Python # Nginx # 反代 # Ansible # Apache # LNMP
  • 首页>
  • 运维>
  • 正文
  • Ansible食用心得

    2022年01月23日 41 阅读 0 评论 6131 字

    Ansible是啥

    Red Hat® Ansible® Automation Platform is a foundation for building and operating automation across an organization. The platform includes all the tools needed to implement enterprise-wide automation.

    以上信息来自于Ansible官网,简单的说Ansible是redhat出品的一个用于自动化运维的工具,可以用于管理服务器、网络设备之类的,当然了仅限于Cisco、F5等知名产品,至于某些呵呵支不支持我就不知道了,反正官网毛都没提。

    安装Ansible

    鉴于安装很简单,一笔带过,RH系直接一条命令。

    yum -y install ansible

    开始前要了解的几个名词

    官网手册只有英文版,貌似有好心人翻译了中文版本但是我不记得地址多少了,而且相对来说版本较低,建议看官方版本手册;看不懂英文的建议使用Google Chrome浏览器,对没错,就是那个全世界最好用没有之一的浏览器进行浏览,可以全文英译中;在此我仅仅只对我自己肤浅的了解做一个总结,方便日后自己翻阅。

    inventory(仓库)

    inventory包含了你所需要自动化管理的服务器、设备,以IP或者域名标识,可以对其进行分组,其默认文件是/etc/ansible/hosts,该文件属于一个ini配置文件,目前可以使用号称更加符合人类阅读的yml文件,保存目录也是/etc/ansbile/,名字自定义,文件拓展名可以是yml、yaml、json或者是不带拓展名,你也可以在该目录下建立不同的文件夹用以区分不同的组,官方也是这样建议的,不同的组建议使用单独的一个仓库文件存储。

    hosts文件内容参考如下

    # 定义单台设备
    192.168.1.1
    abc.cn
    #定义一个组,名为group1,包含两台主机
    [group1]
    192.168.1.2
    bbc.cn
    #定义一个组,名为group2,并且我对其中IP为10.0.0.62的主机单独定义了ansible连接主机时使用的用户名及端口号
    [group2]
    192.168.1.3
    ccc.cn
    10.0.0.62 ansible_user=ubuntu ansible_port=222

    yml官方内容示例如下:

    all:
      hosts:
        mail.example.com:
      children:
        webservers:
          hosts:
            foo.example.com:
            bar.example.com:
        dbservers:
          hosts:
            one.example.com:
            two.example.com:
            three.example.com:

    这种结构层次分明,但是其实我觉得ini方式更加容易理解

    all:
      hosts:
      children:
        testgroup1:
          hosts:
            10.0.0.1:
        testgroup2:
          hosts:
            10.0.0.2:
            10.0.0.3:
        testgroup3:
          hosts:
            10.0.0.4:
              ansible_user:ubuntu
              ansible_port:9888

    tasks(任务)

    没啥好说的,你用ansible执行了一个命令就是做了一个任务

    playbook (剧本)

    顾名思义,如果说Ansible是一台电脑,那么playbook就是电脑上所运行的程序,你所需要实现的自动化操作,全都是由playbook来控制,playbook详细记录了你所要实现的任务名称,任务操作,要操作的对象等,Ansible必须按照你所编写的脚本表演,一个yaml的剧本里面可以包含多个剧情,一个剧情可以有多个任务。你所编写的playbook需要以yaml格式保存,放在哪里无所谓,只要你知道就行,其大概结构如下

    #剧本名称
    - name : change server password
    #剧本所需要操作的对象,这里是group1
      hosts: group1
      gather_facts: False
    #剧本任务
      tasks:
    #任务名称
        - name: chgpwd
    #具体操作,这里调用了chpass模块对系统用户进行密码修改
          user: name={{ item.name }} password={{ item.chpass | password_hash('sha512') }} update_password=always
          with_items:
              - { name: 'root', chpass: 'sdfsdfsdfds' }
    
    
    - name : change server1 password
      hosts: group2
      gather_facts: False
      tasks:
        - name: chgpwd
          user: name={{ item.name }} password={{ item.chpass | password_hash('sha512') }} update_password=always
          with_items:
              - { name: 'root', chpass: 'aabcc' }
              - { name: 'ubuntu', chpass: 'aabbdd' }
    

    Modules (模块)

    本来这块应该放前面讲,但是又要先知道playbook是啥玩意,所以放到这里来说,模块组成了Ansible的一部分,是Ansible的一个功能,比如其包含的shell模块、ping模块等;linux模块可以看这个,其他模块看这里
    例如我要实现所有的主机执行ping命令那么我可以使用如下命令

    ansible all -m ping

    执行一个命令

    ansible all -a "/bin/echo hello"

    执行一个shell命令

    ansible group1 -m shell -a "yum -y update"

    正式部署使用Ansbile

    知道Ansible大概的概念和相关调用方法后,我们就可以使用其进行自动化运维操作,这里我以批量修改主机密码为例,对我目前用到的东西进行一个描述。

    添加免密码登录主机

    Ansible管理节点生成公钥,上传到被管理节点

    ssh-keygen -t rsa
    ssh-copy-id root@10.0.0.1

    这里如果有大量的主机需要添加,我还写了一个shell脚本方便使用

    PATH=/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
    export PATH
    
    array_server=(
    10.0.0.1 \
    10.0.0.2 \
    )
    
    array_server1=(
     \
    )
    passwd="halksdjklsdjv"
    
    #ssh-keygen -t rsa
    echo "Start adding server..."
    
    for rhel in `cat /root/1.txt`
    do
        ping ${rhel} -c 1 > /dev/null 2>&1
        if [ $? == 0 ];then
        expect <<-EOF
            spawn ssh-copy-id root@${rhel}
            expect {
                "yes/no" { send "yes\n";exp_continue }
                "password" { send "$passwd\n" }
            }
            expect eof
        EOF
        echo "${rhel} add success." >> /root/success.log 2>&1
        else
        echo "${rhel} Can't connect to server, skip." >> /root/failed.log 2>&1
        fi
    done
    
    for debian in `cat /root/2.txt`
    do
        ping ${debian} -c 1 > /dev/null 2>&1
        if [ $? == 0 ];then
            expect <<-EOF
                    spawn ssh-copy-id ubuntu@${Debian}
                    expect {
                            "yes/no" { send "yes\n";exp_continue }
                            "password" { send "$passwd\n" }
                    }
                    expect eof
            EOF
        echo "${debian} add success." >> /root/success.log 2>&1
        else
        echo "${debian} Can't connect to server, skip." >> /root/failed.log 2>&1
        fi
    done

    上面的脚本针对root账户和非root账户分别将服务器放到1.txt和2.txt就可以使用,以前使用的是数组,后面发现数组太麻烦,就改了改,代码没删没注释,懒得管了

    PATH=/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
    export PATH
    
    array_server=(
    10.0.0.41 \
    )
    
    array_server1=(
     \
    )
    
    user=$1
    passwd=$2
    
    #ssh-keygen -t rsa
    echo "Start adding server..."
    
    for rhel in `cat /root/1.txt`
    do
            ping ${rhel} -c 1 > /dev/null 2>&1
            if [ $? == 0 ];then
            expect <<-EOF
                    spawn ssh-copy-id $user@${rhel}
                    expect {
                            "yes/no" { send "yes\n";exp_continue }
                            "password" { send "$passwd\n" }
                    }
                    expect eof
            EOF
            echo "${rhel} add success." >> /root/success.log 2>&1
            else
            echo "${rhel} Can't connect to server, skip." >> /root/failed.log 2>&1
            fi
    done

    上面的代码需配合参数使用,用于自动填充的账户和密码,密码用双引号包含,避免特殊符号无法转义。

    生成仓库

    将需要自动运维的服务器设备放到yml文件或者hosts的ini文件中,并做好分组(建议),以便ansible引用。

    创建playbook

    编写需要自动化的脚本,例如修改密码的剧本,当然如果不使用剧本,你用ad-hoc命令也可以。

    - name : change testgroup password
      hosts: testhost
      gather_facts: False
      tasks:
        - name: chgpwd
          user: name={{ item.name }} password={{ item.chpass | password_hash('sha512') }} update_password=always
          with_items:
              - { name: 'root', chpass: 'sjskdjflsjf' }

    调用playbook完成自动运维

    假设inventory配置文件为/etc/Ansible/server.yml使用以下命令调用剧本完成自动化任务:

    ansible-playbook -i /etc/Ansible/server.yml  xxx.yml

    确认剧本执行结果

    执行完成后会有相应反馈,如果成功,则状态为ok,否则为就是失败

    本文著作权归作者 [ Ethan ] 享有,未经作者书面授权,禁止转载,封面图片来源于 [ 互联网 ] ,本文仅供个人学习、研究和欣赏使用。如有异议,请联系博主及时处理。
    Ansible
    — END —
    Copyright©2015-2025  All Rights Reserved.  Load:0.008 s
    Theme by OneBlog V3.6.3
    夜间模式

    开源不易,请尊重作者版权,保留基本的版权信息。