Ansible使用角色部署haproxy和httpd

简单部署apache

修改主机清单

[root@ansible ansible]# vim hosts
[webservers]
node1
node2

[haproxy]
node3

创建角色

[root@ansible roles]# ansible-galaxy init httpd
- Role httpd was created successfully

编写任务

[root@ansible roles]# cd httpd/
[root@ansible httpd]# vim tasks/main.yml
---
# tasks file for httpd
- name: mount cdrom
  mount:
    src: /dev/sr0
    path: /mnt
    fstype: iso9660
    state: mounted

- name: set repo1
  yum_repository:
    file: local
    name: BaseOS
    description: BaseOS
    baseurl: file:///mnt/BaseOS
    enabled: yes
    gpgcheck: no

- name: set repo2
  yum_repository:
    file: local
    name: AppStream
    description: AppStream
    baseurl: file:///mnt/AppStream
    enabled: yes
    gpgcheck: no

- name: install httpd
  yum:
    name: httpd
    state: present

- name: cp index.html
  template:
    src: index.html.j2
    dest: /var/www/html/index.html

- name: restart httpd
  service:
    name: httpd
    state: restarted
    enabled: yes

- name: stop firewalld
  service:
    name: firewalld
    state: stopped
    enabled: no

- name: stop selinux
  lineinfile:
    path: /etc/selinux/config
    regexp: '^SELINUX='
    line: SELINUX=disabled

- name: stop selinux1
  shell:
    cmd: setenforce 0

编写模板

[root@ansible httpd]# vim templates/index.html.j2
This is {{ ansible_fqdn }},IP is {{ ansible_ens33.ipv4.address }}

编写playbook执行

[root@ansible httpd]# cd /etc/ansible/
[root@ansible ansible]# vim http.yml
---
- name: install httpd
  hosts: node2
  roles:
    - httpd

[root@ansible ansible]# ansible-playbook http.yml

PLAY [install httpd] *****************************************************************************************************************************************************************************************

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

TASK [httpd : mount cdrom] ***********************************************************************************************************************************************************************************
ok: [node2]

TASK [httpd : set repo1] *************************************************************************************************************************************************************************************
ok: [node2]

TASK [httpd : set repo2] *************************************************************************************************************************************************************************************
ok: [node2]

TASK [install httpd] *****************************************************************************************************************************************************************************************
ok: [node2]

TASK [httpd : cp index.html] *********************************************************************************************************************************************************************************
changed: [node2]

TASK [restart httpd] *****************************************************************************************************************************************************************************************
changed: [node2]

TASK [httpd : stop firewalld] ********************************************************************************************************************************************************************************
changed: [node2]

TASK [httpd : stop selinux] **********************************************************************************************************************************************************************************
changed: [node2]

TASK [httpd : stop selinux1] *********************************************************************************************************************************************************************************
changed: [node2]

PLAY RECAP ***************************************************************************************************************************************************************************************************
node2                      : ok=10   changed=5    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

在这里插入图片描述

部署haproxy

创建角色

[root@ansible roles]# ansible-galaxy init haproxy
- Role haproxy was created successfully

编写任务

[root@ansible roles]# cd haproxy/
[root@ansible haproxy]# vim tasks/main.yml
---
# tasks file for haproxy
- name: mount cdrom
  mount:
    src: /dev/sr0
    path: /mnt
    fstype: iso9660
    state: mounted

- name: set repo1
  yum_repository:
    file: local
    name: BaseOS
    description: BaseOS
    baseurl: file:///mnt/BaseOS
    enabled: yes
    gpgcheck: no

- name: set repo2
  yum_repository:
    file: local
    name: AppStream
    description: AppStream
    baseurl: file:///mnt/AppStream
    enabled: yes
    gpgcheck: no

- name: install haproxy
  yum:
    name: haproxy
    state: present

- name: cp template
  template:
    src: haproxy.cfg.j2
    dest: /etc/haproxy/haproxy.cfg

- name: restart haproxy
  service:
    name: haproxy
    state: restarted
    enabled: yes

- name: stop firewalld
  service:
    name: firewalld
    state: stopped
    enabled: no

- name: stop selinux
  lineinfile:
    path: /etc/selinux/config
    regexp: '^SELINUX='
    line: SELINUX=disabled

- name: stop selinux1
  shell: 
    cmd: setenforce 0

编写模板

[root@ansible haproxy]# cp /etc/haproxy/haproxy.cfg templates/haproxy.cfg.j2
[root@ansible haproxy]# vim templates/haproxy.cfg.j2
frontend main
    bind *:80
....

backend app
    balance     roundrobin
{% for aa in groups.webservers %}
server  {{ hostvars[aa].ansible_fqdn }}  {{ hostvars[aa].ansible_ens33.ipv4.address }}:80 check
{% endfor %}

编写playbook执行

[root@ansible ansible]# vim haproxy.yml
---
- name: webserver facts
  hosts: webservers

- name: install haproxy
  hosts: node3
  roles:
    - haproxy


[root@ansible ansible]# ansible-playbook haproxy.yml

PLAY [webserver facts] ***************************************************************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************************************************************
ok: [node1]
ok: [node2]

PLAY [install haproxy] ***************************************************************************************************************************************************************************************

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

TASK [haproxy : mount cdrom] *********************************************************************************************************************************************************************************
ok: [node3]

TASK [haproxy : set repo1] ***********************************************************************************************************************************************************************************
ok: [node3]

TASK [haproxy : set repo2] ***********************************************************************************************************************************************************************************
ok: [node3]

TASK [install haproxy] ***************************************************************************************************************************************************************************************
ok: [node3]

TASK [haproxy : cp template] *********************************************************************************************************************************************************************************
changed: [node3]

TASK [restart haproxy] ***************************************************************************************************************************************************************************************
changed: [node3]

TASK [haproxy : stop firewalld] ******************************************************************************************************************************************************************************
ok: [node3]

TASK [haproxy : stop selinux] ********************************************************************************************************************************************************************************
ok: [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=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

访问测试

在这里插入图片描述

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值