1.创建一个ansible存放路径
[root@node02 scripts]# mkdir -p /home/monitor/ansible/nginx/{conf,bin}
2.编写nginx.yml文件
[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;
}
}
4.查看
[root@node02:/home/monitor]$ tree /home/monitor/ansible/
/home/monitor/ansible/
└── nginx
├── bin
│ └── nginx.yml
└── conf
└── site.conf
3 directories, 2 files
5.check
[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.安装nginx
[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进程
[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