准备工作
修改hosts文件位置
[root@master ~]# vim /etc/ansible/ansible.cfg
10 [defaults]
11
12 # some basic default values...
13
14 #inventory = /etc/ansible/hosts
15 inventory = /etc/ansible/inventory
16 #library = /usr/share/my_modules/
配置免密登录
[root@master ~]# 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:AR1cIT2z6+/RV52O8gsdAcafK6YDMfkv0oaV1P0oZ3U root@master
The key's randomart image is:
+---[RSA 3072]----+
| .oo+++ |
| .o.=.. |
| o .+o.. |
| + o.. +..E|
| S .. .=.+|
| . +.+.*+..|
| =.+o*o...|
| o *..+. . |
| o oooo. |
+----[SHA256]-----+
[root@master ~]# ssh-copy-id root@192.168.159.3
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.159.3 (192.168.159.3)' can't be established.
ECDSA key fingerprint is SHA256:QlrvjaCKHxnHJ7jKkwhTaGZZPY7eVHatfWwfsCb4kdc.
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@192.168.159.3's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.159.3'"
and check to make sure that only the key(s) you wanted were added.
添加主机清单
[root@master ansible]# vim inventory
[root@master ansible]# cat inventory
[apache]
102.168.159.3
下载源码包
wget https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.48.tar.gz
wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-util-1.6.1.tar.gz
wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-1.7.0.tar.gz
检测连通性
[root@master apache]# ansible all -m ping
192.168.159.3 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
安装过程
//配置变量
[root@master vars]# vim apache.yml
[root@master vars]# cat apache.yml
packages:
- openssl-devel
- pcre-devel
- expat-devel
- libtool
- gcc
- gcc-c++
- make
- perl
- perl-devel
[root@master scripts]# cat packages.sh
#! /bin/bash
# install apr
tar zxf /root/apr-1.7.0.tar.gz
cd /root/apr-1.7.0
./configure --prefix=/usr/local/apr
make && make install
cd
# install apr-util
tar zxf /root/apr-util-1.6.1.tar.gz
cd /root/apr-util-1.6.1
./configure --prefix=/usr/lcoal/apr-util --with-apr=/usr/local/apr
make && make install
cd
# install apache
tar zxf /root/httpd-2.4.48.tar.gz
cd /root/httpd.2.4.48
./configure --prefix=/usr/lcoal/apache --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util
make && make install
cd
//配置yaml文件
[root@master apache]# vim httpd.yml
[root@master apache]# cat httpd.yml
---
- hosts: apache
vars_files:
- /root/apache/vars/apache.yml
tasks:
- name: install tools
yum:
name: "{{ packages }}"
state: present
- name: cp apr
unarchive:
src: /root/apr-1.7.0.tar.gz
dest: /root/
copy: yes
- name: cp apr-util
unarchive:
src: /root/apr-util-1.6.1.tar.gz
dest: /root/
copy: yes
- name: cp httpd
unarchive:
src: /root/httpd-2.4.48.tar.gz
dest: /root/
copy: yes
- name: create group
group:
name: apache
system: yes
state: present
- name: create user
user:
name: apache
system: yes
state: present
- name: install apr
shell: "{{ install_apr }}"
- name: install apr-util
shell: "{{ install_apr-util }}"
- name: install htppd
shell: "{{ install_httpd }}"
- name: start httpd service
shell: "/usr/local/httpd/bin/apachectl start"
检验结果
//运行playbook
ansible-playbook /root/apache/httpd.yml
配置完成
评论区