运维自动化-Ansible

运维自动化-Ansible

1.准备环境
在使用ansible的时候,最好使用python2.6,,查看目前环境如下:

[root~]# python -V(python版本)
Python2.6.6
[root~]# cat /etc/redhat-release (操作系统版本)
CentOS release 7.10
服务器3台
12.16.1.5服务端
12.16.1.6客户端
12.16.1.7客户端

2.更换yum源
(这一步很重要,做不好你就惨了)

a.把默认yum源备份(可选)

[root~]# mkdir /opt/centos-yum.bak

[root~]# mv /etc/yum.repos.d/* /opt/centos-yum.bak/

b.下载yum源repo文件(对应自己的系统版本下载即可)

#yum路径

[root~]# cd /etc/yum.repos.d

模板文件:

下载新的CentOS-Base.repo 到/etc/yum.repos.d/

CentOS 5
[root~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repo
CentOS 6
[root~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
CentOS 7
[root~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

Yum地址访问404,新增dns域名地址
新增下面一条内容:

[root@RAC01 yum.repos.d]# vi /etc/resolv.conf 
nameserver 8.8.8.8
Nameserver 114.114.114.114

c.yum缓存

[root~]# yum clean all

[root~]# yum makecache        //把yum源缓存到本地,加快软件的搜索好安装速度
[root~]# yum update -y

D.如果Yum异常:
Error: Cannot retrieve metalink for repository: fedora. Please verify its path and try again

解决方法是:
[root~]# su -c “sed -i ‘s|^#baseurl|baseurl| ; s|^mirrorlist|#mirrorlist|’ /etc/yum.repos.d/*”

centos下安装完EPEL源然后更新一下yum缓存如果发现这样的错误:
Error: Cannot retrieve metalink for repository: epel. Please verify its path and try again
这就表明你需要更新CA证书了,那么只需要更新CA证书就可以,不过在此同时需要临时禁用epel源并更新就可以了,命令如下:
[root~]# yum --disablerepo=epel -y update ca-certificates

3.安装ansible

3.1第一种安装ansible

[root~]# yum install epel-release repolist ansible -y

#查看ansible版本
[root~]# ansible --version

3.2第二种安装ansible

#在服务器端:
#先安装epel源,下载路径https://fedoraproject.org/wiki/EPEL 找到epel-releaselatest-7下载
[root~]# yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
[root~]# yum install ansible -y

4.开启ansible日志记录

[root~]# sed -i 's/#log_path/log_path/' /etc/ansible/ansible.cfg

5.ansible设置
A.关闭防火墙
#CentOS 7-----将 SELINUX=enforcing 替换为SELINUX=disabled

[root~]# sed -i "s|SELINUX=enforcing|SELINUX=disabled|" /etc/selinux/config
# CentOS 7	开机不自动启动防火墙
[root~]# systemctl disable firewalld.service

# 关闭防火墙
[root~]# systemctl stop firewalld.service
[root~]# setenforce 0

b.配置文件目录

[root~]# cd /etc/ansible/
[root~]# vim /etc/ansible/hosts
[web]
12.16.1.6
12.16.1.7

#在服务器端 的主机上执行
#直接回车即可,不用设置秘钥密码

[root~]# ssh-keygen -t rsc -P  ''

#将公钥(id_rsc.pub)拷贝到客户端的机器上:

[root~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@12.16.1.6
[root~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@12.16.1.7

(3)、简单测试

[root~]# ansible web -m command -a 'uptime'

说明:第一次运行时,需要输入一下“yes”【进行公钥验证】,后续无需再次输入。

C.测试不要密码登入

[root~]# ssh 12.16.1.16

6.ansible进行远程连接测试

[root~]# ansible 12.16.1.16 -m command -a "date"

01.远程主机上执行命令_command

在远程主机上执行命令

相关选项如下:
creates:一个文件名,当该文件存在,则该命令不执行

[root~]# ansible web -m command -a "chdir=/tmp creates=11.txt ls"

chdir:在执行指令之前,先切换到该目录


[root~]# ansible web -m command -a "chdir=/tmp touch 1.txt"

removes:一个文件名,当该文件不存在,则该选项不执行

[root~]# ansible web -m command -a "chdir=/tmp removes=11.txt ls"

executable:切换shell来执行指令,该执行路径必须是一个绝对路径

[root~]# ansible web -m script -a 'executable=/bin/bash /tmp/test.sh'

02.复制文件到远程主机
A.利用ansible可以方便地将本地的目录或者文件同步到远程服务器,具体命令:
#src:被复制到远程主机的本地文件
#dest:目标远程主机的绝对路径
#mode=一般赋予目录0755权限,文件0644权限。

[root~]# ansible web -m copy -a 'src=/opt/test.sh dest=/opt/test.sh mode=0755'

B.#shell是无法执行目标主机不存在的脚本

[root~]# ansible web -m shell -a '/bin/bash /tmp/test.sh'

C.#scripts执行大量的命令的脚步模块,可执行目标主机不存在而控制端存在的脚本

[root~]# ansible web -m script -a 'executable=/bin/bash /tmp/test.sh'

03、执行指定的指令_shell

切换到某个shell执行指定的指令,参数与command相同。

与command不同的是,此模块可以支持命令管道,同时还有另一个模块也具备此功能:raw

示例:

先在本地创建一个SHELL脚本

[root~]# vim /opt/test.sh
#!/bin/sh
date
mkdir /opt/nihao
 
#授权
[root~]# chmod +x /opt/test.sh

将创建的脚本文件分发到远程

[root~]# ansible web -m copy -a 'src=/opt/test.sh dest=/opt/test.sh mode=0755' 

远程执行

[root~]# ansible web -m shell -a "/opt/test.sh"

7.扩展

01.#修改主机名

[root~]# ansible 12.16.1.5 -m hostname -a 'name=master'

02.#创建用户

[root~]# ansible 12.16.1.5 -m user -a 'name=webapp system=yes groups=root,bin uid=10086 comment=webapp shell=/sbin/nologin state=present'

03.#创建带ssh用户

[root~]# ansible 12.16.1.5 -m user -a "name=webapp1 home=/tmp/webapp1 generate_ssh_key=yes ssh_key_bits=2048 ssh_key_file=.ssh/id_rsa"

04.#删除用户

[root~]# ansible 12.16.1.5 -m user -a 'name=webapp state=absent remove=yes'


[root~]# ansible 12.16.1.5 -m user -a 'name=webapp1 state=absent remove=yes'

05.#查看用户

[root~]# ansible 12.16.1.5 -m command -a 'id webapp'

[root~]# ansible 12.16.1.5 -m command -a 'id webapp1'

06.#创建用户组

[root~]# ansible 12.16.1.5 -m group -a "name=webapp system=yes gid=1088"

07.#查看用户组

[root~]# ansible 12.16.1.5 -m command -a 'getent group webapp'

08.#删除用户组

[root~]# ansible 12.16.1.5 -m group -a "name=webapp state=absent"


09.#创建定时任务

[root~]# ansible 12.16.1.5 -m cron -a 'name="cron test" minute=1 hour=10 day=* month=* weekday=* state=present job="/usr/sbin/ntpdate 12.16.141.1"'

10.#禁止定时任务

[root~]# ansible 12.16.1.5 -m cron -a 'disabled=yes name="cron test" job="/usr/sbin/ntpdate 12.16.141.1"'

11.#开启定时任务

[root~]# ansible 12.16.1.5 -m cron -a 'disabled=no name="cron test" job="/usr/sbin/ntpdate 12.16.141.1"'

12.#删除定时任务

[root~]# ansible 12.16.1.5 -m cron -a 'state=absent name="cron test" job="/usr/sbin/ntpdate 12.16.141.1"'

13.#setup 获取目标主机的属性

[root~]# ansible 12.16.1.5 -m setup -a "filter=ansible_default_ipv4"

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大虾别跑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值