Ansible使用变量安装httpd

这篇博客详细介绍了如何利用Ansible的变量功能来安装httpd服务,包括下载安装包,编写playbook,执行安装任务,关闭防火墙和selinux,重启httpd服务,并通过IP访问测试httpd是否正常运行。
摘要由CSDN通过智能技术生成

Ansible用变量安装httpd

下载httpd安装包

[root@localhost ansible]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.48.tar.gz      
[root@localhost ansible]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-1.7.0.tar.gz  
[root@localhost ansible]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-util-1.6.1.tar.gz
[root@localhost ansible]# ls
apr-util-1.6.1.tar.gz              ansible.cfg             inventory
apr-1.7.0.tar.gz  group_vars             httpd-2.4.48.tar.gz  playbook

编写playbook

[root@localhost ansible]# cat install.yml 
---
- hosts: 192.168.100.42
  tasks: 
    - name: install base packges
      yum:
        name: '{{item}}'
        state: present
      with_items: 
        - pcre
        - pcre-devel
        - expat-devel
        - gcc
        - gcc-c++
        - openssl-devel
        - make

    - name: uncompress apr
      unarchive:
        src: /etc/ansible/apr-1.7.0.tar.gz
        dest: /opt/

    - name: uncompress util 
      unarchive:
        src: /etc/ansible/apr-util-1.6.1.tar.gz
        dest: /opt/

    - name: uncompress httpd
      unarchive:
        src: /etc/ansible/httpd-2.4.48.tar.gz
        dest: /opt/

    - name: configure
      lineinfile: 
        path: /opt/apr-1.7.0/configure
        regexp: '^$RM "$cfgfile"'
        line: '# $RM "$cfgfile"' 

    - name: install apr
      shell: cd /opt/apr-1.7.0 && ./configure --prefix=/usr/local/apr && make && make install

    - name: install apr-util
      shell: cd /opt/apr-util-1.6.1 && ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && make && make install

    - name: install httpd
      shell: cd /opt/httpd-2.4.48 && ./configure --prefix=/usr/local/httpd --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ && make && make install    
[root@localhost ansible]# 

执行

[root@localhost ansible]# ansible-playbook install.yml 

PLAY [192.168.100.42] ***************************************************************************************************************************************************************************

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

TASK [install base packges] *********************************************************************************************************************************************************************
[DEPRECATION WARNING]: Invoking "yum" only once while using a loop via squash_actions is deprecated. Instead of using a loop to supply multiple items and specifying `name: "{{item}}"`, please 
use `name: ['pcre', 'pcre-devel', 'expat-devel', 'gcc', 'gcc-c++', 'openssl-devel', 'make']` and remove the loop. This feature will be removed in version 2.11. Deprecation warnings can be 
disabled by setting deprecation_warnings=False in ansible.cfg.
ok: [192.168.100.42] => (item=['pcre', 'pcre-devel', 'expat-devel', 'gcc', 'gcc-c++', 'openssl-devel', 'make'])

TASK [uncompress apr] ***************************************************************************************************************************************************************************
changed: [192.168.100.42]

TASK [uncompress util] **************************************************************************************************************************************************************************
ok: [192.168.100.42]

TASK [uncompress httpd] *************************************************************************************************************************************************************************
ok: [192.168.100.42]

TASK [configure] ********************************************************************************************************************************************************************************
changed: [192.168.100.42]

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

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

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

PLAY RECAP **************************************************************************************************************************************************************************************
192.168.100.42             : ok=9    changed=5    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@localhost ansible]# 

关闭防火墙

[root@localhost ansible]# ansible all -m service -a 'name=firewalld state=stopped enabled=no' 
192.168.100.42 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "enabled": false,
    "name": "firewalld",
    "state": "stopped",
    "status": {
        "ActiveState": "active",
        "AllowedCPUs": "",
        "AllowedMemoryNodes": "",
        "BlockIOAccounting": "no",
。。。。。。

关闭selinux

[root@localhost ansible]# ansible all -m lineinfile -a 'path=/etc/selinux/config regexp="^SELINUX=" line="SELINUX=disabled"'
192.168.100.42 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "backup": "",
    "changed": true,
    "msg": "line replaced"
}
[root@localhost ansible]#

重启httpd

[root@localhost ansible]# ansible all -a '/usr/local/httpd/bin/apachectl start'
192.168.100.42 | CHANGED | rc=0 >>
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message
[root@localhost ansible]# 

访问ip进行测试

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值