首页 > 其他分享 >ansible部署httpd和haproxy

ansible部署httpd和haproxy

时间:2022-11-08 22:56:22浏览次数:46  
标签:haproxy httpd ok name ansible root

角色部署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  

标签:haproxy,httpd,ok,name,ansible,root
From: https://www.cnblogs.com/loronoa/p/16871547.html

相关文章

  • 基于ansible实现不同AWS账号之间EC2-Linux系统的自动化-环境准备
    不同的AWS之间的EC2之间如果需要通信,实现ansible自动化,是需要有网络条件的本文主要介绍不同AWS账号之间EC2-(Linux系统)之间实现通信的网络环境准备步骤1、在VPC中建立到另......
  • 关于haproxy不重启重新加载配置文件
    HAProxy:在不影响现有连接的情况下,重新加载配置第一种方法:原来的版本好像是比较麻烦,参见:http://haproxy.1wt.eu/download/1.3/doc/architecture.txt第二种方法:但......
  • CentOS7编译安装haproxy-2.6.6
    创建用户并安装依赖包#selinux会影响haproxy启动,会有'cannotbindUNIXsocket(Permissiondenied)'的报错,请关闭它除非你知道设置selinux规则sed-i'/SELINUX/s/enf......
  • 自动化利器 Ansible - 从了解到应用
    本文说明本系列使用ansible2.9.27版本来说明和汇总相关信息。#cat/etc/system-releaseRedHatEnterpriseLinuxServerrelease7.8(Maipo)##uname-aLinu......
  • Ansible - 7 - 性能优化
    性能优化在默认设置的情况下,Ansible的执行效率已经可以满足大多数场景。面对巨量目标主机时,可以通过一些配置优化去再提高ansible的执行效率。基本设置#通过time命......
  • Ansible - 9 - 技巧提示
    Ansible技巧提示1-免密登录#通过秘钥方式连接ssh-keygen-trsassh-copy-id-i/vipxf/.ssh/[email protected]/vipxf/.ssh/id_rsa......
  • Ansible - 8 - 图形界面
    Ansible图形界面AnsibleWebUI目前主要有AnsibleTower、AnsibleAWX、semaphore。AnsibleTowerAnsible官方WebUI文档:https://docs.ansible.com/ansible-tower/......
  • Ansible - 2 - 命令讲解
    Ansible命令命令集#以下所有命令均可使用`-h`参数获取帮助信息ansible#Defineandrunasingletask'playbook'againstasetofhosts#常用命......
  • Ansible - 3 - 变量使用
    变量(variable)Ansible的变量名仅能由字母、数字和下划线组成,且只能以字母开头。Python关键字和playbook关键字都不能作为有效的变量名。Ansible的变量可以被定义在play......
  • Ansible 安装并简单使用
    Ansible简介Ansible是一款IT自动化工具。主要应用场景有配置系统、软件部署、持续发布及不停服平滑滚动更新的高级任务编排。Ansible本身非常简单易用,同时注重安全和可......