ansible的小练习

ansible的小练习

首先要确定ansible是否下载成功

[root@localhost ~]# ansible --version
ansible [core 2.12.7]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.8/site-packages/ansible
  ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.8.13 (default, Jun 24 2022, 15:27:57) [GCC 8.5.0 20210514 (Red Hat 8.5.0-13)]
  jinja version = 2.11.3
  libyaml = True

如果ansible没有下载成功。则需要进行以下步骤:

1.确认控制主机能够上网 ping www.baidu.com

2.配置三个源(本地源、epel源、Centos-stream)

(1)配置本地源

[root@server ~]# mount /dev/sr0 /mnt

查看/etc/yum.repos.d目录下之前是否有配置文件,没有在配置。(有,检查之前本地源配置是否有问题)

[root@server ~]# vim /etc/yum.repos.d/base.repo

[base]
name=base
baseurl=file:///mnt/BaseOS
gpgcheck=0
[App]
name=App
baseurl=file:///mnt/AppStream
gpgcheck=0

(2)配置阿里云的扩展源

yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm

sed -i ‘s|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|’ /etc/yum.repos.d/epel*
sed -i ‘s|^metalink|#metalink|’ /etc/yum.repos.d/epel*

(3)配置centos-stream源

[root@server ~]# vim /etc/yum.repos.d/Centos-stream.repo
[AppStream1]
name=AppStream
baseurl=https://mirrors.aliyun.com/centos/8-stream/AppStream/x86_64/os/
gpgcheck=0
[BaseOS1]
name=BaseOS
baseurl=https://mirrors.aliyun.com/centos/8-stream/BaseOS/x86_64/os/
gpgcheck=0

3.yum install ansible -y

4.ansible --version 判断是否安装成功能显示对应版本就安装好了

配置主机清单

下表列出了有关四个受管主机的信息。您将根据主机的用途、所在的城市以及它所属的部署环境,将每个主机分配给多个组以进行管理。此外,美国城市组(罗利和山景城)必须设为组****us****的子项(children),这样美国的主机就可以作为一 个组进行管理。

| 主机名称 | 用途 | 位置 | 运行环境 |
| ---------------------- - | --------- | ------------------ | --------------- |
| servera.lab.example.com | Web服务器 | 罗利raleigh | 开发development |
| serverb.lab.example.com | Web服务器 | 罗利 | 测试testing |
| serverc.lab.example.com | Web服务器 | 山景城mountainview | 生产production |
| serverd.lab.example.com | Web服务器 | 伦敦london | 生产 |

设置主控主机

[root@server ~]# cd /home
[root@server home]# vim /ansible.cfg
[root@server home]# cat /ansible.cfg
[defaults]
inventory=/home/inventory
[root@server home]# vim inventory
[root@server home]# cat inventory
[raleigh]
servera.lab.example.com
serverb.lab.example.com
[mountainview]
serverc.lab.example.com
[london]
serverd.lab.example.com
[development]
servera.lab.example.com
[testing]
serverb.lab.example.com
[production]
serverc.lab.example.com
serverd.lab.example.com
[us:children]
raleigh
mountainview
[root@server home]# ansible us --list-hosts
 hosts (3):
    servera.lab.example.com
    serverb.lab.example.com
    serverc.lab.example.com
[root@server home]# ansible raleigh --list-hosts
  hosts (2):
    servera.lab.example.com
    serverb.lab.example.com

2.实现ansible配置的四个案例

1、实现ansible 使用 ssh 连接受管主机,一般不建议用管理用户,要求通过普通用 户 student 进行链接

主控主机

[root@localhost ~]# hostnamectl set-hostname example.example.com
[root@localhost ~]# vim /etc/hosts
[root@localhost ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.220.128	server.example.com server
192.168.220.131 node1.example.com node1

[root@localhost ~]# hostname server 
[root@localhost ~]# bash
[root@server ~]# 

受控主机

[root@node1 ~]# hostnamectl set-hostname node1.example.com

主控主机(测试)

[root@server ~]# ping node1
PING node1.example.com (192.168.220.131) 56(84) bytes of data.
64 bytes from node1.example.com (192.168.220.131): icmp_seq=1 ttl=64 time=0.431 ms
64 bytes from node1.example.com (192.168.220.131): icmp_seq=2 ttl=64 time=0.938 ms
[root@localhost ~]# ssh node1			#这里连接node1的那个数据
The authenticity of host 'node1 (192.168.220.131)' can't be established.
ECDSA key fingerprint is SHA256:19i1n6GeJdkjhe5Vbvpk9aoLCiWzaONUMsnq9jdGN24.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes	#这里输入yes
Warning: Permanently added 'node1,192.168.220.131' (ECDSA) to the list of known hosts.
root@node1's password: 			#这里输入密码
[root@server ~]# ssh node1
root@node1's password: 
Permission denied, please try again.
root@node1's password: 
Activate the web console with: systemctl enable --now cockpit.socket

This system is not registered to Red Hat Insights. See https://cloud.redhat.com/
To register this system, run: insights-client --register

Last failed login: Fri Sep  9 05:34:32 EDT 2022 from 192.168.220.128 on ssh:notty
There were 3 failed login attempts since the last successful login.
Last login: Fri Sep  9 05:17:39 2022 from 192.168.220.1
[root@node1 ~]# 
[root@node1 ~]# exit
logout
Connection to node1 closed.
[root@server ~]# 
[root@server ~]# ssh-copy-id -i node1
/usr/bin/ssh-copy-id: ERROR: no ID file found
[root@server ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:Lh5TxY957kKRy4r3ZzU+GnZZlRa9chVVGjlSNdxDrOE root@server
The key's randomart image is:
+---[RSA 3072]----+
|             .*BX|
|         .  ..o*B|
|          o...++=|
|         .o+ E.o.|
|        S.ooo o .|
|       o  +o  oo |
|      +..o  +oo. |
|     ..+o .oooo  |
|      .. ..+o. . |
+----[SHA256]-----+

[root@server ~]# ansible node1 -a 'whoami'
SSH password:
node1 | CHANGED | rc=0 >>
root

2,主机免密登陆,关闭秘钥

root@server ~]# ssh-copy-id -i node1@node1
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed

[root@server ~]# cat /etc/ansible/ansible.cfg 
[defualts]
inventory=/etc/ansible/hosts
remote_user=noed1
ask_pass=false
host_key_checking=false

[root@server ~]# ansible node1 -a 'whoami'
node1 | CHANGED | rc=0 >>
root

3远程用户修改

vim /etc/sudoers

root    ALL=(ALL)       ALL
node1   ALL=(ALL)       NOPASSWD:ALL
[root@server ~]# ansible node1 -a 'sudo useradd user1'

你可能感兴趣的:(rhce,ansible,linux,centos)