ansible基础-ansible角色的使用

                                        作者:尹正杰 

版权声明:原创作品,谢绝转载!否则将追究法律责任。

  我们建议把多个节点都会用到的功能将其定义模块,然后谁要用到该模块就直接调用即可!而在ansible中它有一个特有的名称,即角色。

一.角色相关概念

1>.每个角色都是以特定的层级目录结构进行组织

    我们知道ansible可以自定义模块,便于自己或他人调用,它也有一个特有的名称叫做角色。每个角色对应的服务可能不太一样,比如mysql,httpd,nginx,memcached。虽然每个角色(模块)的功能不一样,但是他们都以特定的目录结构进行组织,相关说明如下所述:

files:
存放由copy或script模块等调用的文件。 templates:
template模块查找所需要模板文件的目录。 tasks:
用于定义任务,至少应该包含一个名为main.yml的文件(类似于java和go等编译性语言,用于指定程序的入口),其他的文件需要在此文件中通过include进行包含。 handlers:
定义处理器,至少应该包含一个名为main.yml的文件,其他的文件需要在此文件中通过include进行包含。 vars:
定义变量,至少应该包含一个名为main.yml的文件,其他的文件需要在此文件中通过include进行包含。 meta:
定义元数据,至少应该包含一个名为main.yml的文件,定义当前角色的特殊设定及其依赖关系,其他的文件需要在此文件中通过include进行包含。 default:
设定模式变量时使用此目录中的main.yml文件。

2>.playbook调用角色方法

在playbook调用角色方法1:
- hosts: web
remote_user: root
roles:
- mysql
- memcached
- nginx 在playbook调用角色方法2:传递变量给角色
- hosts: web
remote_user: root
roles:
- mysql
- memcached
- nginx

3>.ansible默认存放路径

[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# grep roles_path /etc/ansible/ansible.cfg
roles_path = /etc/ansible/roles:/usr/share/ansible/roles
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

二.角色实战案例一

1>.创建初始化目录(所有目录不一定都必须存在,如果用不到对应的功能咱们也可以不创建)

[root@node101.yinzhengjie.org.cn /etc/ansible/roles]#
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# mkdir -pv ./{nginx,tomcat,redis,memcached}/{files,templates,tasks,handlers,vars,meta,default}
mkdir: created directory ‘./nginx’
mkdir: created directory ‘./nginx/files’
mkdir: created directory ‘./nginx/templates’
mkdir: created directory ‘./nginx/tasks’
mkdir: created directory ‘./nginx/handlers’
mkdir: created directory ‘./nginx/vars’
mkdir: created directory ‘./nginx/meta’
mkdir: created directory ‘./nginx/default’
mkdir: created directory ‘./tomcat’
mkdir: created directory ‘./tomcat/files’
mkdir: created directory ‘./tomcat/templates’
mkdir: created directory ‘./tomcat/tasks’
mkdir: created directory ‘./tomcat/handlers’
mkdir: created directory ‘./tomcat/vars’
mkdir: created directory ‘./tomcat/meta’
mkdir: created directory ‘./tomcat/default’
mkdir: created directory ‘./redis’
mkdir: created directory ‘./redis/files’
mkdir: created directory ‘./redis/templates’
mkdir: created directory ‘./redis/tasks’
mkdir: created directory ‘./redis/handlers’
mkdir: created directory ‘./redis/vars’
mkdir: created directory ‘./redis/meta’
mkdir: created directory ‘./redis/default’
mkdir: created directory ‘./memcached’
mkdir: created directory ‘./memcached/files’
mkdir: created directory ‘./memcached/templates’
mkdir: created directory ‘./memcached/tasks’
mkdir: created directory ‘./memcached/handlers’
mkdir: created directory ‘./memcached/vars’
mkdir: created directory ‘./memcached/meta’
mkdir: created directory ‘./memcached/default’
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]#
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# tree .
.
├── memcached
│   ├── default
│   ├── files
│   ├── handlers
│   ├── meta
│   ├── tasks
│   ├── templates
│   └── vars
├── nginx
│   ├── default
│   ├── files
│   ├── handlers
│   ├── meta
│   ├── tasks
│   ├── templates
│   └── vars
├── redis
│   ├── default
│   ├── files
│   ├── handlers
│   ├── meta
│   ├── tasks
│   ├── templates
│   └── vars
└── tomcat
├── default
├── files
├── handlers
├── meta
├── tasks
├── templates
└── vars directories, files
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]#

[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# mkdir -pv ./{nginx,tomcat,redis,memcached}/{files,templates,tasks,handlers,vars,meta,default}

2>.编写nginx角色

[root@node101.yinzhengjie.org.cn /etc/ansible/roles]#
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# ll
total
drwxr-xr-x. root root Mar : memcached
drwxr-xr-x. root root Mar : nginx
drwxr-xr-x. root root Mar : redis
drwxr-xr-x. root root Mar : tomcat
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]#
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# tree nginx/
nginx/
├── default
├── files
│   └── index.html
├── handlers
│   └── main.yml
├── meta
├── tasks
│   └── main.yml
├── templates
│   └── server.conf.j2
└── vars
└── main.yml directories, files
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]#
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]#
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]#
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]#
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# cat nginx/files/index.html
<h1>尹正杰到此一游</h1>
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]#

[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# cat nginx/files/index.html

[root@node101.yinzhengjie.org.cn /etc/ansible/roles]#
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# cat nginx/handlers/main.yml
- name: reload nginx
service: name=nginx state=reloaded
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]#
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]#

[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# cat nginx/handlers/main.yml

[root@node101.yinzhengjie.org.cn /etc/ansible/roles]#
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# cat nginx/tasks/main.yml
- name: install epel-release package
yum: name=epel-release state=installed
- name: install nginx package
yum: name=nginx state=installed
- name: create doc root
file: path={{ docroot }} state=directory
- name: install home page
copy: src=index.html dest={{ docroot }}/
- name: install configure file
template: src=server.conf.j2 dest=/etc/nginx/conf.d/server.conf
notify: reload nginx
- name: start nginx service
service: name=nginx enabled=true state=started
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]#
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]#

[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# cat nginx/tasks/main.yml

[root@node101.yinzhengjie.org.cn /etc/ansible/roles]#
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# cat nginx/templates/server.conf.j2
server {
listen ;
server_name {{ ansible_fqdn }} {{ ansible_hostname }}; location / {
root {{ docroot }};
index index.jsp index.html;
}
}
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]#
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]#

[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# cat nginx/templates/server.conf.j2

[root@node101.yinzhengjie.org.cn /etc/ansible/roles]#
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# cat nginx/vars/main.yml
docroot: /yinzhengjie/data/nginx
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]#
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]#

[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# cat nginx/vars/main.yml

3>.编写剧本调用nginx角色

[root@node101.yinzhengjie.org.cn ~/ansible_workshop]#
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# cat nginx.yaml
- hosts: all
remote_user: root
roles:
- nginx
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]#
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible-playbook --syntax-check nginx.yaml playbook: nginx.yaml
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]#

[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# cat nginx.yaml

[root@node101.yinzhengjie.org.cn ~/ansible_workshop]#
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible-playbook --check nginx.yaml PLAY [all] ************************************************************************************************************************************************** TASK [Gathering Facts] **************************************************************************************************************************************
ok: [node102.yinzhengjie.org.cn]
ok: [node101.yinzhengjie.org.cn]
ok: [node103.yinzhengjie.org.cn] TASK [nginx : install epel-release package] *****************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]
ok: [node102.yinzhengjie.org.cn]
ok: [node103.yinzhengjie.org.cn] TASK [nginx : install nginx package] ************************************************************************************************************************
changed: [node101.yinzhengjie.org.cn]
changed: [node102.yinzhengjie.org.cn]
changed: [node103.yinzhengjie.org.cn] TASK [nginx : create doc root] ******************************************************************************************************************************
changed: [node101.yinzhengjie.org.cn]
changed: [node102.yinzhengjie.org.cn]
changed: [node103.yinzhengjie.org.cn] TASK [nginx : install home page] ****************************************************************************************************************************
changed: [node103.yinzhengjie.org.cn]
changed: [node101.yinzhengjie.org.cn]
changed: [node102.yinzhengjie.org.cn] TASK [nginx : install configure file] ***********************************************************************************************************************
changed: [node101.yinzhengjie.org.cn]
changed: [node102.yinzhengjie.org.cn]
changed: [node103.yinzhengjie.org.cn] TASK [nginx : start nginx service] **************************************************************************************************************************
changed: [node103.yinzhengjie.org.cn]
changed: [node101.yinzhengjie.org.cn]
changed: [node102.yinzhengjie.org.cn] RUNNING HANDLER [nginx : reload nginx] **********************************************************************************************************************
changed: [node101.yinzhengjie.org.cn]
changed: [node103.yinzhengjie.org.cn]
changed: [node102.yinzhengjie.org.cn] PLAY RECAP **************************************************************************************************************************************************
node101.yinzhengjie.org.cn : ok= changed= unreachable= failed=
node102.yinzhengjie.org.cn : ok= changed= unreachable= failed=
node103.yinzhengjie.org.cn : ok= changed= unreachable= failed= [root@node101.yinzhengjie.org.cn ~/ansible_workshop]#

[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible-playbook --check nginx.yaml

[root@node101.yinzhengjie.org.cn ~/ansible_workshop]#
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible-playbook nginx.yaml PLAY [all] ************************************************************************************************************************************************************************ TASK [Gathering Facts] ************************************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]
ok: [node103.yinzhengjie.org.cn]
ok: [node102.yinzhengjie.org.cn] TASK [nginx : install epel-release package] ***************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]
ok: [node103.yinzhengjie.org.cn]
ok: [node102.yinzhengjie.org.cn] TASK [nginx : install nginx package] **********************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]
ok: [node102.yinzhengjie.org.cn]
ok: [node103.yinzhengjie.org.cn] TASK [nginx : create doc root] ****************************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]
ok: [node102.yinzhengjie.org.cn]
ok: [node103.yinzhengjie.org.cn] TASK [nginx : install home page] **************************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]
ok: [node102.yinzhengjie.org.cn]
ok: [node103.yinzhengjie.org.cn] TASK [nginx : install configure file] *********************************************************************************************************************************************
changed: [node101.yinzhengjie.org.cn]
changed: [node102.yinzhengjie.org.cn]
changed: [node103.yinzhengjie.org.cn] TASK [nginx : start nginx service] ************************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]
ok: [node103.yinzhengjie.org.cn]
changed: [node102.yinzhengjie.org.cn] RUNNING HANDLER [nginx : reload nginx] ********************************************************************************************************************************************
changed: [node101.yinzhengjie.org.cn]
changed: [node102.yinzhengjie.org.cn]
changed: [node103.yinzhengjie.org.cn] PLAY RECAP ************************************************************************************************************************************************************************
node101.yinzhengjie.org.cn : ok= changed= unreachable= failed=
node102.yinzhengjie.org.cn : ok= changed= unreachable= failed=
node103.yinzhengjie.org.cn : ok= changed= unreachable= failed= [root@node101.yinzhengjie.org.cn ~/ansible_workshop]#

[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible-playbook nginx.yaml                       #运行剧本,注意,安装nginx时请务必关闭掉httpd服务,否则可能会导致nginx服务启动不了!

[root@node101.yinzhengjie.org.cn ~/ansible_workshop]#
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]#
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible all -m shell -a 'ss -ntl | grep 7000'
node101.yinzhengjie.org.cn | SUCCESS | rc= >>
LISTEN *: *:* node103.yinzhengjie.org.cn | SUCCESS | rc= >>
LISTEN *: *:* node102.yinzhengjie.org.cn | SUCCESS | rc= >>
LISTEN *: *:* [root@node101.yinzhengjie.org.cn ~/ansible_workshop]#
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]#
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible all -m shell -a 'cat /etc/nginx/conf.d/server.conf'
node101.yinzhengjie.org.cn | SUCCESS | rc= >>
server {
listen ;
server_name node101.yinzhengjie.org.cn node101; location / {
root /yinzhengjie/data/nginx;
index index.jsp index.html;
}
} node103.yinzhengjie.org.cn | SUCCESS | rc= >>
server {
listen ;
server_name node103.yinzhengjie.org.cn node103; location / {
root /yinzhengjie/data/nginx;
index index.jsp index.html;
}
} node102.yinzhengjie.org.cn | SUCCESS | rc= >>
server {
listen ;
server_name node102.yinzhengjie.org.cn node102; location / {
root /yinzhengjie/data/nginx;
index index.jsp index.html;
}
} [root@node101.yinzhengjie.org.cn ~/ansible_workshop]#

[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible all -m shell -a 'ss -ntl | grep 7000'            #检查nginx服务是否正常启动

[root@node102.yinzhengjie.org.cn ~]#
[root@node102.yinzhengjie.org.cn ~]# curl http://node101.yinzhengjie.org.cn:7000/
<h1>尹正杰到此一游</h1>
[root@node102.yinzhengjie.org.cn ~]#
[root@node102.yinzhengjie.org.cn ~]#
[root@node102.yinzhengjie.org.cn ~]# curl http://node102.yinzhengjie.org.cn:7000/
<h1>尹正杰到此一游</h1>
[root@node102.yinzhengjie.org.cn ~]#
[root@node102.yinzhengjie.org.cn ~]# curl http://node103.yinzhengjie.org.cn:7000/
<h1>尹正杰到此一游</h1>
[root@node102.yinzhengjie.org.cn ~]#
[root@node102.yinzhengjie.org.cn ~]#

三.角色实战案例二

1>.备份之前的配置

[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# ll
total
drwxr-xr-x. root root Mar : memcached
drwxr-xr-x. root root Mar : nginx
drwxr-xr-x. root root Mar : redis
drwxr-xr-x. root root Mar : tomcat
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]#
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]#
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# cp -a nginx/ /root/
[root@node101.yinzhengjie.org.cn /etc/ansible/roles]#

[root@node101.yinzhengjie.org.cn /etc/ansible/roles]# cp -a nginx/ /root/

2>. 编写nginx角色

[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]#
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]#
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# pwd
/etc/ansible/roles/nginx
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]#
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# ll
total
drwxr-xr-x. root root Mar : default
drwxr-xr-x. root root Mar : files
drwxr-xr-x. root root Mar : handlers
drwxr-xr-x. root root Mar : meta
drwxr-xr-x. root root Mar : tasks
drwxr-xr-x. root root Mar : templates
drwxr-xr-x. root root Mar : vars
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]#
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# tree .
.
├── default
├── files
│   └── index.html
├── handlers
│   └── main.yml
├── meta
├── tasks
│   └── main.yml
├── templates
│   ├── proxy.conf.j2
│   └── server.conf.j2
└── vars
└── main.yml directories, files
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]#
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]#
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]#
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# cat files/index.html
<h1>尹正杰到此一游</h1>
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]#
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]#

[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# cat files/index.html

[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]#
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# cat handlers/main.yml
- name: reload nginx
service: name=nginx state=reloaded
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]#
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]#

[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# cat handlers/main.yml

[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]#
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# cat tasks/main.yml
- name: install epel-release package
yum: name=epel-release state=installed - name: install nginx package
yum: name=nginx state=installed - name: create doc root
file: path={{ docroot }} state=directory
when: servertype == 'web' - name: install home page
copy: src=index.html dest={{ docroot }}/
when: servertype == 'web' - name: install web configure file
template: src=server.conf.j2 dest=/etc/nginx/conf.d/server.conf
when: servertype == 'web'
notify: reload nginx - name: install proxy configure file
template: src=proxy.conf.j2 dest=/etc/nginx/conf.d/server.conf
when: servertype == 'proxy'
notify: reload nginx - name: start nginx service
service: name=nginx enabled=true state=started
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]#

[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# cat tasks/main.yml

[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]#
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# cat templates/proxy.conf.j2
server {
listen ;
server_name {{ ansible_fqdn }} {{ ansible_hostname }}; location / {
proxy_pass {{ backendurl }}
}
}
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]#
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]#

[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# cat templates/proxy.conf.j2

[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]#
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# cat templates/server.conf.j2
server {
listen ;
server_name {{ ansible_fqdn }} {{ ansible_hostname }}; location / {
root {{ docroot }};
index index.jsp index.html;
}
}
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]#
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]#

[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# cat templates/server.conf.j2

[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]#
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# cat vars/main.yml
servertype: web
backendurl: 'http:node101.yinzhengjie.org.cn:8080/'
docroot: /yinzhengjie/data/nginx
[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]#

[root@node101.yinzhengjie.org.cn /etc/ansible/roles/nginx]# cat vars/main.yml

3>.编写剧本调用nginx角色

[root@node101.yinzhengjie.org.cn ~/ansible_workshop]#
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# cat /etc/ansible/hosts
[web]
node[:].yinzhengjie.org.cn [tomcat]
node101.yinzhengjie.org.cn
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]#
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# cat nginx-v2.yaml
- hosts: tomcat
remote_user: root
roles:
- { role: nginx, servertype=proxy } - hosts: web
remote_user: root
roles:
- nginx
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]#
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]#

[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# cat nginx-v2.yaml

[root@node101.yinzhengjie.org.cn ~/ansible_workshop]#
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible-playbook --check nginx-v2.yaml PLAY [tomcat] ********************************************************************************************************************************************************************* TASK [Gathering Facts] ************************************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn] TASK [nginx : install epel-release package] ***************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn] TASK [nginx : install nginx package] **********************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn] TASK [nginx : create doc root] ****************************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn] TASK [nginx : install home page] **************************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn] TASK [nginx : install web configure file] *****************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn] TASK [nginx : install proxy configure file] ***************************************************************************************************************************************
skipping: [node101.yinzhengjie.org.cn] TASK [nginx : start nginx service] ************************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn] PLAY [web] ************************************************************************************************************************************************************************ TASK [Gathering Facts] ************************************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]
ok: [node102.yinzhengjie.org.cn] TASK [nginx : install epel-release package] ***************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]
ok: [node102.yinzhengjie.org.cn] TASK [nginx : install nginx package] **********************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]
ok: [node102.yinzhengjie.org.cn] TASK [nginx : create doc root] ****************************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]
ok: [node102.yinzhengjie.org.cn] TASK [nginx : install home page] **************************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]
ok: [node102.yinzhengjie.org.cn] TASK [nginx : install web configure file] *****************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]
ok: [node102.yinzhengjie.org.cn] TASK [nginx : install proxy configure file] ***************************************************************************************************************************************
skipping: [node101.yinzhengjie.org.cn]
skipping: [node102.yinzhengjie.org.cn] TASK [nginx : start nginx service] ************************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]
ok: [node102.yinzhengjie.org.cn] PLAY RECAP ************************************************************************************************************************************************************************
node101.yinzhengjie.org.cn : ok= changed= unreachable= failed=
node102.yinzhengjie.org.cn : ok= changed= unreachable= failed= [root@node101.yinzhengjie.org.cn ~/ansible_workshop]#
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]#

[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible-playbook --check nginx-v2.yaml

[root@node101.yinzhengjie.org.cn ~/ansible_workshop]#
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible-playbook nginx-v2.yaml PLAY [tomcat] ********************************************************************************************************************************************************************* TASK [Gathering Facts] ************************************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn] TASK [nginx : install epel-release package] ***************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn] TASK [nginx : install nginx package] **********************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn] TASK [nginx : create doc root] ****************************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn] TASK [nginx : install home page] **************************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn] TASK [nginx : install web configure file] *****************************************************************************************************************************************
changed: [node101.yinzhengjie.org.cn] TASK [nginx : install proxy configure file] ***************************************************************************************************************************************
skipping: [node101.yinzhengjie.org.cn] TASK [nginx : start nginx service] ************************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn] RUNNING HANDLER [nginx : reload nginx] ********************************************************************************************************************************************
changed: [node101.yinzhengjie.org.cn] PLAY [web] ************************************************************************************************************************************************************************ TASK [Gathering Facts] ************************************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]
ok: [node102.yinzhengjie.org.cn] TASK [nginx : install epel-release package] ***************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]
ok: [node102.yinzhengjie.org.cn] TASK [nginx : install nginx package] **********************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]
ok: [node102.yinzhengjie.org.cn] TASK [nginx : create doc root] ****************************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]
ok: [node102.yinzhengjie.org.cn] TASK [nginx : install home page] **************************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]
ok: [node102.yinzhengjie.org.cn] TASK [nginx : install web configure file] *****************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]
changed: [node102.yinzhengjie.org.cn] TASK [nginx : install proxy configure file] ***************************************************************************************************************************************
skipping: [node101.yinzhengjie.org.cn]
skipping: [node102.yinzhengjie.org.cn] TASK [nginx : start nginx service] ************************************************************************************************************************************************
ok: [node101.yinzhengjie.org.cn]
ok: [node102.yinzhengjie.org.cn] RUNNING HANDLER [nginx : reload nginx] ********************************************************************************************************************************************
changed: [node102.yinzhengjie.org.cn] PLAY RECAP ************************************************************************************************************************************************************************
node101.yinzhengjie.org.cn : ok= changed= unreachable= failed=
node102.yinzhengjie.org.cn : ok= changed= unreachable= failed= [root@node101.yinzhengjie.org.cn ~/ansible_workshop]#
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]#

[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible-playbook nginx-v2.yaml

[root@node101.yinzhengjie.org.cn ~/ansible_workshop]#
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible all -m shell -a 'cat /etc/nginx/conf.d/server.conf'
node101.yinzhengjie.org.cn | SUCCESS | rc= >>
server {
listen ;
server_name node101.yinzhengjie.org.cn node101; location / {
root /yinzhengjie/data/nginx;
index index.jsp index.html;
}
} node103.yinzhengjie.org.cn | FAILED | rc= >>
cat: /etc/nginx/conf.d/server.conf: No such file or directorynon-zero return code node102.yinzhengjie.org.cn | SUCCESS | rc= >>
server {
listen ;
server_name node102.yinzhengjie.org.cn node102; location / {
root /yinzhengjie/data/nginx;
index index.jsp index.html;
}
} [root@node101.yinzhengjie.org.cn ~/ansible_workshop]#
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]#
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# cat /etc/ansible/hosts
node[:].yinzhengjie.org.cn
[web]
node[:].yinzhengjie.org.cn [tomcat]
node101.yinzhengjie.org.cn
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]#
[root@node101.yinzhengjie.org.cn ~/ansible_workshop]#

[root@node101.yinzhengjie.org.cn ~/ansible_workshop]# ansible all -m shell -a 'cat /etc/nginx/conf.d/server.conf'

  角色的使用就介绍到这里了,其实我们可以用角色可以干很多我们想要干的事情,比如:设定基于msm的tomcat集群的角色,为tomcat部署应用,每个节点本地使用nginx发现代理,所有节点前端有一个nginx负载均衡器等等之类的!感兴趣的小伙伴可以自行测试~

ansible基础-ansible角色的使用的更多相关文章

  1. ansible基础-playbook剧本的使用

    ansible基础-playbook剧本的使用 作者:尹正杰  版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.YAML概述 1>.YAML的诞生 YAML是一个可读性高,用来表达数据序 ...

  2. 自动化运维工具-Ansible基础

    目录 自动化运维工具-Ansible基础 什么是Ansible 同类型软件对比 Ansible的功能及优点 Ansible的架构 Ansible的执行流程 安装Ansible ansible配置文件 ...

  3. Ansible--01 ansible基础 Ansible-ad- hoc

    目录 自动化运维工具-Ansible基础 自动化运维的含义 Ansible 基础及安装 Ansible的架构 Ansible的执行流程 ansible配置文件 ansible Inventory(主机 ...

  4. 一、Ansible基础之入门篇

    目录 1. Ansible基础 1.1 介绍 1.2 工作原理 1.3 如何安装 1.3.1 先决条件 1.3.2 安装Ansible 1.4 管理节点与被管理节点建立SSH信任关系 1.5 快速入门 ...

  5. ansible基础-安装与配置

    一 安装 1.1 ansible架构 ansible是一个非常简单的自动化部署项目,由python编写并且开源.用于提供自动化云配置.配置文件管理.应用部署.服务编排和很多其他的IT自动化需求. an ...

  6. ansible基础-优化

    简介 当管理集群达到一定规模时,ansible达到性能瓶颈是难以避免的,此时我们可以通过一定手段提高ansible的执行效率和性能. 笔者虽未管理过超大规模服务器,但也通过查找资料和咨询大神了解了一些 ...

  7. ansible基础-Jinja2模版 | 过滤器

    Jinja2模版介绍 注:本文demo使用ansible2.7稳定版 在ansible基础-变量的「8.2 模版使用变量」章节中关于模版与变量也有所提及,有兴趣的同学可以去回顾一下. ansible通 ...

  8. ansible基础-理解篇

    1. 介绍 要说现在的部署工具,ansible可以说家喻户晓了. ansible是一个开源软件,用于软件供应.配置管理.应用部署.ansible可以通过SSH.remote PowerShell.其他 ...

  9. ansible基础-roles

    一 简介 注:本文demo使用ansible2.7稳定版 在我看来,role是task文件.变量文件.handlers文件的集合体,这个集合体的显著特点是:可移植性和可重复执行性. 实践中,通常我们以 ...

随机推荐

  1. 洛谷 P1112 波浪数

    题目描述 波浪数是在一对数字之间交替转换的数,如 121212112121211212121 ,双重波浪数则是指在两种进制下都是波浪数的数,如十进制数 191919191919191919 是一个十进 ...

  2. table-layui

    本文章为原创文章,转载请注明出处 html <div class="layui-btn-group tableBtn"> <button class=" ...

  3. devops工具

    工具类型及对应的不完全列举整理如下: 代码管理(SCM):GitHub.GitLab.BitBucket.SubVersion 构建工具:Ant.Gradle.maven 自动部署:Capistran ...

  4. BZOJ3638[Codeforces280D]k-Maximum Subsequence Sum&BZOJ3272Zgg吃东西&BZOJ3267KC采花——模拟费用流+线段树

    题目描述 给一列数,要求支持操作: 1.修改某个数的值 2.读入l,r,k,询问在[l,r]内选不相交的不超过k个子段,最大的和是多少. 输入 The first line contains inte ...

  5. BZOJ2281[Sdoi2011]黑白棋&BZOJ4550小奇的博弈——DP+nimk游戏

    题目描述 小A和小B又想到了一个新的游戏. 这个游戏是在一个1*n的棋盘上进行的,棋盘上有k个棋子,一半是黑色,一半是白色. 最左边是白色棋子,最右边是黑色棋子,相邻的棋子颜色不同. 小A可以移动白色 ...

  6. 【XSY2771】城市 分治

    题目描述 一个平原上有\(n\)个城市,第\(i\)个城市在点\((\cos \frac{2i\pi}{n},\sin \frac{2i\pi}{n})\)上. 每个城市和最近的两个城市有一条直线段的 ...

  7. Win10报错0x800f0906

    在安装适用于Linux的Windows子系统(Beta)的时候,有选中开发人员模式这一步设置->更新和安全->针对开发人员->选中开发人员模式 如果报错0x800f0906 那是因为 ...

  8. 【BZOJ 1701】Cow School(斜率优化/动态凸包/分治优化)

    原题题解和数据下载 Usaco2007 Jan 题意 小牛参加了n个测试,第i个测试满分是\(p_i\),它的得分是\(t_i\).老师去掉\(t_i/p_i\)最小的d个测试,将剩下的总得分/总满分 ...

  9. Centos Install Keepalived

    Keepalived简介Keepalived 的作用是检测 web 服务器的状态,如果有一台 web 服务器死机,或工作出现故障,Keepalived 将检测到,并将有故障的 web 服务器从系统中剔 ...

  10. LOJ# 572. 「LibreOJ Round #11」Misaka Network 与求和(min25筛,杜教筛,莫比乌斯反演)

    题意 求 \[ \sum_{i = 1}^{n} \sum_{i = 1}^{n} f(\gcd(i, j))^k \pmod {2^{32}} \] 其中 \(f(x)\) 为 \(x\) 的次大质 ...