実験環境:(実験を開始する前に、ファイアウォールがオフになっていて、selinuxがオフになっていて、時刻が同期していることを確認してください)
ansible:192.168.2.7 web1:192.168.2.7 web2:192.168.2.17
OS:centos7 OS:centos7 OS:centos7
実現機能:(それぞれのホストによって指定された2つのhttpdサーバーを自動的に展開します)
1。ansibleプレイブックを使用してhttpdを自動的にインストールします
2. httpdサーバーを確立し、ホスト名に基づいてそれぞれの仮想ホストを要求します:
(1)www.x.comページファイルディレクトリは/ web / vhosts、エラーログは
/var/log/httpd/x.err、アクセスログは/var/log/httpd/x.access(2)www.y.com
、ページファイルディレクトリは/です。 web / vhosts;エラーログは/var/log/httpd/y.errで、アクセスログは/var/log/httpd/y.accessです。
(3)2つの仮想ホスト用にそれぞれのホームページファイル/ web / vhosts / indexを作成します。 .html、コンテンツは対応するホスト名です
注:スクリプトはhttpd2.4でコンパイルされ、ソースコードからインストールされます
Ansibleは
epelソースをインストールして構成し、yumはansibleをインストールできます
[root@ansible ~]$cat /etc/yum.repos.d/epel.repo
[epel]
name=aliyun epel
baseurl=http://mirrors.aliyun.com/epel/$releasever/$basearch
enabled=1
gpgcheck=0
[root@ansible ~]$yum -y install ansible
....
2つのホストが展開され、Ansibleはsshキー認証に依存しているため、次のステップはパスワードなしでweb1とweb2にログインすることであり、/ etc / hostsファイルも変更する必要があります。
[root@ansible ~]$ssh-keygen
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:o+OlbC5xbEnPKc3MnhP++eHX78+oN9RPhQTJoihxyUg root@ansible
The key's randomart image is:
+---[RSA 2048]----+
| .Eo . ..o |
| o + . o . |
| o . . . . . |
| . o . . .|
| + OS. ..|
| . *.X. . o|
| +o+.o .. o.|
| .o.+= o .+oo|
| += oo.++.oB|
+----[SHA256]-----+
[root@ansible ~]$ssh-copy-id
anaconda-ks.cfg .bash_history .bash_logout .bash_profile .bashrc .cshrc original-ks.cfg .ssh/ .tcshrc .viminfo
[root@ansible ~]$ssh-copy-id 192.168.2.17
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.2.17 (192.168.2.17)' can't be established.
ECDSA key fingerprint is SHA256:bzwVyjsq4E6PnawN+cXFHl3yIXEgmN64I2+pwUKKKkU.
ECDSA key fingerprint is MD5:1d:d8:5a:07:13:a4:5e:dc:a7:69:7c:79:95:69:fe:67.
Are you sure you want to continue connecting (yes/no)? 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
[email protected]'s password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '192.168.2.17'"
and check to make sure that only the key(s) you wanted were added.
[root@ansible ~]$ssh-copy-id 192.168.2.27
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.2.27 (192.168.2.27)' can't be established.
ECDSA key fingerprint is SHA256:ZWtO06H5nDqNbg12inajd66G9wInPDgEg7OQpiLvSME.
ECDSA key fingerprint is MD5:31:d1:a6:74:bf:e3:d2:09:8b:29:94:f1:53:1f:8c:06.
Are you sure you want to continue connecting (yes/no)? y
Please type 'yes' or 'no': 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
[email protected]'s password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '192.168.2.27'"
and check to make sure that only the key(s) you wanted were added.
[root@ansible ~]$ssh 192.168.2.17
Last login: Sun Nov 22 12:27:57 2020 from afocl-605021336.lan
[root@web1 ~]$exit
logout
Connection to 192.168.2.17 closed.
[root@ansible ~]$ssh 192.168.2.27
Last login: Sun Nov 22 12:28:10 2020 from afocl-605021336.lan
[root@web2 ~]$exit
logout
Connection to 192.168.2.27 closed.
[root@ansible ~]$vim /etc/hosts
[root@ansible ~]$ping -c1 web1
PING web1 (192.168.2.17) 56(84) bytes of data.
64 bytes from web1 (192.168.2.17): icmp_seq=1 ttl=64 time=0.316 ms
--- web1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.316/0.316/0.316/0.000 ms
[root@ansible ~]$ping -c1 web2
PING web2 (192.168.2.27) 56(84) bytes of data.
64 bytes from web2 (192.168.2.27): icmp_seq=1 ttl=64 time=0.285 ms
--- web2 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.285/0.285/0.285/0.000 ms
[root@ansible ~]$cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.2.17 web1 www.web1.com
192.168.2.27 web2 www.web2.com
[root@ansible ~]$scp /etc/hosts 192.168.2.17:/etc/hosts
hosts
[root@ansible ~]$scp /etc/hosts 192.168.2.27:/etc/hosts
hosts
ansibleホストリストを構成し、pingをテストします
[root@ansible ~]$tail -3 /etc/ansible/hosts
[webservers]
web1
web2
[root@ansible ~]$ansible webservers -m ping
web1 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
web2 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
次に、展開を開始し、httpdを自動的にインストールして構成します。アイデアは次のとおりです
。1。ソースコードパッケージを各ホストの/ dataディレクトリにコピーします
。2。スクリプトを実行します(ソースコードからコンパイルおよびインストールされたhttpd2.4バージョン)
3。サービスを構成します。
準備オーケー
[root@ansible ~]$mkdir /data/playbook/roles -pv
mkdir: created directory ‘/data/playbook’
mkdir: created directory ‘/data/playbook/roles’
[root@ansible ~]$cd !:1
cd /data/playbook/roles
[root@ansible roles]$
[root@ansible roles]$mkdir httpd24/{files,tasks,handlers,templates} -pv
mkdir: created directory ‘httpd24’
mkdir: created directory ‘httpd24/files’
mkdir: created directory ‘httpd24/tasks’
mkdir: created directory ‘httpd24/handlers’
mkdir: created directory ‘httpd24/templates’
[root@ansible roles]$tree -d
.
└── httpd24
├── files
├── handlers
├── tasks
└── templates
[root@ansible roles]$cd httpd24/tasks/
[root@ansible tasks]$vim main.yml
[root@ansible tasks]$cat main.yml
- include: time.yml
- include: copypkg.yml
- include: script.yml
- include: dir.yml
- include: config.yml
- include: index.yml
[root@ansible tasks]$awk '{print $3}' main.yml | while read file; do touch $file ; done
[root@ansible tasks]$ll
total 28
-rw-r--r-- 1 root root 228 Nov 22 19:38 config.yml
-rw-r--r-- 1 root root 211 Nov 22 18:25 copypkg.yml
-rw-r--r-- 1 root root 208 Nov 22 20:14 dir.yml
-rw-r--r-- 1 root root 77 Nov 22 20:23 index.yml
-rw-r--r-- 1 root root 0 Nov 22 17:35 install.yml
-rw-r--r-- 1 root root 127 Nov 22 20:23 main.yml
-rw-r--r-- 1 root root 46 Nov 22 19:34 script.yml
-rw-r--r-- 1 root root 105 Nov 22 18:10 time.yml
各ymlファイルを次のように記述します。
[root@ansible tasks]$cat time.yml
- name: install ntpdate
yum: name=ntpdate
- name: time synchronization
shell: ntpdate ntp.aliyun.com
[root@ansible tasks]$cat copypkg.yml
- name: copy apr package
copy: src=apr-1.7.0.tar.gz dest=/data
- name: copy apr-util package
copy: src=apr-util-1.6.1.tar.gz dest=/data
- name: copy httpd package
copy: src=httpd-2.4.39.tar.bz2 dest=/data
[root@ansible tasks]$cat script.yml
- name: run script
script: install_httpd.sh
[root@ansible tasks]$cat dir.yml
- name: virtual host configure dir
file: path=/app/httpd24/conf/vhosts state=directory
- name: log dir
file: path=/var/log/httpd state=directory
- name: data dir
file: path=/web/vhosts state=directory
[root@ansible tasks]$cat config.yml
- name: include configure dir
shell: echo "Include conf/vhosts/*.conf" >> /app/httpd24/conf/httpd.conf
- name: configure virtual host
template: src=web.conf.j2 dest=/app/httpd24/conf/vhosts/web.conf
notify: restart httpd
[root@ansible tasks]$cat index.yml
- name: index page
template: src=index.html.j2 dest=/web/vhosts/index.html
[root@ansible roles]$vim /data/playbook/roles/role_httpd.yml
[root@ansible roles]$cat /data/playbook/roles/role_httpd.yml
- hosts: webservers
roles:
- role: httpd24
実行を開始します
[root@ansible roles]$pwd
/data/playbook/roles
[root@ansible roles]$ansible-playbook role_httpd.yml
実験結果は以下のとおりです。
[root@web1 ~]$ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 [::1]:25 [::]:*
LISTEN 0 128 [::]:80 [::]:*
LISTEN 0 128 [::]:22
[root@web1 ~]$curl web1
web1
[root@web1 ~]$curl www.web1.com
web1
[root@web1 ~]$systemctl is-enabled httpd24
enabled
[root@web2 ~]$ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 [::1]:25 [::]:*
LISTEN 0 128 [::]:80 [::]:*
LISTEN 0 128 [::]:22
[root@web2 ~]$curl web2
web2
[root@web2 ~]$curl www.web2.com
web2
[root@web2 ~]$systemctl is-enabled httpd24
enabled