准备工作
环境:CentOS8
主机:192.168.74.136
受控机:192.168.74.137
部署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.74.137 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:HqdjkNUlY8Lry535qYISs+PXx+c48n3dLghk6vmn8HQ root@localhost.localdomain
The key's randomart image is:
+---[RSA 3072]----+
| .. + . |
| .+ + |
| ... |
| o. o |
| o.S+. |
| o oo+. |
| + =*=.+E. ..|
| + o.O=B++.o o|
| ..+ *BO=. o.|
+----[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.74.137)' can't be established.
ECDSA key fingerprint is SHA256:vt77BH13KRojr1k6CUrPh1rG3Ofh5WNAEermoTMBYkg.
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: Wed Jun 8 01:24:10 2022 from 192.168.74.1
[root@localhost ~]# hostnamectl set-hostname web.example.com
[root@localhost ~]# bash
[root@web ~]#
#配置ansible
[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
[root@ansible httpd]# ansible --version
ansible [core 2.12.5]
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.8/site-packages/ansible
ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/bin/ansible
python version = 3.8.12 (default, Sep 21 2021, 00:10:52) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3)]
#将yum仓库文件复制到files内
[root@ansible ~]# cp -r /etc/yum.repos.d/CentOS-Base.repo /opt/httpd/files/
#下载网页源码包
[root@ansible httpd]# cd files/
[root@ansible files]# ls
CentOS-Base.repo chishenme.zip
[root@ansible files]# unzip chishenme.zip
[root@ansible files]# mv chishenme /opt/httpd/files/web
[root@ansible files]# ls
CentOS-Base.repo chishenme.zip web
[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
[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
chishenme.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