准备两台主机
主机 | IP |
---|
client | 192.168.8.129 |
node1 | 192.168.8.130 |
创建项目文件
[root@client playbook]
.
├── 1
├── apache.yml
└── vars
└── vars_file.yml
下载APR包并解压
[root@client ~]
[root@client ~]
修改配置文件
[root@client ~]
trap "$RM \"$cfgfile\"; exit 1" 1 2 15
编写ansible变量
[root@client playbook]
user: apache
dependency_package: openssl-devel,pcre-devel,expat-devel,libtool,gcc,gcc-c++
httpd: wget https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.48.tar.gz
apr: wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-1.7.0.tar.gz
apr_util: wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-util-1.6.1.tar.gz
编写playbook
[root@client playbook]
---
- name: install apache
hosts: 192.168.8.130
vars_files:
- vars/vars_file.yml
tasks:
- name: create user
user:
name: apache
- name: install Package
dnf:
name: "{{ dependency_package }}"
state: latest
when: ansible_facts['distribution'] in platform
- name: tar packages
unarchive:
src: "{{ item }}"
copy: yes
dest: /usr/src/
mode: 0755
loop:
- /usr/src/apr-1.7.0.tar.gz
- /usr/src/apr-util-1.6.1.tar.gz
- /usr/src/httpd-2.4.48.tar.gz
- name: apr configure
copy:
src: /usr/src/apr-1.7.0/configure
dest: /usr/src/apr-1.7.0/
- name: copy script
copy:
src: /etc/ansible/playbook/apache.sh
dest: /root/
- name: mode
file:
path: /root/apache.sh
mode: 0655
- name: script
shell: "./apache.sh"
- name: restart httpd
shell: "/usr/local/httpd/bin/apachectl start"
[root@client playbook] cat apache.sh
cd /usr/src/apr-1.7.0 && ./configure --prefix=/usr/local/apr && make && make install
cd /usr/src/apr-util-1.6.1 && ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && make && make install
cd /usr/src/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@client playbook]
[WARNING]: While constructing a mapping from /etc/ansible/playbook/vars/vars_file.yml, line
1, column 1, found a duplicate dict key (compile_apr_util). Using last defined value only.
PLAY [install apache] ************************************************************************
TASK [Gathering Facts] ***********************************************************************
ok: [192.168.8.130]
TASK [user apache] ***************************************************************************
ok: [192.168.8.130]
TASK [install Package] ***********************************************************************
ok: [192.168.8.130]
TASK [wget httpd] ****************************************************************************
[WARNING]: Consider using the get_url or uri module rather than running 'wget'. If you need
to use command because get_url or uri is insufficient you can add 'warn: false' to this
command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
changed: [192.168.8.130]
TASK [wget apr] ******************************************************************************
changed: [192.168.8.130]
TASK [wget apr-util] *************************************************************************
changed: [192.168.8.130]
TASK [tar package] ***************************************************************************
[WARNING]: Consider using the unarchive module rather than running 'tar'. If you need to use
command because unarchive is insufficient you can add 'warn: false' to this command task or
set 'command_warnings=False' in ansible.cfg to get rid of this message.
changed: [192.168.8.130]
TASK [tar package] ***************************************************************************
changed: [192.168.8.130]
TASK [tar package] ***************************************************************************
changed: [192.168.8.130]
TASK [vim configure] *************************************************************************
changed: [192.168.8.130]
TASK [compile apr1] **************************************************************************
changed: [192.168.8.130]
···略
查看测试结果
