ansible安装nginx
查看hosts文件
[root@ansible playbooks]# ansible all --list
hosts (4):
192.168.1.114
192.168.1.113
192.168.1.111
192.168.1.117
第一次安装报错,发现被控的服务器没有nginx
解决:
[root@database ~]# cd /etc/yum.repos.d/
[root@database yum.repos.d]# ll
total 40
-rw-r--r--. 1 root root 1664 Apr 7 18:01 CentOS-Base.repo
-rw-r--r--. 1 root root 1309 Apr 7 18:01 CentOS-CR.repo
-rw-r--r--. 1 root root 649 Apr 7 18:01 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root 314 Apr 7 18:01 CentOS-fasttrack.repo
-rw-r--r--. 1 root root 630 Apr 7 18:01 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 Apr 7 18:01 CentOS-Sources.repo
-rw-r--r--. 1 root root 7577 Apr 7 18:01 CentOS-Vault.repo
-rw-r--r--. 1 root root 616 Apr 7 18:01 CentOS-x86_64-kernel.repo
-rw-r--r--. 1 root root 2640 Mar 16 06:38 docker-ce.repo
[root@database yum.repos.d]# vim nginx.repo
[root@database yum.repos.d]# cat nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
[root@database yum.repos.d]#
剧本:
[root@ansible playbooks]# cat web-notls.yml
---
- name: Configure dbservers with nginx
hosts: dbservers
become: True
tasks:
- name: install nginx
yum: name=nginx update_cache=yes
- name: copy nginx config file
copy: src=files/nginx.conf dest=/etc/nginx/conf.d
- name: copy index.html
template: src=templates/index.html.j2 dest=/usr/share/nginx/html/index.html mode=0644
- name: restart nginx
service: name=nginx state=restarted
[root@ansible playbooks]#
[root@ansible templates]# cat index.html.j2
<html>
<head>
<title>Welcome to ansible</title>
</head>
<body>
<h1>nginx, configured by Ansible</h1>
<p>If you can see this, Ansible successfully installed nginx.</p>
<p>Running on {{ inventory_hostname }}</p>
</body>
</html>
[root@ansible templates]#
运行:
[root@ansible playbooks]# ansible-playbook web-notls.yml
_______________________________________
< PLAY [Configure dbservers with nginx] >
---------------------------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
________________________
< TASK [Gathering Facts] >
------------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
ok: [192.168.1.117]
______________________
< TASK [install nginx] >
----------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
ok: [192.168.1.117]
_______________________________
< TASK [copy nginx config file] >
-------------------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
ok: [192.168.1.117]
________________________
< TASK [copy index.html] >
------------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
changed: [192.168.1.117]
______________________
< TASK [restart nginx] >
----------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
changed: [192.168.1.117]
____________
< PLAY RECAP >
------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
192.168.1.117 : ok=5 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
结果: