搭建環(huán)境
- 安裝Ansible
yum -y install ansible
- 配置主機(jī)清單
vim /etc/ansible/hosts
定義主機(jī)清單
- 檢查主機(jī)清單是否添加成功
[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]#
- 基于ssh做無(wú)密鑰認(rèn)證
[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
...
- 測(cè)試 ssh 鏈接
- 執(zhí)行簡(jiǎn)單的ansible命令測(cè)試能否管控遠(yuǎn)程主機(jī)
[root@Client ansible]#ansible all -m shell -a "ifconfig ens33"
- 同步時(shí)間
創(chuàng)建角色
需要?jiǎng)?chuàng)建三個(gè)角色分別為nginx 和 tomcat 以及 jdk
- 編寫playbook劇本nt.yml :
- 編寫nginx角色的tasks :
- 編寫nginx角色的配置文件 :
- 編寫nginx角色的handlers :
- 測(cè)試一下nginx角色能否成執(zhí)行:
- 在lb主機(jī)上查看進(jìn)程和端口是否存在:
- 編輯tomcat角色的tasks :
- 編寫jdk角色的tasks :
- 編寫jdk角色的files/java.sh :
- 編寫jdk角色的vars/main.yml :
完善我們的劇本nt.yml
執(zhí)行我們的劇本
[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]#
最終測(cè)試
在瀏覽器里輸入www.grh.io