ansible实战: 使用roles完成nginx的源码安装

1. 安装ansible并配置主机清单

[root@localhost ~]# yum -y install epel-release
[root@localhost ~]# yum -y install ansible
[root@localhost ~]# ls /etc/ansible/
ansible.cfg  hosts  nginx.yml  roles
[root@localhost ~]# cat /etc/ansible/hosts 
[dbservers]
192.168.20.124

[webservers]
192.168.20.125 http_port=80
192.168.20.126 http_port=81

[webservers:vars]
http_port=8080
groupname=webservers
domain=kgc.cn

2. roles的规范和书写

这里使用的是/etc/ansible/roles目录

[root@localhost ~]# tree /etc/ansible/ -L 4
/etc/ansible/
├── ansible.cfg
├── hosts
├── nginx.yml
└── roles
    └── nginx
        ├── files
        │   └── nginx-1.15.4.tar.gz
        ├── handlers
        │   └── main.yml
        ├── tasks
        │   ├── check.yml
        │   ├── install.yml
        │   ├── main.yml
        │   └── tem.yml
        ├── templates
        │   └── nginx.conf.j2
        └── vars
            └── main.yml

7 directories, 11 files

说明:

templates目录用于防止模板,注意模板的格式即可;files目录用于存放分发给被控端的文件,直接放进去就可以;下边只展示yml文件的内容

/etc/ansible/nginx.yml

[root@localhost ansible]# cat nginx.yml 
- hosts: dbservers
  remote_user: root
  roles:
    - role: nginx

tasks目录

[root@localhost ~]# cat /etc/ansible/roles/nginx/tasks/install.yml 
- name: add user
  user: name=nginx shell=/sbin/nologin create_home=no
- name: install dependency software
  yum: name=gcc,gcc-c++,zlib-devel,openssl-devel,pcre-devel
- name: distribute tar file
  copy: src={{ tarfile }} dest=/root
- name: unarchive tar file
  unarchive: src=/root/{{ tarfile }} dest=/usr/src/ remote_src=yes
- name: configure and install nginx
  shell: ./configure --prefix={{ basedir }} --user=nginx --group=nginx --with-http_stub_status_module && make && make install
  args:
    chdir: /usr/src/nginx-1.15.4
- name: make soft link
  file: src={{ basedir }}/sbin/nginx dest=/usr/sbin/nginx state=link

[root@localhost ~]# cat /etc/ansible/roles/nginx/tasks/tem.yml 
- name: apply template
  template: src=nginx.conf.j2 dest={{ basedir }}/conf/nginx.conf
  notify: reload nginx

[root@localhost ~]# cat /etc/ansible/roles/nginx/tasks/check.yml 
- name: check syntax error
  shell: nginx -t
- name: start nginx
  shell: nginx

[root@localhost ~]# cat /etc/ansible/roles/nginx/tasks/main.yml 
- import_tasks: install.yml
- import_tasks: tem.yml
- import_tasks: check.yml

vars目录

[root@localhost ~]# cat /etc/ansible/roles/nginx/vars/main.yml 
tarfile: nginx-1.15.4.tar.gz
basedir: /usr/local/nginx

handlers目录

[root@localhost ~]# cat /etc/ansible/roles/nginx/handlers/main.yml 
- name: reload nginx
  shell: nginx -s reload

3. 检测和执行

[root@localhost ~]# cd /etc/ansible/
[root@localhost ansible]# ansible-playbook nginx.yml --syntax-check

playbook: nginx.yml

如上所示则表示没有语法错误

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

PLAY [dbservers] ***************************************************************

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

TASK [nginx : add user] ********************************************************
ok: [192.168.20.124]

TASK [nginx : install dependency software] *************************************
ok: [192.168.20.124]

TASK [nginx : distribute tar file] *********************************************
ok: [192.168.20.124]

TASK [nginx : unarichive tar file] *********************************************
ok: [192.168.20.124]

TASK [nginx : configure and install nginx] *************************************
changed: [192.168.20.124]

TASK [nginx : make soft link] **************************************************
changed: [192.168.20.124]

TASK [nginx : apply template] **************************************************
ok: [192.168.20.124]

TASK [nginx : check syntax error] **********************************************
changed: [192.168.20.124]

TASK [nginx : start nginx] *****************************************************
changed: [192.168.20.124]

PLAY RECAP *********************************************************************
192.168.20.124             : ok=10   changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

发布了67 篇原创文章 · 获赞 56 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43557605/article/details/102661361
今日推荐