签到成功

知道了

CNDBA社区CNDBA社区

ansible-playbook安装nginx

2021-11-15 15:10 1250 0 原创 ansible
作者: hbhe0316

1.创建一个ansible存放路径http://www.cndba.cn/hbhe0316/article/22182http://www.cndba.cn/hbhe0316/article/22182

[root@node02 scripts]# mkdir -p /home/monitor/ansible/nginx/{conf,bin}

2.编写nginx.yml文件

http://www.cndba.cn/hbhe0316/article/22182

[root@node02:/home/monitor]$ cat /home/monitor/ansible/nginx/bin/nginx.yml 
- hosts: node01 
  remote_user: monitor
  become: yes
  become_method: sudo
  vars:
    hello: Ansible

  tasks:
  - name: Add repo
    yum_repository:
      name: nginx
      description: nginx repo
      baseurl: http://192.168.56.100/epel 
      gpgcheck: no
      enabled: 1
  - name: Install nginx
    yum:
      name: nginx
      state: latest
  - name: Copy nginx configuration file
    copy:
      src: /home/monitor/ansible/nginx/conf/site.conf
      dest: /etc/nginx/conf.d/site.conf
  - name: Start nginx
    service:
      name: nginx
      state: started
  - name: Create wwwroot directory
    file:
      dest: /var/www/html
      state: directory
  - name: Create test page index.html
    shell: echo "hello {{hello}}" > /var/www/html/index.html

3.编写site.conf文件
[root@node02:/home/monitor]$ cat /home/monitor/ansible/nginx/conf/site.conf
server {
listen 80;
server_name 192.168.56.101;
location / {
root /var/www/html;
index index.html;
}
}http://www.cndba.cn/hbhe0316/article/22182

4.查看

http://www.cndba.cn/hbhe0316/article/22182
http://www.cndba.cn/hbhe0316/article/22182

[root@node02:/home/monitor]$ tree /home/monitor/ansible/
/home/monitor/ansible/
└── nginx
    ├── bin
    │   └── nginx.yml
    └── conf
        └── site.conf

3 directories, 2 files

5.check

http://www.cndba.cn/hbhe0316/article/22182

[monitor@node02 bin]$ ansible-playbook -i /home/monitor/hosts /home/monitor/ansible/nginx/bin/nginx.yml --syntax-check

playbook: /home/monitor/ansible/nginx/bin/nginx.yml

6.安装nginxhttp://www.cndba.cn/hbhe0316/article/22182http://www.cndba.cn/hbhe0316/article/22182

[monitor@node02 bin]$ ansible-playbook -i /home/monitor/hosts /home/monitor/ansible/nginx/bin/nginx.yml 

PLAY [node01] *************************************************************************************************************************************************************

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

TASK [Add repo] ***********************************************************************************************************************************************************
changed: [192.168.56.100]

TASK [Install nginx] ******************************************************************************************************************************************************
changed: [192.168.56.100]

TASK [Copy nginx configuration file] **************************************************************************************************************************************
changed: [192.168.56.100]

TASK [Start nginx] ********************************************************************************************************************************************************
changed: [192.168.56.100]

TASK [Create wwwroot directory] *******************************************************************************************************************************************
ok: [192.168.56.100]

TASK [Create test page index.html] ****************************************************************************************************************************************
changed: [192.168.56.100]

PLAY RECAP ****************************************************************************************************************************************************************
192.168.56.100               : ok=7    changed=5    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

7.检查nginx进程http://www.cndba.cn/hbhe0316/article/22182

[monitor@node02 bin]$ ansible  -i /home/monitor/hosts node01 -m shell -a "ps -ef |grep nginx"
192.168.56.100 | CHANGED | rc=0 >>
root     14311     1  0 13:40 ?        00:00:00 nginx: master process /usr/sbin/nginx
nginx    14312 14311  0 13:40 ?        00:00:00 nginx: worker process
nginx    14313 14311  0 13:40 ?        00:00:00 nginx: worker process
nginx    14314 14311  0 13:40 ?        00:00:00 nginx: worker process
nginx    14315 14311  0 13:40 ?        00:00:00 nginx: worker process
monitor  16309 16304  0 13:58 pts/2    00:00:00 /bin/sh -c ps -ef |grep nginx
monitor  16311 16309  0 13:58 pts/2    00:00:00 grep nginx

版权声明:本文为博主原创文章,未经博主允许不得转载。

ansible

用户评论
* 以下用户言论只代表其个人观点,不代表CNDBA社区的观点或立场
hbhe0316

hbhe0316

关注

1.只有承认无知,才能装下新的东西; 2.进步来自一点点滴滴的积累; 3.广博让你更优秀,而专业让你无法替代; 4.挫折和失败能够转换为一种财富。

  • 889
    原创
  • 1
    翻译
  • 13
    转载
  • 24
    评论
  • 访问:1038951次
  • 积分:1523
  • 等级:核心会员
  • 排名:第6名
精华文章
    最新问题
    查看更多+
    热门文章
      热门用户
      推荐用户
        Copyright © 2016 All Rights Reserved. Powered by CNDBA · 皖ICP备2022006297号-1·

        QQ交流群

        注册联系QQ