ansible之role

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 [email protected]
ssh-copy-id [email protected]

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的主要内容,如果未能解决你的问题,请参考以下文章

Ansible之Roles

Ansible之roles使用

Ansible之Roles角色

自动化运维Ansible之Roles部署配置LAMP架构

ansible之role

Ansible自动化部署之ROLES