自动化运维工具-Ansible(2)-安装

4 篇文章 0 订阅
2 篇文章 0 订阅

 

目录

 


一、CentOS

  1. 设置YUM源
  • 备份REPO :mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bk
  • 下载对应版本repo文件,存入/etc/yum.repo.d/ 文件夹
  • 推荐网易(163),中科大 (ustc),华为(Huawei)的yum源,各个地址以下供参考,如果失效,请再仔细查看网站说明。
    • 【163】CentOS5 :http://mirrors.163.com/.help/CentOS5-Base-163.repo
                             CentOS6 :http://mirrors.163.com/.help/CentOS6-Base-163.repo
                             CentOS7 :http://mirrors.163.com/.help/CentOS7-Base-163.repo

                             更新方法: cd /etc/yum.repo.d/   &&  wget   http://mirrors.163.com/.help/CentOS6-Base-163.repo  &&  mv CentOS6-Base-163.repo CentOS-Base.repo

                                                  yum clean all  && yum makecache

  • 【中科大】只有CentOS 7, 8 的源
    • 对于 CentOS 8,使用以下命令替换默认的配置

      sudo sed -e 's|^mirrorlist=|#mirrorlist=|g' \
               -e 's|^#baseurl=http://mirror.centos.org/$contentdir|baseurl=https://mirrors.ustc.edu.cn/centos|g' \
               -i.bak \
               /etc/yum.repos.d/CentOS-Linux-AppStream.repo \
               /etc/yum.repos.d/CentOS-Linux-BaseOS.repo \
               /etc/yum.repos.d/CentOS-Linux-Extras.repo \
               /etc/yum.repos.d/CentOS-Linux-PowerTools.repo \
               /etc/yum.repos.d/CentOS-Linux-Plus.repo
      

      对于 CentOS 7,使用以下命令替换默认配置

      sudo sed -e 's|^mirrorlist=|#mirrorlist=|g' \
               -e 's|^#baseurl=http://mirror.centos.org/centos|baseurl=https://mirrors.ustc.edu.cn/centos|g' \
               -i.bak \
               /etc/yum.repos.d/CentOS-Base.repo
      

      以上命令只替换了默认启用的仓库。替换之后请运行 yum makecache 更新缓存。

    • 【华为】CentOS 7, 8 的源

CentOS的镜像地址为:https://repo.huaweicloud.com/centos/
1、备份配置文件:
cp -a /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
2、两种方案,请大家自行选取。
方案一:
下载新的CentOS-Base.repo文件到/etc/yum.repos.d/目录下,选择CentOS版本:
CentOS 8
执行如下命令:
wget -O /etc/yum.repos.d/CentOS-Base.repo https://repo.huaweicloud.com/repository/conf/CentOS-8-reg.repo

或者

wget -O /etc/yum.repos.d/CentOS-Base.repo https://repo.huaweicloud.com/repository/conf/CentOS-7-reg.repo

方案二:
修改CentOS-Base.repo文件,取消baseurl开头的行的注释,并增加mirrorlist开头的行的注释。将文件中的http://mirror.centos.org替换成https://repo.huaweicloud.com,可以参考如下命令:
sed -i "s/#baseurl/baseurl/g" /etc/yum.repos.d/CentOS-Base.repo
sed -i "s/mirrorlist=http/#mirrorlist=http/g" /etc/yum.repos.d/CentOS-Base.repo
sed -i "s@http://mirror.centos.org@https://repo.huaweicloud.com@g" /etc/yum.repos.d/CentOS-Base.repo
3、执行yum clean all清除原有yum缓存。
4、执行yum makecache(刷新缓存)或者yum repolist all(查看所有配置可以使用的文件,会自动刷新缓存)。

.安装,例如使用网易源:

  1)安装epel源

yum install http://mirrors.163.com/centos/7.9.2009/extras/x86_64/Packages/epel-release-7-11.noarch.rpm

  2)查看epel源并安装ansible

[root@an ~]ll /etc/yum.repos.d/epel*

[root@an ~]yum install ansible -y

  3)查看ansible版本

ansible --version

二、Ubuntu

更换为国内软件源
         #Ctrl + Alt + T进入命令行,命令需有sudo以获取权限。 

sudo cp /etc/apt/sources.list /etc/apt/sources.list_backup  #备份原有配置文件 

sudo gedit /etc/apt/sources.list #将原文件内容全部删除,替换为下面任意链接内镜像源(无图形界面建议gedit换成vim) 

sudo apt-get update #保存后执行。等待更新完毕 

#sudo apt-get update 

#sudo apt-get upgrade 

#update是更新软件列表,upgrade是更新软件

清华源:https://mirror.tuna.tsinghua.edu.cn/help/ubuntu/

中科大源:https://mirrors.ustc.edu.cn/repogen/


清华源

Ubuntu 的软件源配置文件是 /etc/apt/sources.list。将系统自带的该文件做个备份,将该文件替换为下面内容,即可使用 TUNA 的软件源镜像。

选择你的ubuntu版本: 
21.04
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ hirsute main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ hirsute main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ hirsute-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ hirsute-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ hirsute-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ hirsute-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ hirsute-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ hirsute-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ hirsute-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ hirsute-proposed main restricted universe multiverse
Ubuntu (/etc/apt/sources.list): 
HTTPS
 
IPv4
 
hh (21.04)
 Download
deb https://mirrors.ustc.edu.cn/ubuntu/ hh main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ hh main restricted universe multiverse

deb https://mirrors.ustc.edu.cn/ubuntu/ hh-security main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ hh-security main restricted universe multiverse

deb https://mirrors.ustc.edu.cn/ubuntu/ hh-updates main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ hh-updates main restricted universe multiverse

deb https://mirrors.ustc.edu.cn/ubuntu/ hh-backports main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ hh-backports main restricted universe multiverse

## Not recommended
# deb https://mirrors.ustc.edu.cn/ubuntu/ hh-proposed main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/ubuntu/ hh-proposed main restricted universe multiverse

APT 安装Ansible

sudo apt-get install software-properties-common
        sudo apt-add-repository ppa:ansible/ansible
        sudo apt-get update
        sudo apt-get install ansible

sudo ansible --version

三、源码

  • Ansible安装包下载: 官网(https://releases.ansible.com/ansible/
  • CentOS7源码安装Ansible2.X
    • 1、安装Python 3.x环境
      [root@ansible ~]# yum install -y python36 python36-pip git
      [root@ansible ~]# ln -s /usr/bin/pip36 /usr/bin/pip3
      [root@ansible ~]# pip install virtualenv
       
      2、配置virtualenv环境(可选)
      [deploy@ansible ~]$ useradd deploy && su - deploy
      [deploy@ansible ~]$ virtualenv -p /bin/python3.6 .py3-a2.5-env
      [deploy@ansible ~]$ source .py3-a2.5-env/bin/activate
       
      3、从Github下载最新稳定版本代码
      
      [root@ansible ~]# git clone -b stable-2.7 https://github.com/ansible/ansible.git
       
      4、安装ansible Python依赖包
      [root@ansible ~]# pip3 install paramiko PyYAML jinja2
       
      5、安装ansible
      [root@ansible ~]# cd ansible/
      [root@ansible ansible]# python3 setup.py install
       
      6、创建Ansible配置文件,并开启Ansible日志(Ansible默认不启用日志)
      [root@ansible ~]# mkdir /etc/ansible
      [root@ansible ~]# cp ansible/examples/ansible.cfg /etc/ansible/
      [root@ansible ~]# sed -i 's/#log_path/log_path/' /etc/ansible/ansible.cfg
       
      7、验证Ansible安装结果
      
      [root@ansiblet ansible]# ansible --version

       

四、Docker镜像

容器centos7安装部署ansible

centos镜像版本及ansible版本
centos:centos7.5.1804
ansible:2.9.11
启动容器并进入容器
docker run -itd centos:centos7.5.1804 /bin/bash
docker exec -it ae10a393f8df /bin/bash
# 注意以下操作均在容器内进行
安装sshpass、openssh-clients
yum -y install sshpass openssh-clients
# "msg": "to use the 'ssh' connection type with passwords, you must install the sshpass program"
# "msg": "Failed to create temporary directory.In some cases, you may have been able to authenticate and did not have permissions\
 on the target directory. Consider changing the remote tmp path in ansible.cfg to a path rooted in \"/tmp\", for more error \
 information use -vvv. Failed command was: ( umask 77 && mkdir -p \"` echo ~/.ansible/tmp `\"&& mkdir ~/.ansible/tmp/ \
 ansible-tmp-1595925588.91-314-228861218215040 && echo ansible-tmp-1595925588.91-314-228861218215040=\"` \
 echo ~/.ansible/tmp/ansible-tmp-1595925588.91-314-228861218215040 `\" ), exited with result 3"
安装pip
yum -y install epel-release
yum install python-pip -y
安装ansible
pip install ansible
安装winrm
pip install pywinrm
# 连接windows主机需要安装此模块
# 解决: "msg": "winrm or requests is not installed: No moudle named winrm"

创建目录及hosts文件
mkdir -p /etc/ansible/
touch /etc/ansible/hosts
vi /etc/ansible/hosts
# windows-hosts
[windows]
1.x.x.x
2.x.x.x
[windows:vars]
ansible_user = xxx
ansible_password = xxx
ansible_port = 598x
ansible_connection = winrm
ansible_winrm_server_cert_validation = ignore
# linux-hosts
host1 ansible_ssh_host=192.168.x.x ansible_ssh_user=root ansible_ssh_pass="Password"
host2 ansible_ssh_host=1.x.x.x ansible_ssh_user=appuser ansible_ssh_pass="Passw0rd"
创建配置文件
点击查看详细配置文件
修改配置文件
# 解决第一次连接主机需要询问
# "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."
# host_key_checking = False
修改为
host_key_checking = False
生成镜像
docker commit -m "py2_ansible" ae10a393f8df py2_ansible:0728
# ae10a393f8df:容器ID
验证
启动容器并进入容器
docker run -itd py2_ansible:0728 /bin/bash
docker exec -it 8549dabb93fb /bin/bash
执行ansible命令(修改/etc/ansible/hosts)
# hosts 内容见 windows-hosts
[root@8549dabb93fb ansible]# ansible windows -m win_ping 
/usr/lib64/python2.7/site-packages/cryptography/__init__.py:39: CryptographyDeprecationWarning: Python 2 is no longer supported by
 the Python core team. Support for it is now deprecated in cryptography, and will be removed in a future release.
  CryptographyDeprecationWarning,
1.x.x.x | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
2.x.x.x | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
打印执行详细信息
ansible windows -m win_ping -vvv

原地址:https://www.cnblogs.com/fengzi-2016/p/13392343.html

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自动化运维工具——ansible是一款开源的IT自动化工具,广泛应用于软件部署、配置管理和任务协同等领域。它的最大特点是简单易用、功能强大且跨平台。 首先,ansible具有简单易用的特点。它使用简单的YAML语言作为配置文件,无需编写复杂的脚本。用户只需要简单地定义主机和操作即可完成任务的执行。这使得即便是非开发人员,也能轻松上手使用ansible进行自动化管理。 其次,ansible功能强大。它支持广泛的操作系统、云平台和网络设备,并提供了丰富的模块化功能。通过在任务中使用ansible的模块,我们可以实现系统配置、软件安装、文件传输等各种常见操作。此外,ansible不仅支持并发执行任务,还能够将任务分组执行,并提供了强大的变量和条件控制功能。 再次,ansible跨平台。无论是在Linux、Unix还是Windows系统上,ansible都能够良好地运行。此外,它还支持云平台,如AWS、Azure等,以及网络设备,如Cisco、Juniper等。这使得ansible成为一个非常灵活的自动化运维工具,能够满足各种不同环境和需求的自动化管理。 最后,ansible还具有良好的社区支持。ansible拥有庞大的用户社区和活跃的开发者社区,有大量的文档、示例和插件可供参考和使用。这使得我们在使用ansible时能够获得快速解答和支持,同时也能够从社区中学习到更多的技巧和经验。 总而言之,ansible是一款简单易用、功能强大且跨平台的自动化运维工具。它在软件部署、配置管理和任务协同等方面具有广泛的应用,并且得到了良好的社区支持。无论是企业还是个人,都可以通过ansible来提高工作效率和自动化管理水平。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值