Ansible自动化运维----yum源的方式安装ansible(国内镜像)

本文介绍了如何在Linux环境中通过yum源安装Ansible,强调了确保虚拟机联网的重要性,并详细步骤配置国内镜像,以实现18560个包的正常扫描。同时,文章还涉及建立普通用户并进行权限设置,以实现免密连接所有主机,完成了Ansible的安装和初步配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

准备主机

模拟多台主机ansible部署

主机功能
ansible-server172.25.65.10
agent1172.25.65.11
agent2172.25.65.12

部署准备

[root@server10 ~]# vim yum.repo    ##添加镜像文件
[ansible]
name=ansible
baseurl=https://mirror.tuna.tsinghua.edu.cn/epel/7/x86_64/
gpgcheck=0
[root@server10 yum.repos.d]# yum clean all
Loaded plugins: product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
Cleaning repos: ansible rhel7.5
Cleaning up everything
Maybe you want: rm -rf /var/cache/yum, to also free up space taken by orphaned data from disabled or removed repos
[root@server10 yum.repos.d]# yum repolist
Loaded plugins: product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
ansible                                                  | 5.3 kB     00:00     
rhel7.5                                                  | 4.3 kB     00:00     
(1/5): ansible/updateinfo                                  | 1.0 MB   00:01     
(2/5): rhel7.5/group_gz                                    | 145 kB   00:00     
(3/5): rhel7.5/primary_db                                  | 4.1 MB   00:00     
(4/5): ansible/group_gz                                    |  90 kB   00:01     
(5/5): ansible/primary_db                                  | 6.9 MB   00:08     
repo id                              repo name                            status
ansible                              ansible                              13,461
rhel7.5                              rhel7.5                               5,099
repolist: 18,560

扫描到的包数量是18560才是正常的 rhel7.5+外网镜像

[root@server10 yum.repos.d]# yum install ansible -y

Installed:
  ansible.noarch 0:2.9.1-1.el7                                                  

Dependency Installed:
  PyYAML.x86_64 0:3.10-11.el7                                                   
  libyaml.x86_64 0:0.1.4-11.el7_0                                               
  python-babel.noarch 0:0.9.6-8.el7                                             
  python-backports.x86_64 0:1.0-8.el7                                           
  python-backports-ssl_match_hostname.noarch 0:3.5.0.1-1.el7                    
  python-cffi.x86_64 0:1.6.0-5.el7                                              
  python-enum34.noarch 0:1.0.4-1.el7                                            
  python-httplib2.noarch 0:0.9.2-0.2.el7                                        
  python-idna.noarch 0:2.4-1.el7                                                
  python-ipaddress.noarch 0:1.0.16-2.el7                                        
  python-jinja2.noarch 0:2.7.2-2.el7                                            
  python-markupsafe.x86_64 0:0.11-10.el7                                        
  python-paramiko.noarch 0:2.1.1-0.10.el7                                       
  python-ply.noarch 0:3.4-11.el7                                                
  python-pycparser.noarch 0:2.14-1.el7                                          
  python-setuptools.noarch 0:0.9.8-7.el7                                        
  python2-cryptography.x86_64 0:1.7.2-2.el7                                     
  python2-jmespath.noarch 0:0.9.0-1.el7                                         
  python2-pyasn1.noarch 0:0.1.9-7.el7                                           
  sshpass.x86_64 0:1.06-1.el7                                                   

Complete!

从以上安装过程可以看出,ansible和python环境有依赖

  • 建立普通用户
    超级用户也可以,但是一般的生产环境中,普通用户更多被使用
    三台主机都做
[root@server10 yum.repos.d]# useradd devops 
[root@server10 yum.repos.d]# passwd devops  ##给devops设置密码
  • 给普通用户授权
[root@server10 yum.repos.d]# visudo 
大概在93行的地方,添加
93 devops  ALL=(ALL)       NOPASSWD:ALL
  • 切换到普通用户(master)
[root@server10 yum.repos.d]# su - devops
  • 免密
[devops@server10 yum.repos.d]# ssh-keygen
[devops@server10 yum.repos.d]# ssh-copy-id server11
[devops@server10 yum.repos.d]# ssh-copy-id server12
  • /etc/ansible 拷贝到devops的用户家目录下
[root@server10 yum.repos.d]# cd /etc/ansible 
[root@server10 yum.repos.d]# ls
ansible.cfg  hosts  
[root@server10 etc]# cp -ir  /etc/ansible  /home/devops/
[devops@server10 ~]$ ls
ansible
[devops@server10 ~]$ ll ansible 
-rw-r--r-- 1 devops devops 146 Nov 23 21:24 ansible.cfg
-rw-rw-r-- 1 devops devops 140 Nov 23 20:35 hosts
  • 修改inventory文件
[devops@server10 ~]$  cat ansible.cfg
[defaults]
inventory = ./hosts    ##会优先读取当前目录下的hosts
[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=False
  • 修改hosts文件
    该文件中存放主机的信息
[devops@server10 ansible]$ cat hosts
[test]
172.25.65.11
172.25.65.12
[my]
172.25.65.10

测试

[root@server10 ansible]# ansible test -m ping
server11 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
  • 测试所有的主机是否都能免密连接
[root@server10 ansible]# ansible all -m ping
server12 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
server11 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

至此,ansible的安装和免密设置完成!!!

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值