1、环境
服务 | ip |
---|
ansible | 192.168.129133 | httpd | 192.168.129.135 |
2、解析
[root@ansible ~]
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.129.133 httpd
3、密码
SSH免密登陆
[root@ansible ~]
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:m9UgEbkFLID4MnlRrsmn76K0ARRhb6NEScTM1etAfEM root@ansible
The key's randomart image is:
+---[RSA 3072]----+
|*O=+=E .++ |
|+==+ +. o.. |
| =.=o o..o. |
|*.=+.. .. o |
|.=+ + S . . |
|. o . + |
| o. o |
|. +. |
|.o oo |
+----[SHA256]-----+
[root@ansible ~]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host 'httpd (192.168.129.133)' can't be established.
ECDSA key fingerprint is SHA256:+wH81RHiBmLpbkuk2OWGZxVRziiaNwJ9KAVjGtEM8zs.
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@httpd's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@httpd'"
and check to make sure that only the key(s) you wanted were added.
ping
[root@ansible playbook]
192.168.129.135 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
4、查看项目文件
[root@ansible playbook]
.
├── bao.yml
├── install.yml
└── pei.yml
5、源码包地址
wget https://mirrors.bfsu.edu.cn/apache/apr/apr-1.7.0.tar.gz
wget https://mirrors.bfsu.edu.cn/apache/apr/apr-util-1.6.1.tar.gz
wget https://mirrors.bfsu.edu.cn/apache/httpd/httpd-2.4.48.tar.gz
6、下载源码包
bao.yml
[root@ansible playbook]
---
- hosts: httpd
tasks:
- name: httpd
get_url:
url: https://mirrors.bfsu.edu.cn/apache/httpd/httpd-2.4.48.tar.gz
dest: /opt
- name: apr
get_url:
url: https://mirrors.bfsu.edu.cn/apache/apr/apr-1.7.0.tar.gz
dest: /opt
- name: apr-util
get_url:
url: https://mirrors.bfsu.edu.cn/apache/apr/apr-util-1.6.1.tar.gz
dest: /opt
空运行
[root@ansible playbook]
PLAY [httpd] ********************************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************************
ok: [192.168.129.135]
TASK [httpd] ********************************************************************************************************************************************************
changed: [192.168.129.135]
TASK [apr] **********************************************************************************************************************************************************
changed: [192.168.129.135]
TASK [apr-util] *****************************************************************************************************************************************************
changed: [192.168.129.135]
PLAY RECAP **********************************************************************************************************************************************************
192.168.129.135 : ok=4 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
7、编写主机变量文件 pei.yml文件
[root@ansible playbook]
---
dell: gcc,gcc-c++,perl,perl-devel,expat-devel,pcre-devel,pcre
apr_install: " cd /root/apr-1.7.0/ && ./configure --prefix=/usr/local/apr && make && make install "
apr_util_install: "cd /root/apr-util-1.6.1/ && ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && make && make install"
httpd_install: " cd /root/httpd-2.4.48/ && ./configure --prefix=/usr/local/httpd --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util && make && make install "
8、编写源码安装apache文件
install.yml文件
[root@ansible playbook]
---
- hosts: httpd
vars_files:
- /opt/playbook/pei.yml
tasks:
- name: stop and disabled firewalld
service:
name: firewalld
state: stopped
enabled: no
- name: disabled selinux
lineinfile:
path: /etc/selinux/config
regexp: "^SELINUX="
line: "SELINUX=disabled"
state: present
- name: install tools
yum:
name: "{{ dell }}"
state: present
- name: apr
unarchive:
src: /opt/apr-1.7.0.tar.gz
dest: /root
copy: no
tags: unarchive
- name: apr-util
unarchive:
src: /opt/apr-util-1.6.1.tar.gz
dest: /root
copy: no
tags: unarchive
- name: httpd
unarchive:
src: /opt/httpd-2.4.48.tar.gz
dest: /root
copy: no
tags: unarchive
- name: del
lineinfile:
dest: /root/apr-17.0/configure
regexp: '$RM "$cfgfile"'
state: absent
- name: create group
group:
name: apache
system: yes
state: present
- name: create user
user:
name: apache
system: yes
state: present
- name: install apr
shell: " {{ apr_install }}"
- name: intall apr-util
shell: " {{ apr_util_install }}"
- name: install httpd
shell: " {{ httpd_install }} "
- name: start httpd service
shell: " /usr/local/httpd/bin/apachectl start "
验证语法
[root@ansible playbook]
playbook: install.yml
空运行
[root@ansible playbook]
PLAY [httpd] ********************************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************************
ok: [192.168.129.135]
TASK [stop and disabled firewalld] **********************************************************************************************************************************
ok: [192.168.129.135]
TASK [disabled selinux] *********************************************************************************************************************************************
ok: [192.168.129.135]
TASK [install tools] ************************************************************************************************************************************************
ok: [192.168.129.135]
TASK [apr] **********************************************************************************************************************************************************
ok: [192.168.129.135]
TASK [apr-util] *****************************************************************************************************************************************************
ok: [192.168.129.135]
TASK [httpd] ********************************************************************************************************************************************************
ok: [192.168.129.135]
TASK [del] **********************************************************************************************************************************************************
ok: [192.168.129.135]
TASK [create group] *************************************************************************************************************************************************
ok: [192.168.129.135]
TASK [create user] **************************************************************************************************************************************************
ok: [192.168.129.135]
TASK [install apr] **************************************************************************************************************************************************
changed: [192.168.129.135]
TASK [intall apr-util] **********************************************************************************************************************************************
changed: [192.168.129.135]
TASK [install httpd] ************************************************************************************************************************************************
changed: [192.168.129.135]
TASK [start httpd service] ******************************************************************************************************************************************
changed: [192.168.129.135]
PLAY RECAP **********************************************************************************************************************************************************
192.168.129.135 : ok=14 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
执行playbook文件
[root@ansible playbook]
PLAY [httpd] ********************************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************************
ok: [192.168.129.135]
TASK [stop and disabled firewalld] **********************************************************************************************************************************
ok: [192.168.129.135]
TASK [disabled selinux] *********************************************************************************************************************************************
ok: [192.168.129.135]
TASK [install tools] ************************************************************************************************************************************************
ok: [192.168.129.135]
TASK [apr] **********************************************************************************************************************************************************
ok: [192.168.129.135]
TASK [apr-util] *****************************************************************************************************************************************************
ok: [192.168.129.135]
TASK [httpd] ********************************************************************************************************************************************************
ok: [192.168.129.135]
TASK [del] **********************************************************************************************************************************************************
ok: [192.168.129.135]
TASK [create group] *************************************************************************************************************************************************
ok: [192.168.129.135]
TASK [create user] **************************************************************************************************************************************************
ok: [192.168.129.135]
TASK [install apr] **************************************************************************************************************************************************
changed: [192.168.129.135]
TASK [intall apr-util] **********************************************************************************************************************************************
changed: [192.168.129.135]
TASK [install httpd] ************************************************************************************************************************************************
changed: [192.168.129.135]
TASK [start httpd service] ******************************************************************************************************************************************
changed: [192.168.129.135]
PLAY RECAP **********************************************************************************************************************************************************
192.168.129.135 : ok=14 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
9、在浏览器用HTTP主机IP访问
|