角色部署httpd
[root@ansible ansible]# ls
ansible.cfg hosts php.yml
apache.yml mysql.yml roles
[root@ansible ansible]# vim hosts
[webservers]
node1
node2
[haproxy]
node3
[root@ansible roles]# ansible-galaxy init httpd
- Role httpd was created successfully
[root@ansible httpd]# cd templates/
[root@ansible templates]# ls
httpd.service.j2
[root@ansible templates]# vim index.html.j2
[root@ansible templates]# cat index.html.j2
welcome to {{ ansible_fqdn }} of {{ ansible_default_ipv4.address }}
[root@ansible httpd]# cat tasks/main.yml
---
# tasks file for httpd
- name: stop firwalld
service:
name: firewalld
state: stopped
enabled: no
- name: stop selinux
lineinfile:
path: /etc/selinux/config
regexp: '^SELINUX'
line: SELINUX=disabled
- name: stop firewalld
service:
name: firewalld
state: stopped
enabled: no
- name: mount cdrom
mount:
src: /dev/cdrom
path: /mnt
fstype: iso9660
state: mounted
- name: set repo1
yum_repository:
file: server
name: 11
description: 111
baseurl: file:///mnt/BaseOS
enabled: yes
gpgcheck: no
- name: set repo2
yum_repository:
file: server
name: 22
description: 222
baseurl: file:///mnt/AppStream
enabled: yes
gpgcheck: no
- name: install httpd
dnf:
name: httpd
state: present
- name: index.html
template:
src: index.html.j2
dest: /var/www/html/index.html
- name: restart httpd
service:
name: httpd
state: restarted
enabled: yes
[root@ansible ansible]# vim httpd.yml
[root@ansible ansible]# cat httpd.yml
---
- name:
hosts: node2
roles:
- httpd
[root@ansible ansible]# ansible-playbook httpd.yml
PLAY [node2] *******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node2]
TASK [httpd : stop firwalld] ***************************************************
ok: [node2]
TASK [httpd : stop selinux] ****************************************************
ok: [node2]
TASK [httpd : stop firewalld] **************************************************
ok: [node2]
TASK [httpd : mount cdrom] *****************************************************
ok: [node2]
TASK [httpd : set repo1] *******************************************************
[WARNING]: The value 111 (type int) in a string field was converted to '111'
(type string). If this does not look like what you expect, quote the entire
value to ensure it does not change.
[WARNING]: The value 11 (type int) in a string field was converted to '11'
(type string). If this does not look like what you expect, quote the entire
value to ensure it does not change.
ok: [node2]
TASK [httpd : set repo2] *******************************************************
[WARNING]: The value 222 (type int) in a string field was converted to '222'
(type string). If this does not look like what you expect, quote the entire
value to ensure it does not change.
[WARNING]: The value 22 (type int) in a string field was converted to '22'
(type string). If this does not look like what you expect, quote the entire
value to ensure it does not change.
ok: [node2]
TASK [install httpd] ***********************************************************
ok: [node2]
TASK [httpd : index.html] ******************************************************
changed: [node2]
TASK [restart httpd] ***********************************************************
changed: [node2]
PLAY RECAP *********************************************************************
node2 : ok=10 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
角色部署haproxy
[root@ansible ~]# cd /etc/ansible/
[root@ansible ansible]# cd roles/
[root@ansible roles]# ansible-galaxy init haproxy
- Role haproxy was created successfully
[root@ansible roles]# ls
apache haproxy httpd mysql php
[root@ansible yum.repos.d]# dnf -y install haproxy
[root@ansible ~]# cd /etc/ansible/roles/haproxy/
[root@ansible haproxy]# cp /etc/haproxy/haproxy.cfg templates/haproxy.cfg.j2
[root@ansible haproxy]# cd templates/
[root@ansible templates]# ls
haproxy.cfg.j2
[root@ansible templates]# vim haproxy.cfg.j2
frontend main
bind *:80
backend app
balance roundrobin
{% for yum in groups.webservers %}
server {{ hostvars[yum].ansible_fqdn }} {{ hostvars[yum].ansible_ens33.ipv4.address }}:80 c
heck
{% endfor %}
[root@ansible haproxy]# cat tasks/main.yml
---
# tasks file for haproxy
- name: stop firewalld
service:
name: firewalld
state: stopped
enabled: no
- name: stop selinux
lineinfile:
path: /etc/selinux/config
regexp: '^SELINUX'
line: SELINUX=disabled
- name: mount cdrom
mount:
src: /dev/cdrom
path: /mnt
fstype: iso9660
state: mounted
- name: set repo1
yum_repository:
file: server
name: 11
description: 111
baseurl: file:///mnt/BaseOS
enabled: yes
gpgcheck: no
- name: set repo2
yum_repository:
file: server
name: 22
description: 222
baseurl: file:///mnt/AppStream
enabled: yes
gpgcheck: no
- name: install haproxy
dnf:
name: haproxy
state: present
- name: cp config
template:
src: haproxy.cfg.j2
dest: /etc/haproxy/haproxy.cfg
- name: restart haproxy
service:
name: haproxy
state: restarted
enabled: yes
[root@ansible ansible]# cat haproxy.yml
---
- name:
hosts: webservers
- name:
hosts: node3
roles:
- haproxy
[root@ansible ansible]# ansible-playbook haproxy.yml
PLAY [webservers] **************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node2]
ok: [node1]
PLAY [node3] *******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node3]
TASK [haproxy : stop firewalld] ************************************************
ok: [node3]
TASK [haproxy : stop selinux] **************************************************
ok: [node3]
TASK [haproxy : mount cdrom] ***************************************************
ok: [node3]
TASK [haproxy : set repo1] *****************************************************
[WARNING]: The value 111 (type int) in a string field was converted to '111'
(type string). If this does not look like what you expect, quote the entire
value to ensure it does not change.
[WARNING]: The value 11 (type int) in a string field was converted to '11'
(type string). If this does not look like what you expect, quote the entire
value to ensure it does not change.
ok: [node3]
TASK [haproxy : set repo2] *****************************************************
[WARNING]: The value 222 (type int) in a string field was converted to '222'
(type string). If this does not look like what you expect, quote the entire
value to ensure it does not change.
[WARNING]: The value 22 (type int) in a string field was converted to '22'
(type string). If this does not look like what you expect, quote the entire
value to ensure it does not change.
ok: [node3]
TASK [install haproxy] *********************************************************
ok: [node3]
TASK [haproxy : cp config] *****************************************************
ok: [node3]
TASK [restart haproxy] *********************************************************
changed: [node3]
PLAY RECAP *********************************************************************
node1 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node2 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node3 : ok=9 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0