playbook--apache(变量)

playbook 使用变量编译安装apache

环境

ip功能
192.168.58.10ansible
192.168.58.30apache

配置主机清单

[root@server ansible]# vim inventory
  
[apache]
192.168.58.30

互信

[root@lserver ]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:ZX7EsseGIyy6NzFuxt2irfhyJc7lAy+ZqZ2bxSfP3U root@localhost.localdomain
The key's randomart image is:
+---[RSA 3072]----+
|      .=.+. o    |
|     .. O .= .   |
|      .+ +o B    |
|      ..*+.+ o   |
|      =+SO.oo    |
|     =. X B.. . E|
|    o .= = = . . |
|   . . .o . o    |
|       ..    .   |
+----[SHA256]-----+
[root@server ]# ssh-copy-id root@192.168.58.30
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.58.30's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@192.168.58.30'"
and check to make sure that only the key(s) you wanted were added.

测试

[root@lserver ansible]# ansible all  -m ping
192.168.58.30 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"

下载apr、apr-util/httpd源码包

控制主机

[root@server] wget https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.48.tar.gz
[root@server]wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-1.7.0.tar.gz
[root@server]wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-util-1.6.1.tar.gz

##创建packages目录放置源码包
[root@server] cd /etc/ansible/
[root@server ansible] mkdir packages
[root@server ansible]cd
[root@server]cp apr-1.7.0.tar.gz /etc/ansible/packages
[root@server]cp apr-util-1.6.1.tar.gz /etc/ansible/packages
[root@server]cp httpd.2.4.48.tar.gz /etc/ansible/package

通过host_vars定义变量

[root@ansible ~]# vim /etc/ansible/host_vars/xumeng.yml
---
   # 编译工具
   dell:  gcc,gcc-c++,perl,perl-devel,expat-devel,pcre-devel,pcre,make
   # 编译安装apr          
   apr_install: " cd /root/apr-1.7.0/  &&  ./configure --prefix=/usr/local/apr && make && make install "
   # 编译安装apr-util
   apr_util_intall: " cd /root/apr-util-1.6.1/ && ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && make && make install "
   # 编译安装httpd
   httpd_install: " cd  /root/httpd-2.4.48/ && ./configure --prefix=/usr/local/httpd --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util  && make && make install "    


编写源码安装apache文件

---
- hosts: apache
  vars_files:
    - /etc/ansible/host_vars/xumeng.yml #指定变量文件

  tasks:

    - name: install tools ##安装编译工具
      yum:
       name: "{{ dell }}"
       state: present

    - name: co package apr ##解压后copy到目标主机
      unarchive:
        src: /etc/ansible/packages/apr-1.7.0.tar.gz
        dest: /root/
        copy: yes

    - name: cp package apr-util ##解压后copy到目标主机
      unarchive:
        src: /etc/ansible/packages/apr-util-1.6.1.tar.gz
        dest: /root/
        copy: yes

    - name: cp pachage httpd ##解压后copy到目标主机
      unarchive:
        src: /etc/ansible/packages/httpd-2.4.48.tar.gz
        dest: /root/
        copy: yes
        
     - name: configure
        lineinfile:
          path: /root/apr-1.7.0/configure
          regexp: '^$RM "$cfgfile"'
          line: '# $RM "$cfgfile"'

    - name: install apr ##编译安装
      shell: "{{ apr_install }}"

    - name: install apr-util 编译安装
      shell: "{{ apr_util_install }}"

    - name: install httpd 编译安装
      shell: "{{ httpd_install }}"

    - name: start httpd service 开启服务
      shell: "/usr/local/httpd/bin/apachectl restart"


编写firewall.yml

[root@server ansible]# vim playbook/firewalld.yml

---
- hosts: apache
  tasks: 
    - name: stop firewalld
      service: 
        name: firewalld
        state: stopped

    - name: 
      shell: 
          setenforce 0

tree

[root@server ansible]# tree
.
├── ansible.cfg
├── hosts
├── host_vars
│   └── xumeng.yml
├── inventory
├── packages
│   ├── apr-1.7.0.tar.gz
│   ├── apr-util-1.6.1.tar.gz
│   └── httpd-2.4.48.tar.gz
├── playbook
│   ├── apache.yml
│   └── firewalld.yml
└── roles

执行apache.yml

[root@server playbook]# ansible-playbook  apache.yml 

PLAY [apache] ***************************************************************************

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

TASK [install dell] ********************************************************************
changed: [192.168.58.30]

TASK [co package apr] *******************************************************************
changed: [192.168.58.30]

TASK [cp package apr-util] **************************************************************
changed: [192.168.58.30]

TASK [cp pachage httpd] *****************************************************************
changed: [192.168.58.30]

TASK [install apr] **********************************************************************
changed: [192.168.58.30]

TASK [install apr-util] *****************************************************************
changed: [192.168.58.30]

TASK [install httpd] ********************************************************************
changed: [192.168.58.30]

TASK [start httpd service] **************************************************************
changed: [192.168.58.30]

PLAY RECAP ******************************************************************************
192.168.58.30              : ok=9    changed=8    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   


执行firewalld.yml

[root@server playbook]# ansible-playbook  firewalld.yml 

PLAY [apache] ***************************************************************************

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

TASK [stop firewalld] *******************************************************************
ok: [192.168.58.30]

TASK [shell] ****************************************************************************
changed: [192.168.58.30]

PLAY RECAP ******************************************************************************
192.168.58.30              : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

访问
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值