Ansible实现Nginx负载均衡客户请求至后端Tomcat服

2017-09-27  本文已影响0人  Sainbyno

搭建环境

yum -y install ansible
 vim /etc/ansible/hosts
定义主机清单
[root@Client ansible]#ansible all --list-hosts
  hosts (3):
    host1.centos7
    host2.centos7
    host3.centos7
[root@Client ansible]#ansible lb --list-hosts
  hosts (1):
    host1.centos7
[root@Client ansible]#ansible tc --list-hosts
  hosts (2):
    host2.centos7
    host3.centos7
[root@Client ansible]#
[root@Client ansible]#ssh-keygen -t rsa -P ''
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
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:
02:6e:ab:d7:9a:35:54:25:7b:e6:2d:f7:c8:d8:aa:c1 root@Client.centos7
The key's randomart image is:
+--[ RSA 2048]----+
|        . .      |
|         +       |
|    .   o o      |
|   . . . + .     |
|    o o S o o    |
|   . o o   * o   |
|    ..o E . + .  |
|   ..o.. . .     |
|  ..o.  ...      |
+-----------------+
[root@Client ansible]#ssh-copy-id -i ~/.ssh/id_rsa.pub root@host1
The authenticity of host 'host1 (192.168.10.11)' can't be established.
ECDSA key fingerprint is 9a:7d:a7:45:06:e0:7f:f2:d5:9c:35:05:8c:c2:f7:e9.
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
root@host1's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@host1'"
and check to make sure that only the key(s) you wanted were added.

[root@Client ansible]#ssh-copy-id -i ~/.ssh/id_rsa.pub root@host2
...
[root@Client ansible]#ssh-copy-id -i ~/.ssh/id_rsa.pub root@host3
...
[root@Client ansible]#ansible all -m shell -a "ifconfig ens33" 

创建角色

需要创建三个角色分别为nginxtomcat 以及 jdk




完善我们的剧本nt.yml

执行我们的剧本

[root@Client ansible]#ansible-playbook nt.yml   

PLAY [lb] **********************************************************************

TASK [setup] *******************************************************************
ok: [host1.centos7]

TASK [nginx : install nginx] ***************************************************
ok: [host1.centos7]

TASK [nginx : install conf] ****************************************************
ok: [host1.centos7]

TASK [nginx : start nginx] *****************************************************
ok: [host1.centos7]

PLAY [tc] **********************************************************************

TASK [setup] *******************************************************************
ok: [host3.centos7]
ok: [host2.centos7]

TASK [jdk : install openjdk] ***************************************************
changed: [host2.centos7]
changed: [host3.centos7]

TASK [jdk : install evn file] **************************************************
changed: [host3.centos7]
changed: [host2.centos7]

TASK [tomcat : install package] ************************************************
changed: [host3.centos7] => (item=[u'tomcat', u'tomcat-admin-webapps', u'tomcat-webapps', u'tomcat-docs-webapp'])
changed: [host2.centos7] => (item=[u'tomcat', u'tomcat-admin-webapps', u'tomcat-webapps', u'tomcat-docs-webapp'])

TASK [tomcat : start tomcat] ***************************************************
changed: [host3.centos7]
changed: [host2.centos7]

PLAY RECAP *********************************************************************
host1.centos7              : ok=4    changed=0    unreachable=0    failed=0   
host2.centos7              : ok=5    changed=4    unreachable=0    failed=0   
host3.centos7              : ok=5    changed=4    unreachable=0    failed=0   

[root@Client ansible]#

最终测试

在浏览器里输入www.grh.io

上一篇 下一篇

猜你喜欢

热点阅读