1.环境
cat /etc/hosts127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4::1 localhost localhost.localdomain localhost6 localhost6.localdomain610.0.0.50 ansible-master10.0.0.51 ansible-node110.0.0.52 ansible-node210.0.0.53 ansible-node3ssh-keygen -t rsa -P ""ssh-copy-id -i ~/.ssh/id_rsa.pub root@ansible-node1ssh-copy-id -i ~/.ssh/id_rsa.pub root@ansible-node2ssh-copy-id -i ~/.ssh/id_rsa.pub root@ansible-node3cat /etc/ansible/hosts[lb]ansible-node1[tcsrvs]ansible-node2ansible-node3# 创建角色目录mkdir -pv /etc/ansible/roles/{nginx,tomcat}/{files,templetes,tasks,vars,handlers,meta,default}# 配置文件cat /etc/ansible/roles/nginx/tasks/main.yml- name: install nginx yum: name=nginx state=latest 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=truecat /etc/ansible/roles/nginx/handlers/main.yml- name: restart nginx service: name=nginx state=restartedcat /etc/ansible/roles/nginx/files/lb.confupstream tcsrvs { server ansible-node2:8080; server ansible-node3:8080;}server { listen 80; server_name www.ilinux.net; location / { proxy_pass http://tcsrvs; }}cat /etc/ansible/playbooks/nginx-tomcat.yml- hosts: lb remote_user: root roles: - nginx- hosts: tcsrvs remote_user: root roles: - {role: jdk,version: 1.8.0} - tomcatansible-playbook -C nginx.yaml
2.ansible以roles的方式安装tomcat
cat /etc/ansible/roles/tomcat/tasks/main.yml- name: install package yum: name={ { item }} state=latest with_item: - tomcat - tomcat-admin-webapps - tomcat-webapps - tomcat-docs-webapp when: ansible_os_family == "RedHat"- name: start tomcat service: name=tomcat state=started enabled=true# 安装jdkmkdir -pv /etc/ansible/roles/jdk/{files,templetes,tasks,vars,handlers,meta,default}# 系统安装的openjdk不用手动提供java家目录cat /etc/ansible/roles/jdk/tasks/main.yml- name: install openjdk yum: name=java-{ { version }}-openjdk-devel state=latest- name: create env file copy: src=java.sh dest=/etc/profile.d/cat files/java.shexport JAVA_HOME=/usr