美文网首页
ansible实战教程(一)--安装与初步使用

ansible实战教程(一)--安装与初步使用

作者: Tang_Lyan | 来源:发表于2019-08-16 16:08 被阅读0次

    ansible实战教程(一)

    最近需要部署分布式的爬虫项目,所以挑选了分布式部署最好用的工具---Ansible,但是从网上教程来说,以及数据阅读来说,上手确实不快,所以在这写一些自己的使用以及理解;

    安装

    centos:

    yum -y install ansible
    

    pip:

    pip install ansible 
    

    使用步骤

    1、编写hosts,ansible中比较专业的叫法:Inventory(可管理服务器集合),其实就是编写hosts,让ansible可以找到服务器ip,以及一些连接参数;

    2、执行命令;

    大致完成以上步骤就可以用ansible,完成批量的命令处理了;在这我们使用的是ansible中ad-hoc命令;如果想看详细的文档,参照

    编写Hosts

    在ansible中,hosts是最重要的一部分;(不写,ansible哪知道你要糟蹋哪台机器)

    既然Hosts这么重要,那么ansible肯定有自己的寻找方式,如下:

    1、默认读取/etc/ansible/hosts文件;

    2、通过命令行参数-i指定hosts文件;

    3、通过ansible.cfg文件中inventory选项;

    那么开始编写:

    vim hosts

      1 [test]
      2 10.251.1.165 ansible_ssh_user=root ansible_ssh_pass=1
    

    那么我们开始执行下ping命令:

    [root@localhost ansible]# ansible -i hosts test -m ping
    10.251.1.165 | FAILED! => {
        "msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this.  Please add this host's fingerprint to your known_hosts file to manage this host."
    }
    

    发现报错;

    原因:需要安装sshpass(我两台机器都装了)

    执行成功:

    [root@localhost ansible]# ansible -i hosts test -m ping
    10.251.1.165 | SUCCESS => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        },
        "changed": false,
        "ping": "pong"
    }
    

    到此为止,我们的第一步最重要的步骤,已经完成了;

    关于inventory进阶的一些知识

    inventory作为可管理服务器集合,那么我们对服务器的一些配置,都可以在hosts中编写;

    一些配置包括如下:详细可参考

    • ansible_connection:这个选项我们可以修改连接的方式:ssh、smart、paramiko;
    • ansible_user:使用什么用户登录这个host
    • ansible_password:密码(也可以使用ansible_ssh_pass)
    • ansible_python_interpreter:目标主机的python路径,如果在使用ansible-playbook的时候可以使用;

    动态inventory

    ansible是支持动态的hosts的;但是我没用过,嫌麻烦。参考

    host分组玩法

    对于host编写时,我们需要对host进行分组,这里推荐一些写法:

    • 使用python的序列,节约时间
      1 [test]
      2 10.251.1.16[2:5] ansible_user=root ansible_password=1
    

    执行结果:

    [root@localhost ansible]# ansible -i hosts test -m ping
    10.251.1.162 | FAILED! => {
        "msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this.  Please add this host's fingerprint to your known_hosts file to manage this host."
    }
    10.251.1.164 | FAILED! => {
        "msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this.  Please add this host's fingerprint to your known_hosts file to manage this host."
    }
    10.251.1.163 | FAILED! => {
        "msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this.  Please add this host's fingerprint to your known_hosts file to manage this host."
    }
    10.251.1.165 | SUCCESS => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        },
        "changed": false,
        "ping": "pong"
    }
    

    报错,所以如果使用ssh_pass方式连接,那么每台机器上都需要安装sshpass;(之后介绍一个批量脚本)

    • 使用children分组
    [test]
    10.251.1.163
    
    [other]
    10.251.1.164
    
    [test:children]
    other
    

    执行结果:

    [root@localhost ansible]# ansible -i hosts test -m ping
    10.251.1.163 | SUCCESS => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        },
        "changed": false,
        "ping": "pong"
    }
    10.251.1.164 | SUCCESS => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        },
        "changed": false,
        "ping": "pong"
    }
    
    • 使用变量vars
    [test]
    10.251.1.163
    
    [other]
    10.251.1.164
    
    [test:children]
    other
    
    [test:vars]
    ansible_user=tly
    

    注:test组下都使用tly这个用户登录,这个用户不存在;

    [root@localhost ansible]# ansible -i hosts test -m ping
    10.251.1.163 | UNREACHABLE! => {
        "changed": false,
        "msg": "Failed to connect to the host via ssh: Permission denied (publickey,password).",
        "unreachable": true
    }
    10.251.1.164 | UNREACHABLE! => {
        "changed": false,
        "msg": "Failed to connect to the host via ssh: Permission denied (publickey,password).",
        "unreachable": true
    }
    

    所以以上报错;

    • 另外,介绍yaml配置文件的写法(上面的改写)
    all:
      children:
         test:
           hosts:
             10.251.1.163:
           children:
             other:
               hosts:
                 10.251.1.164
    

    我个人偏向使用ini,conf这种方式,比较好看;

    相关文章

      网友评论

          本文标题:ansible实战教程(一)--安装与初步使用

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