准备工作
环境:CentOS8
主机:192.168.220.5
受控机:192.168.220.20
部署ansible
#配置网络源
[root@localhost ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2495 100 2495 0 0 10895 0 --:--:-- --:--:-- --:--:-- 10895
[root@localhost ~]# yum makecache
[root@localhost ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
#安装ansible
[root@localhost ~]# yum install -y centos-release-ansible-29.noarch
[root@localhost ~]# yum install -y ansible
设置免密登录
#配置域名
[root@ansible ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.220.20 web.example.com
#生成密钥
[root@ansible ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
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:b7t6lMfM9n8CLomu2n0BK+FnneUSIebvJE+o90l6IxU root@ansible.example.com
The key's randomart image is:
+---[RSA 3072]----+
| |
| o . |
| o . . |
| . o E . |
| . .S* @ |
| o *.% O |
| = X+B o |
| ...++Xo. o .|
| ..o+=B=+ oo|
+----[SHA256]-----+
[root@ansible ~]# ssh-copy-id root@web.example.com
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host 'web.example.com (192.168.220.20)' can't be established.
ECDSA key fingerprint is SHA256:3RNgVHjr3Kprokn65z0oljPucgxxn1RLJhXsgC6ssqM.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@web.example.com's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@web.example.com'"
and check to make sure that only the key(s) you wanted were added.
#登录到受管主机并修改主机名
[root@ansible ~]# ssh root@web.example.com
Activate the web console with: systemctl enable --now cockpit.socket
Last login: Sun Jun 5 10:43:18 2022 from 192.168.220.5
[root@localhost ~]# ip addr show ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:d8:4f:4a brd ff:ff:ff:ff:ff:ff
inet 192.168.220.20/24 brd 192.168.220.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fed8:4f4a/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@localhost ~]# hostnamectl set-hostname web.example.com
[root@localhost ~]# bash
[root@web ~]#
配置ansible
#设置主机名
[root@localhost ~]# hostnamectl set-hostname ansible.example.com
[root@localhost ~]# bash
[root@ansible ~]#
#将ansible的默认文件复制一份到opt的httpd目录内
[root@ansible ~]# mkdir /opt/httpd
[root@ansible ~]# cp -r /etc/ansible/* /opt/httpd/
[root@ansible ~]# cd /opt/httpd/
[root@ansible httpd]# ls
ansible.cfg hosts roles
#创建清单文件
[root@ansible httpd]# cat inventory
[webservers]
web.example.com
#修改ansible.cfg文件
[root@ansible httpd]# vim ansible.cfg
inventory = ./inventory
[root@ansible httpd]# ansible --version
ansible 2.9.27
config file = /opt/httpd/ansible.cfg
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.6/site-packages/ansible
executable location = /usr/bin/ansible
python version = 3.6.8 (default, Mar 25 2022, 11:15:52) [GCC 8.5.0 20210514 (Red Hat 8.5.0-10)]
#将yum仓库文件复制到files内
[root@ansible ~]# cp -r /etc/yum.repos.d/CentOS-Base.repo /opt/httpd/files/
#下载网页源码包 解压改名为web
[root@ansible httpd]# cd files/
[root@ansible files]# ls
漂亮的个人主页HTML源码.zip CentOS-Base.repo
[root@ansible files]# unzip 漂亮的个人主页HTML源码.zip
[root@ansible files]# mv 漂亮的个人主页HTML源码 /opt/httpd/files/web
[root@ansible files]# ls
漂亮的个人主页HTML源码.zip CentOS-Base.repo web
```bash
#创建剧本
[root@ansible httpd]# cat install.yml
---
- hosts: webservers
vars:
web: httpd
tasks:
- name: copy yum
copy:
src: files/CentOS-Base.repo
dest: /etc/yum.repos.d/
- name: install "{{ web }}"
yum:
name: "{{ web }}"
state: latest
- name: copy web
copy:
src: files/web
dest: /var/www/html/
#运行
[root@ansible httpd]# ansible-playbook install.yml
#将兽管主机的httpd-vhosts.conf文件传到主机的files目录内 并修改配置
[root@ansible files]# scp web.example.com:/usr/share/doc/httpd/httpd-vhosts.conf .
httpd-vhosts.conf 100% 1477 1.2MB/s 00:00
[root@ansible files]# ls
漂亮的个人主页HTML源码.zip CentOS-Base.repo httpd-vhosts.conf web
[root@ansible files]# cat httpd-vhosts.conf
<VirtualHost *:80>
ServerAdmin web.example.com
DocumentRoot "/var/www/web"
ServerName dummy-host2.example.com
ErrorLog "/var/log/httpd/web.com-error_log"
CustomLog "/var/log/web.example.com-access_log" common
</VirtualHost>
为剧本添加其他任务
[root@ansible httpd]# cat install.yml
---
- hosts: webservers
vars:
web: httpd
tasks:
- name: copy yum
copy:
src: files/CentOS-Base.repo
dest: /etc/yum.repos.d/
- name: install "{{ web }}"
yum:
name: "{{ web }}"
state: latest
- name: copy web
copy:
src: files/web
dest: /var/www/html/
- name: config apache
copy:
src: files/httpd-vhosts.conf
dest: /etc/httpd/conf.d/
- name: run "{{ web }}"
service:
name: "{{ web }}"
state: started
enabled: yes
- name: stop firewalld
service:
name: firewalld
state: stopped
enabled: no
#运行
[root@ansible httpd]# ansible-playbook install.yml
修改hosts文件
测试
浏览器访问域名