ansible之role-创新互联

注意三台都需要联网
我的是80.100、80.101、80.102
我是在80.100上操作的其他两台只要开着就行了不需要操作

创新互联公司专注于金林网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供金林营销型网站建设,金林网站制作、金林网页设计、金林网站官网定制、小程序制作服务,打造金林网络公司原创品牌,更为您提供金林网站排名全网营销落地服务。
yum install -y epel-release  //安装epel源
yum install ansible -y

ansible --version          //查看ansible版本

yum install tree -y

cd /etc/ansible
vi hosts       //配置主机清单
最后一行添加
[web]
192.168.80.10[1:2]

ansible all --list-hosts    显示所有已定义的主机

ssh-keygen -t rsa
ssh-copy-id root@192.168.80.101
ssh-copy-id root@192.168.80.102

ansible all -a 'systemctl stop firewalld'

ansible all -a 'setenforce 0'

mkdir -p /opt/roles/nginx/{tasks,templates}

cd /opt/roles/nginx/tasks/

vi epel.yml
- name: copy epel
  copy: src=/etc/yum.repos.d/  dest=/etc/yum.repos.d/

vi user.yml
- name: create user
  user: name=nginx  group=nginx system=yes shell=/sbin/nologin

vi group.yml
- name: create group
  group: name=nginx  system=yes

vi yum.yml
- name: install package
  yum: name=nginx

vi start.yml
- name: start service
  service: name=nginx state=restarted  enabled=true

vi restart.yml
- name: restart service
  service: name=nginx state=restarted

cd ../templates/

yum install -y nginx

cp /etc/nginx/nginx.conf nginx.conf.j2

vi nginx.conf.j2
worker_processes {{ ansible_processor_vcpus }};

cd ../tasks/

vi temp.yml
- name: copy conf file
  template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf

vi main.yml
- include: epel.yml
- include: group.yml
- include: user.yml
- include: yum.yml
- include: temp.yml
- include: start.yml

cd /etc/ansible/

vi nginx_role.yml
- hosts: web
  remote_user: root
  roles:
   - role: nginx

tree roles/

mkdir -p /etc/ansible/group_vars/

touch /etc/ansible/group_vars/all

mkdir -p /etc/ansible/roles/common

mkdir -p /etc/ansible/roles/httpd/{files,templates,tasks,handlers,vars,defaults,meta}

mkdir -p /etc/ansible/roles/mysql/{files,templates,tasks,handlers,vars,defaults,meta}

mkdir -p /etc/ansible/roles/php/{files,templates,tasks,handlers,vars,defaults,meta}

touch /etc/ansible/roles/httpd/{defaults,vars,tasks,meta,handlers}/main.yml

touch /etc/ansible/roles/mysql/{defaults,vars,tasks,meta,handlers}/main.yml

touch /etc/ansible/roles/php/{defaults,vars,tasks,meta,handlers}/main.yml

vi /etc/ansible/site.yml
- hosts: web
  remote_user: root
  roles:
   - httpd
   - mysql

cd /opt/

vi nginx.yml
- hosts: web
  remote_user: root
  roles:
   - role: nginx

ansible web -a "rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm"

ansible-playbook nginx.yml

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


分享文章:ansible之role-创新互联
当前链接:http://pwwzsj.com/article/dsdseg.html