zoukankan      html  css  js  c++  java
  • Ansible(自动化运维工具安装配置)

    一、环境

    主机 系统 角色
    192.168.10.200 centos-7 ansible管理
    192.168.10.210 centos-7 被管理主机
    192.168.10.211 centos-7 被管理主机
     1 [root@ansible ~]# hostnamectl 
     2    Static hostname: ansible
     3          Icon name: computer-vm
     4            Chassis: vm
     5         Machine ID: de646e2f538548169ed072b494dc78b2
     6            Boot ID: 1b88c107784b42b9aa456cf4d1e4120c
     7     Virtualization: vmware
     8   Operating System: CentOS Linux 7 (Core)
     9        CPE OS Name: cpe:/o:centos:centos:7
    10             Kernel: Linux 3.10.0-693.el7.x86_64
    11       Architecture: x86-64

    二、安装ansible

    安装ansible常用两种方式

    1、使用yum安装

    [root@ansible ~]# yum -y install ansible
    [root@ansible ~]# rpm -ql ansible
    /etc/ansible
    /etc/ansible/ansible.cfg
    /etc/ansible/hosts
    /etc/ansible/roles
    /usr/bin/ansible
    /usr/bin/ansible-2
    /usr/bin/ansible-2.7
    /usr/bin/ansible-config
    /usr/bin/ansible-connection
    /usr/bin/ansible-console
    /usr/bin/ansible-console-2
    /usr/bin/ansible-console-2.7
    /usr/bin/ansible-doc
    /usr/bin/ansible-doc-2
    /usr/bin/ansible-doc-2.7
    /usr/bin/ansible-galaxy
    /usr/bin/ansible-galaxy-2
    ...........................

    2、使用pip(python的包管理模块)安装

    [root@ansible ansible]# pip install ansible
    [root@ansible ansible]# pip list|grep ansible
    ansible             2.5.3  
    [root@ansible ansible]# pip show ansible
    Name: ansible
    Version: 2.5.3
    Summary: Radically simple IT automation
    Home-page: https://ansible.com/
    Author: Ansible, Inc.
    Author-email: info@ansible.com
    License: GPLv3+
    Location: /usr/lib/python2.7/site-packages
    Requires: jinja2, PyYAML, paramiko, cryptography, setuptools
    Required-by: 

    三、ansible配置

    /etc/ansible  
    /etc/ansible/ansible.cfg    #ansible配置文件
    /etc/ansible/hosts      #ansible主机清单
    /etc/ansible/roles    #ansible角色的目录
    /usr/bin/ansible    #ansible命令工具
    

    ansible.cfg文件

       [defaults]
        # some basic default values...
        hostfile       = /etc/ansible/hosts   \\指定默认hosts配置的位置
        # library_path = /usr/share/my_modules/
        remote_tmp     = $HOME/.ansible/tmp
        pattern        = *
        forks          = 5
        poll_interval  = 15
        sudo_user      = root  \\远程sudo用户
        #ask_sudo_pass = True  \\每次执行ansible命令是否询问ssh密码
        #ask_pass      = True  \\每次执行ansible命令时是否询问sudo密码
        transport      = smart
        remote_port    = 22
        module_lang    = C
        gathering = implicit
        host_key_checking = False    \\关闭第一次使用ansible连接客户端是输入命令提示
        log_path    = /var/log/ansible.log \\需要时可以自行添加。chown -R root:root ansible.log
        system_warnings = False    \\关闭运行ansible时系统的提示信息,一般为提示升级
        # set plugin path directories here, separate with colons
        action_plugins     = /usr/share/ansible_plugins/action_plugins
        callback_plugins   = /usr/share/ansible_plugins/callback_plugins
        connection_plugins = /usr/share/ansible_plugins/connection_plugins
        lookup_plugins     = /usr/share/ansible_plugins/lookup_plugins
        vars_plugins       = /usr/share/ansible_plugins/vars_plugins
        filter_plugins     = /usr/share/ansible_plugins/filter_plugins
        fact_caching = memory
        [accelerate]
        accelerate_port = 5099
        accelerate_timeout = 30
        accelerate_connect_timeout = 5.0
        # The daemon timeout is measured in minutes. This time is measured
        # from the last activity to the accelerate daemon.
        accelerate_daemon_timeout = 30

    hosts主机清单

    
    主机和主机组
    192.168.10.200 #定义主机 [ansible] #定义主机组 192.168.10.200 [web_server] 192.168.10.210 192.168.64.211
    ----------------------------------------------------------------------
    端口和别名
    192.168.64.211:222
    ansible_server ansible_ssh_port = 3333 ansible_ssh_host = 192.168.10.200    
    -----------------------------------------------------------------------
    指定主机范围
    [servers]
    192.168.10.[100:200]

    使用主机变量

    ansible_ssh_host     #用于指定被管理的主机的真实IP
    ansible_ssh_port     #用于指定连接到被管理主机的ssh端口号,默认是22
    ansible_ssh_user     #ssh连接时默认使用的用户名
    ansible_ssh_pass     #ssh连接时的密码
    ansible_sudo_pass     #使用sudo连接用户时的密码
    ansible_sudo_exec     #如果sudo命令不在默认路径,需要指定sudo命令路径
    ansible_ssh_private_key_file     #秘钥文件路径,秘钥文件如果不想使用ssh-agent管理时可以使用此选项
    ansible_shell_type     #目标系统的shell的类型,默认sh
    ansible_connection     #SSH 连接的类型: local , ssh , paramiko,在 ansible 1.2 之前默认是 paramiko ,后来智能选择,优先使用基于 ControlPersist 的 ssh (支持的前提)
    ansible_python_interpreter     #用来指定python解释器的路径,默认为/usr/bin/python 同样可以指定ruby 、perl 的路径
    ansible_*_interpreter     #其他解释器路径,用法与ansible_python_interpreter类似,这里"*"可以是ruby或才perl等其他语言

    主机配置:使用用户和密码及端口配置

    [web]
    192.168.10.210 ansible_ssh_user=root ansible_ssh_pass='123456'
    192.168.10.210 ansible_ssh_user=ops ansible_ssh_pass='123456'
    192.168.10.211 ansible_ssh_user=manager ansible_ssh_port=3055 ansible_ssh_pass='456789'

    主机与组正则匹配部分

    1、表示所有的主机可以使用all 或 * 
    2、通配符与逻辑或
    
    server.web.com
    192.168.10.*
    
    *.web.com
    *.com
    webservers1[0]     #表示匹配 webservers1 组的第 1 个主机    webservers1[0:25]  #表示匹配 webservers1 组的第 1 个到第 25 个主机

    四、ssh主机信任密钥配置

    [root@ansible ~]# ssh-keygen -t dsa
    Generating public/private dsa key pair.
    Enter file in which to save the key (/root/.ssh/id_dsa): 
    Enter passphrase (empty for no passphrase): 
    Enter same passphrase again: 
    Your identification has been saved in /root/.ssh/id_dsa.
    Your public key has been saved in /root/.ssh/id_dsa.pub.
    The key fingerprint is:
    SHA256:2mJq8FNVETmkzZsCaeZcxNgutl5FDUtuDXUUI58KKWE root@ansible
    The key's randomart image is:
    +---[DSA 1024]----+
    |       +E.BBo.=. |
    |      .+oB==.+ o |
    |      =.+oO.. o  |
    |     =oooo.+ .   |
    |     .o+S.o .    |
    |  .   oo..       |
    |   o o+..        |
    |    +o..         |
    |   ...           |
    +----[SHA256]-----+
    
    
    [root@ansible
    ~]# ssh-copy-id -i .ssh/id_dsa.pub root@192.168.10.210
    [root@ansible ~]# ssh-copy-id -i .ssh/id_dsa.pub root@192.168.10.211
    [root@ansible ~]# ssh-copy-id -i .ssh/id_dsa.pub root@192.168.10.200  #ansible管理
    主机多可以使用脚本利用sshpass批量发送

    测试主机清单配置

    [root@ansible ~]# cat /etc/ansible/hosts
    [ansible]
    192.168.10.200
    [web]
    192.168.10.210
    192.168.10.211

    五、简单命令测试

    [root@ansible ~]# ansible all -m shell -a "date"
    192.168.64.129 | SUCCESS | rc=0 >>
    2018年 05月 31日 星期四 08:43:02 EDT
    
    192.168.64.131 | SUCCESS | rc=0 >>
    2018年 05月 31日 星期四 20:43:03 CST
    
    192.168.64.128 | SUCCESS | rc=0 >>
    2018年 05月 31日 星期四 20:43:04 CST

    [root@ansible ~]# ansible all -m shell -a "df -h"
    192.168.64.131 | SUCCESS | rc=0 >>
    文件系统 容量 已用 可用 已用% 挂载点
    /dev/mapper/centos-root 17G 2.2G 15G 13% /
    devtmpfs 478M 0 478M 0% /dev
    tmpfs 489M 0 489M 0% /dev/shm
    tmpfs 489M 13M 476M 3% /run
    tmpfs 489M 0 489M 0% /sys/fs/cgroup
    /dev/sda1 1014M 134M 881M 14% /boot
    tmpfs 98M 0 98M 0% /run/user/0

    192.168.64.129 | SUCCESS | rc=0 >>
    文件系统 容量 已用 可用 已用% 挂载点
    /dev/mapper/centos-root 17G 4.7G 13G 28% /
    devtmpfs 478M 0 478M 0% /dev
    tmpfs 489M 0 489M 0% /dev/shm
    tmpfs 489M 6.8M 482M 2% /run
    tmpfs 489M 0 489M 0% /sys/fs/cgroup
    /dev/sda1 1014M 143M 872M 15% /boot
    tmpfs 98M 0 98M 0% /run/user/0

    192.168.64.128 | SUCCESS | rc=0 >>
    文件系统 容量 已用 可用 已用% 挂载点
    /dev/mapper/centos-root 17G 15G 2.4G 87% /
    devtmpfs 478M 0 478M 0% /dev
    tmpfs 489M 160K 488M 1% /dev/shm
    tmpfs 489M 26M 463M 6% /run
    tmpfs 489M 0 489M 0% /sys/fs/cgroup
    /dev/sda1 1014M 143M 872M 15% /boot
    tmpfs 98M 0 98M 0% /run/user/0

    
    

    [root@ansible ~]# ansible all -m shell -a "ss -tunl|grep 22"
    192.168.64.129 | SUCCESS | rc=0 >>
    tcp LISTEN 0 128 *:22 *:*
    tcp LISTEN 0 128 :::22 :::*

    
    

    192.168.64.131 | SUCCESS | rc=0 >>
    tcp LISTEN 0 128 *:22 *:*
    tcp LISTEN 0 128 :::22 :::*

    
    

    192.168.64.128 | SUCCESS | rc=0 >>
    tcp LISTEN 0 128 *:22 *:*
    tcp LISTEN 0 128 :::22 :::*

  • 相关阅读:
    Ajax ToolKit ModelPopupExtender报错:Sys.InvalidOperationException: Handler was not added through the Sys.UI.DomE
    C# 注释 类头部
    matlab练习程序(图像滤波时的边界处理2)
    更改chrome默认搜索引擎
    matlab练习程序(表面模糊)
    matlab练习程序(三阶张量TQR分解)
    linux命令行关机
    matlab练习程序(奇异值分解压缩图像)
    matlab练习程序(方框模糊)
    matlab练习程序(图像滤波时的边界处理)
  • 原文地址:https://www.cnblogs.com/51wansheng/p/9118281.html
Copyright © 2011-2022 走看看