• ansible


    ansible批量管理工具的搭建与简单的操作

     

    ansible的安装

    复制代码
    #
    [root@localhost ~]# cat /etc/redhat-release 
    CentOS Linux release 7.5.1804 (Core) 
    [root@localhost ~]# uname -r
    3.10.0-862.3.3.el7.x86_64
    [root@localhost ~]# systemctl stop firewalld
    [root@localhost ~]# systemctl disable firewalld
    [root@localhost ~]# systemctl stop NetworkManager
    [root@localhost ~]# systemctl disable NetworkManager
    #通过yum源方式安装ansible
    [root@ansible ~]# yum -y install epel-release
    [root@ansible ~]# yum -y install ansible
    #通过Python的pip方式安装ansible
    [root@ansible ~]# yum -y install epel-release
    [root@ansible ~]# yum -y install python2-pip
    [root@ansible ~]# pip install ansible
    复制代码

    ssh的,安全策略

    复制代码
    备份:cp /etc/ssh/sshd_config{,.bak}
    
    [root@www ~]# cat -n /etc/ssh/sshd_config.bak | sed -n '17p;38p;43p;47p;65p;79p;115p'
        17  #Port 22                    #修改ssh连接端口
        38  #PermitRootLogin yes        #是否允许root账号远程登陆
        43  #PubkeyAuthentication yes   #是否开启公钥连接认证
        47  AuthorizedKeysFile  .ssh/authorized_keys    #公钥文件的放置位置
        65  PasswordAuthentication yes  #是否开启密码验证登陆
        79  GSSAPIAuthentication yes    #是否关闭GSSAPI认证
       115  #UseDNS yes                 #是否关闭DNS反向解析
    [root@www ~]# cat -n /etc/ssh/sshd_config | sed -n '17p;38p;43p;47p;65p;79p;115p'
        17  Port 22221                  #工作中需要设定到1万以上的端口,避免被扫描出来。
        38  PermitRootLogin yes         #如果不是超大规模的服务器,为了方便我们可以暂时开启root远程登录
        43  PubkeyAuthentication yes    #开启公钥认证模式
        47  AuthorizedKeysFile  .ssh/authorized_keys    #公钥放置位置
        65  PasswordAuthentication no   #为了安全我们关闭服务器的密码认证方式
        79  GSSAPIAuthentication no     #关闭GSSAPI认证,极大提高ssh连接速度
       115  UseDNS no                   #关闭DNS反向解析,极大提高ssh连接速度
    复制代码

    然后配置主机清单

    复制代码
    /etc/ansible/hosts文件中可以定义被管理主机,Ansible通过读取/etc/ansible/hosts文件内定义的主机清单批量做一些操作。比如定义一个nginx组,包含一台主机Web01,再定义一个apache组,包含另一台主机Web02.
    
    [root@ansible ~]# cat /etc/ansible/hosts
    [nginx]
    Web01 ansible_ssh_host=192.168.200.184
    Web02 ansible_ssh_host=192.168.200.185
    说明:
    ansible_ssh_host:被管理主机IP
    ansible_ssh_user:被管理主机用户名
    ansible_ssh_pass:被管理主机用户的登陆密码
    ansible_sudo_pass:被管理主机用户sudo时的密码
    复制代码
    复制代码
     Ansible服务器简单的综合安全管理策略
    #禁止非root用户查看Ansible管理服务器端/etc/hosts文件
    [root@ansible ~]# ll /etc/hosts
    -rw-r--r--. 1 root root 180 9月   9 00:38 /etc/hosts
    [root@ansible ~]# chmod 600 /etc/hosts
    #禁止非root用户查看Ansible的主机清单配置文件
    [root@ansible ~]# ll /etc/ansible/hosts
    -rw-r--r-- 1 root root 87 9月   9 21:59 /etc/ansible/hosts
    [root@ansible ~]# chmod 600 /etc/ansible/hosts
    复制代码
    [root@ansible ~]# /usr/local/python/bin/ansible-doc -l 查看总帮助 
    [root@ansible ~]# /usr/local/python/bin/ansible-doc -s shell 查看shell模块的帮助

    ansible的十大基础模块

    1.

    ping模块
    Ansible中使用ping模块来检测指定主机的连通性

    2.

    command模块
    在远程主机执行命令,不支持管道符和重定向等复杂命令,可完全被shell模块替代
    
    [root@ansible ~]# ansible Web01 -m command -a 'uptime'

    3.

    shell模块
    Ansible中的shell模块可以在被管理主机上运行命令,并支持像管道符重定向这样的复杂命令。
    
    #在Web01上创建用户yunjisuan,并非交互方式设定密码
    [root@ansible ~]# ansible Web01 -m shell -a 'useradd yunjisuan'

    4.

    复制代码
    cron模块
    Ansible中的cron模块用于定义任务计划。主要包括两种状态(state);
    
    crontab时间周期: 
    minute:分钟
    hour:小时
    day:日期
    month:月份
    weekday:周期
    crontab任务: 
    job:指明运行的命令是什么
    crontab任务描述: 
    name:定时任务描述(定时任务清除的依据)
    state状态: 
    present:表示添加(省略状态时默认使用);
    absent:表示移除;
    crontab任务的用户身份: 
    user:指定定时任务以哪个用户身份执行
    #添加定时任务计划,在所有被管理的主机里每十分钟输出hello字符串,定时任务描述为test cron job
    [root@ansible ~]# ansible all -m cron -a 'minute="*/10" job="/bin/echo hello" name="test cron job"'
    复制代码

    5.

     copy模块
    Ansible中的copy模块用于实现文件复制和批量下发文件。其中使用src来定义本地源文件路径;使用dest定义被管理主机文件路径;使用content则是使用指定信息内容来生成目标文件。
    ansible all -m copy -a 'src=/etc/hosts dest=/etc/hosts owner=root mode=640 backup=yes'

    6

    script模块
    Ansible中的script模块可以将本地脚本复制到被管理主机的内存中并运行,不会在被管理主机中留下脚本文件。
    ansible all -m script -a '/tmp/test.sh'

    7.

    复制代码
    yum模块
    利用yum模块安装软件包,虽然能被shell模块替代 
    但是用yum模块更显专业一些
    
    软件包名: 
    name:指定软件包的名字
    state状态: 
    present:安装软件包(默认就是这个)
    absent:卸载软件包
    #安装nmap软件包
    [root@ansible ~]# ansible all -m yum -a 'name=nmap'
    #卸载nmap软件包
    [root@ansible ~]# ansible all -m yum -a 'name=nmap state=absent'
    复制代码

    8.

    复制代码
    service模块
    利用service模块管理服务程序,虽然能被shell模块替代 
    但是用service模块更显专业一些
    
    服务名称: 
    name:指定服务的名字
    state状态: 
    started:启动服务
    stopped:停止服务
    restarted:重启服务
    reloaded:平滑重载
    enabled开机自启动: 
    true:设置开机自启动
    false:设置开启不启动
    #启动firewalld并设置开机自启动
    [root@ansible ~]# ansible Web01 -m service -a 'name=firewalld state=started enabled=true'
    #关闭firewalld并设置开机不启动
    [root@ansible ~]# ansible Web01 -m service -a 'name=firewalld state=stopped enabled=false'
    复制代码

    9.

    复制代码
    user模块
    用户管理模块。管理用户账号
    
    :指定用户名 
    name:指定操作的用户的名字
    :用户描述 
    comment:指定用户的描述信息
    :createhome:是否创建家目录
    :uid:指定用户的uid号
    :groups:指定用户的附加组(默认创建和用户名相同的组)
    :password:指定用户的密码
    :update_password:更新用户的密码
    :shell指定用户的登陆方式 
    /bin/bash:能登录系统
    /sbin/nologin:不能登录系统
    :home:指定用户的家目录路径
    :state状态: 
    present:创建用户(默认就是这个)
    absent:删除用户
    :remove:当指定state=absent时,确认是否删除用户家目录 
    true
    false
    #在Web02上创建一个普通用户yunjisuan,并设置用户的密码为123123
    [root@ansible ~]# ansible Web02 -m user -a 'name=yunjisuan comment="welcom to yunjisuan" uid=1066 groups=wheel password=123123 shell=/bin/bash home=/home/yunjisuan'
    复制代码

    密码是明文的需要下载个命令加密

    复制代码
    利用ansible的user模块状态用户时要注意在password参数的后边添加密文,否则不能登陆用户 
    通过Python的pip程序安装passlib即可为密码加密
    
    #安装Python2的pip工具,并通过pip工具安装Python的加密模块来给密码加密
    [root@ansible ~]# yum -y install epel-release
    [root@ansible ~]# yum -y install python2-pip
    [root@ansible ~]# pip install passlib
    #生成密文密码
    [root@ansible ~]# python -c "from passlib.hash import sha512_crypt;import getpass;print sha512_crypt.encrypt(getpass.getpass())"
    Password:       #输入你想要加密的密码
    $6$rounds=656000$Tw15COd8DLh/VS94$Mcmz/8CcjBKiEl0mYHcOQQCxEA5mz66EcGH2qXVk6o.Sm7FsRS.DsDVy6ET8iI6jDa045I94slZqWFwyYnRSW1          #加密后的密码
    复制代码

    10.

    setup模块
    Ansible中使用setup模块收集,查看被管理主机的facts(facts是Ansible采集被管理主机设备信息的一个功能)。每个被管理主机在接收并运行管理命令之前,都会将自己的相关信息(操作系统版本,IP地址等)发送给控制主机
    
    #查看远程主机的facts信息
    [root@ansible ~]# ansible Web01 -m setup | head
  • 相关阅读:
    【MySQL】Mysql模糊查询like提速优化
    mysql实现row_number()和row_number() over(partition by)
    mysql limit 分页优化
    js正则验证方法大全
    如何获取select下拉框中option选中的文本值
    js控制input框只能输入数字和一位小数点和小数点后面两位小数
    js页面 :函数名 is not defined
    算法名称 Alias Method
    解决mybatis报错Result Maps collection does not contain value for java.lang.Integer
    nginx的rewrite规则
  • 原文地址:https://www.cnblogs.com/A6666/p/9995586.html
Copyright © 2020-2023  润新知