1.在/etc/hosts 文件中解析4台主机名
2.在/etc/ansible/hosts中添加主机:
[lb]
node1
[tcsrvs]
node[2:3]
3. 基于私钥认证免密码连接:
ssh-keygen -t rsa -P ''
ssh-copy-id -i ~/.ssh/id_rsa.pub root@node{1,2,3,4}
4.ansible all --list-hosts验证是否连接成功
5.对于nginx角色:
mkdir -pv /etc/ansible/roles/{nginx,tomcat}/{files,templates,tasks,handlers,vars,meta,default}
vim task/main.yml
-name: install nginx
yum : name=nginx state=lastest
when :ansible_os_family == "RedHat"
- name : install conf
copy : src = lb.conf dest=/etc/nginx/conf.d
tags: conf
notify: restart nginx
- name : start nginx
service :name=nginx state=started enabled=yes
vim handlers/main.yml
- name:restart nginx
service : name=nginx state=restarted
vim files/lb.conf
upstream tcsrvs{
server node1:8080;
server node2:8080;
}
server{
listen 80;
server_name www.ilinux.io;
location / {
proxy_pass http://tcsrvs;
}
}
mkdir playbook/ cd /playbook
vim nt.yml
- host : lb
remote_user: root
roles:
- nginx
ansible-playbook -C nt.yml ----->至此nginx主机配置完成
6 配置tomcat,jdk角色
cd /etc/ansible/roles/tomcat/
vim tasks/main.yml
- name : install package
yum : name={{ item }} state=lastest
with_items:
- tomcat
-tomcat-admin-webapps
-tomcat-webapps
-tomcat-docs-webapps
when: ansible_os_faminly == "RedHAT"
-name : start tomcat
service: name=tomcat state=started enabled=yes
因为tomcat需要依赖jdk所以另外需要建立一个jdk的角色:
mkdir jdk/{files,templates,tasks,vars,handlers}
vim tasks/main.yml
-name : install jdk
yum : name=java-{{ version }}-openjdk-devel state=lastest
name: install env file
copy: src=java.sh dest=/etc/profile.d/
vim /files/java.sh
export ....
vim /vars/main.yml
vertion: 1.8.0
在之前的nt.yml文件中添加一段:
-host: tcsrvs
remote_user: root
roles:
- jdk
- tomcat
ansible-playbook -C nt.yml