ansible角色调用

  • 本篇博客仅供学习,没有实际项目意义,仅供学习之用
  • 实验目的:
    配置中心服务器自动配置两台机器分别部署为nginx的反响为例服务和nginx的web服务。

ansible角色调用

配置密钥连接:

进入用户的ssh密钥路径
cd /root/.ssh/
生成公私钥
ssh-keygen -t rsa -b 4096
将公钥发送给远程目标主机
ssh-copy-id -i id_rsa.pub [email protected]
测试连接
ssh [email protected]

定义角色的路径:默认位置
[root@localhost ~]# less /etc/ansible/ansible.cfg

# additional paths to search for roles in, colon separated
#roles_path    = /etc/ansible/roles

创建相应的目录:

~]# cd /etc/ansible/roles/
[root@localhost roles]#  mkdir -pv ./nginx/{files,templates,tasks,handlers,vars,meta,dafault}

目录结构

[root@localhost roles]# tree
├── memcached
            ├── dafault
            ├── files
            ├── handlers
            ├── meta
            ├── tasks
            ├── templates
            └── vars

配置nginx思路:
安装程序包,复制配置文件,启动服务。

编辑/nginx/tasks/main.yml文件(设置任务列表)

[root@localhost nginx]# vim 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 }}/
        #自动会去templates目录下找j2文件
        #复制带变量的文件需要使用templates来执行
- name: install conf 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

     #使用templates复制文件时的路劲设置方式:
         相对于etc/ansible/roles/nginx的相对路径
         template: copy: src=templates/server.conf.j2  dest=/etc/nginx/conf.d/server.conf
         完全绝对路径
            template: copy: src=/etc/ansible/roles/nginx/templates/server.conf.j2  dest=/etc/nginx/conf.d/server.conf
            需要自动去templates目录下自动识别
         template: src=server.conf.j2 dest=/etc/nginx/conf.d/server.conf

nginx的模版配置文件(.j2文件)

[root@localhost nginx]# vim templates/server.conf.j2 
server {
                listen 80;
                server_name {{ ansible_fqdn }} {{ ansible_hostname  }};

                location / {
                                root {{ docroot }};
                                index index.jsp index.html;
                }
}

触发器文件配置

[root@localhost nginx]# vim handlers/main.yml
- name: reload nginx
    server: name=nginx state=reloaded

这个nginx wed服务,需要测试页,编辑网站的主页:

[root@localhost nginx]# vim files/index.html
<h1>Welcome to nginx </h1>

定义变量:

[root@localhost nginx]# vim vars/main.yml
#定义网站主页的路径
docroot: /data/nginx/

??

[root@localhost ~]# cd ansible/
[root@localhost ansible]# vim nginx.yml
- hosts: all
    remote_user: root
    roles:
    - nginx

进行语法检测:

[root@localhost ansible]# ansible-playbook --syntax-check nginx.yml 

进行测试执行:

[root@localhost ansible]# ansible-playbook --check nginx.yml 

PLAY [10.10.11.2] *************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************
ok: [10.10.11.2]

TASK [nginx : install epel-release package] ***********************************************************************
ok: [10.10.11.2]

TASK [nginx : install nginx package] ******************************************************************************
ok: [10.10.11.2]

TASK [nginx : create doc root] ************************************************************************************
ok: [10.10.11.2]

TASK [nginx : install home page] **********************************************************************************
ok: [10.10.11.2]

TASK [nginx : install conf file] **********************************************************************************
ok: [10.10.11.2]

TASK [nginx : start nginx service] ********************************************************************************
ok: [10.10.11.2]

PLAY RECAP ***********************************************************************************
10.10.11.2                 : ok=7    changed=0    unreachable=0    failed=0   

确认执行文件:

[root@localhost ansible]# ansible-playbook  nginx.yml 

PLAY [10.10.11.2] *******************************************************************************

TASK [Gathering Facts] **************************************************************************
ok: [10.10.11.2]

TASK [nginx : install epel-release package] *****************************************************
ok: [10.10.11.2]

TASK [nginx : install nginx package] ************************************************************
ok: [10.10.11.2]

TASK [nginx : create doc root] ******************************************************************
ok: [10.10.11.2]

TASK [nginx : install home page] ****************************************************************
ok: [10.10.11.2]

TASK [nginx : install conf file] ****************************************************************
ok: [10.10.11.2]

TASK [nginx : start nginx service] **************************************************************
ok: [10.10.11.2]

PLAY RECAP **************************************************************************************
10.10.11.2                 : ok=7    changed=0    unreachable=0    failed=0   

去执行命令的节点查看
查看端口

[root@localhost ~]# ss -ntlp |grep 80
LISTEN     0      128          *:80                       *:*                   users:(("nginx",pid=11368,fd=6),("nginx",pid=11314,fd=6))

查看配置文件

[root@localhost ~]#  cat /etc/nginx/conf.d/server.conf 
server {
    listen 80;
    server_name localhost.localdomain localhost;

    location / {
        root /data/nginx/;
        index index.jsp index.html;
    } 
}

上边实验的结果是实现nginx的web服务功能,而现在需要实现nginx做web和proxy,针对nginx的不同角色功能定位,来实现不同机器的分功能的配置

修改前先进行备份

[root@localhost roles]#  cp -a nginx/ /root/

修改自定义变量

[root@localhost nginx]# vim vars/main.yml 
#添加servertype变量值为web
servertype: web    
docroot: /data/nginx/

修改:/nginx/tasks/main.yml文件(设置任务列表)

[root@localhost nginx]# vim 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
     #新添加类型为web
    when: servertype == 'web'
- name: install home page
    copy: src=index.html dest={{ docroot }}/
     #新添加类型为web
    when: servertype == 'web'
- name: install conf file
    template: src=server.conf.j2 dest=/etc/nginx/conf.d/server.conf
     #新添加类型为web
    when: servertype == 'web'
    notify: reload nginx
- name: start nginx service
    service: name=nginx enabled=true state=started

测试执行nging=web (没有问题)

[root@localhost ansible]# ansible-playbook --check nginx.yml 

PLAY [10.10.11.2] *******************************************************************************

TASK [Gathering Facts] **************************************************************************
ok: [10.10.11.2]

TASK [nginx : install epel-release package] *****************************************************
ok: [10.10.11.2]

TASK [nginx : install nginx package] ************************************************************
ok: [10.10.11.2]

TASK [nginx : create doc root] ******************************************************************
ok: [10.10.11.2]

TASK [nginx : install home page] ****************************************************************
ok: [10.10.11.2]

TASK [nginx : install conf file] ****************************************************************
ok: [10.10.11.2]

TASK [nginx : start nginx service] **************************************************************
changed: [10.10.11.2]

PLAY RECAP **************************************************************************************
10.10.11.2                 : ok=7    changed=1    unreachable=0    failed=0   

 配置代理

设置变量

[root@localhost nginx]# vim vars/main.yml 
servertype: web
#设置变量,代理服务器的代理目标主机变量
backendurl: 'http://127.0.0.1:8080'
docroot: /data/nginx/

创建proxy的模版文件

[root@localhost nginx]# vim templates/proxy.conf.j2 
server {
                listen 80;
                server_name {{ ansible_fqdn }} {{ ansible_hostname }};
                location / {
                                proxy_pass {{ backendurl }};
                }
}

yaml文件,play_books文件,
修改:/nginx/tasks/main.yml文件(设置任务列表)

[root@localhost nginx]# vim 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 conf file
    template: src=server.conf.j2 dest=/etc/nginx/conf.d/server.conf
    when: servertype == 'web'
    notify: reload nginx
    #新加proxy部分配置段
- name: install proxy conf 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
/nginx/tasks/main.yml文件(设置任务列表)测试

手动分别测试

默认执行web
ansible角色调用

使用:-e servertype=proxy 手动触发proxy
ansible角色调用

自动实现(修改部分如下:)

配置/etc/ansible/hosts

[root@localhost nginx]# vim /etc/ansible/hosts 
[tomcat]
10.10.11.133
[web]
10.10.11.2

yaml文件play_books文件

[root@localhost ansible]# cat nginx.yml 
- hosts: tomcat
remote_user: root
roles:
- { role: nginx, servertype: proxy }

- hosts: web
remote_user: root
roles:
- nginx

测试:

[root@localhost ansible]# ansible-playbook --check nginx.yml 

PLAY [tomcat] ***********************************************************************************

TASK [Gathering Facts] **************************************************************************
ok: [10.10.11.133]

TASK [nginx : install epel-release package] *****************************************************
changed: [10.10.11.133]

TASK [nginx : install nginx package] ************************************************************
changed: [10.10.11.133]

TASK [nginx : create doc root] ******************************************************************
skipping: [10.10.11.133]

TASK [nginx : install home page] ****************************************************************
skipping: [10.10.11.133]

TASK [nginx : install web conf file] ************************************************************
skipping: [10.10.11.133]

TASK [nginx : install proxy conf file] **********************************************************
changed: [10.10.11.133]

TASK [nginx : start nginx service] **************************************************************
changed: [10.10.11.133]

RUNNING HANDLER [nginx : reload nginx] **********************************************************
changed: [10.10.11.133]

PLAY [web] **************************************************************************************

TASK [Gathering Facts] **************************************************************************
ok: [10.10.11.2]

TASK [nginx : install epel-release package] *****************************************************
ok: [10.10.11.2]

TASK [nginx : install nginx package] ************************************************************
ok: [10.10.11.2]

TASK [nginx : create doc root] ******************************************************************
ok: [10.10.11.2]

TASK [nginx : install home page] ****************************************************************
ok: [10.10.11.2]

TASK [nginx : install web conf file] ************************************************************
ok: [10.10.11.2]

TASK [nginx : install proxy conf file] **********************************************************
skipping: [10.10.11.2]

TASK [nginx : start nginx service] **************************************************************
changed: [10.10.11.2]

PLAY RECAP **************************************************************************************
10.10.11.133               : ok=6    changed=5    unreachable=0    failed=0   
10.10.11.2                 : ok=7    changed=1    unreachable=0    failed=0   

真正执行结果

ansible角色调用

最后的目录结构如下:

[root@localhost nginx]# tree
.
[root@localhost nginx]# tree

.
├── dafault
├── files
│   └── index.html
├── handlers
│   └── main.yml
├── meta
├── tasks
│   └── main.yml
├── templates
│   ├── proxy.conf.j2
│   └── server.conf.j2
└── vars
        └── main.yml

猜你喜欢

转载自blog.51cto.com/12105235/2128557