成都网站建设设计

将想法与焦点和您一起共享

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 

当前文章:ansible之role
地址分享:http://chengdu.cdxwcx.cn/article/jhgpoo.html