编写yml文件
yum文件严格遵守两个空格缩进的格式。
- 发布httpd并启动
[devops@server1 ansible]$ cat playbook.yml
---
- hosts: prod
tasks:
- name: install httpd
yum:
name: httpd
state: present
- name: start httpd
service:
name: httpd
state: started
[devops@server1 ansible]$ ansible-playbook playbook.yml --syntax-check##检测文件语法正确性

[devops@server1 ansible]$ ansible-playbook playbook.yml --list-task##检测任务

[devops@server1 ansible]$ ansible-playbook playbook.yml --list-hosts##检测生效主机

[devops@server1 ansible]$ ansible-playbook playbook.yml ##执行文件

server3查看结果

- 添加配置文件
[devops@server1 ansible]$ cat playbook.yml
---
- hosts: prod
tasks:
- name: install httpd
yum:
name: httpd
state: present
- name: configure httpd
copy:
src: files/httpd.conf
dest: /etc/httpd/conf/httpd.conf
owner: root
group: root
mode: 644
notify: restart httpd
- name: start httpd
service:
name: httpd
state: started
handlers:
- name: restart httpd
service:
name: httpd
state: restarted
[devops@server1 ansible]$ mkdir files
[devops@server1 ansible]$ cd files
[devops@server1 files]$ scp root@172.25.32.3:/etc/httpd/conf/httpd.conf .
httpd.conf 100% 11KB 11.5KB/s 00:00
[devops@server1 files]$ ls
httpd.conf

[devops@server1 ansible]$ vi files/httpd.conf


查看结果

还原配置文件后再次运行yml文件

- 添加火墙与默认发布文件
---
- hosts: prod
tasks:
- name: install httpd
yum:
name: httpd
state: present
- name: copy index.html
copy:
src: files/index.html
dest: /var/www/html/index.html
- name: configure httpd
copy:
src: files/httpd.conf
dest: /etc/httpd/conf/httpd.conf
owner: root
group: root
mode: 644
notify: restart httpd
- name: start httpd and firewalld
service:
name: "{{ item }}"
state: started
loop:
- httpd
- firewalld
- name: configure firewalld
firewalld:
service: http
permanent: yes
immediate: yes
state: enabled
handlers:
- name: restart httpd
service:
name: httpd
state: restarted
- hosts: localhost
become: no
tasks:
- name: test http
uri:
url: http://172.25.32.3
status_code: 200

访问测试:

- 给所有成员安装httpd并发布默认文件
---
- hosts: all
vars:
http_port: 80
tasks:
- name: install httpd
yum:
name: httpd
state: present
- name: copy index.html
copy:
src: files/index.html
dest: /var/www/html/index.html
- name: configure httpd
template:
src: templates/httpd.conf.j2
dest: /etc/httpd/conf/httpd.conf
owner: root
group: root
mode: 644
notify: restart httpd
- name: start httpd and firewalld
service:
name: "{{ item }}"
state: started
loop:
- httpd
- firewalld
- name: configure firewalld
firewalld:
service: http
permanent: yes
immediate: yes
state: enabled
handlers:
- name: restart httpd
service:
name: httpd
state: restarted
- hosts: localhost
tasks:
- name: test httpd
uri:
url: http://172.25.32.3
status_code: 200

[devops@server1 ansible]$ mkdir templates
[devops@server1 ansible]$ cp files/httpd.conf templates/httpd.conf.j2
[devops@server1 ansible]$ vim templates/httpd.conf.j2

推送
[devops@server1 ansible]$ ansible-playbook playbook.yml
在server2和server3查看端口
[root@server2 .ssh]# vi /etc/httpd/conf/httpd.conf
[root@server3 .ssh]# vi /etc/httpd/conf/httpd.conf


[root@server2 .ssh]# netstat -lnput | grep httpd
[root@server3 .ssh]# netstat -lnput | grep httpd



[devops@server1 ansible]$ cat file.yml
- hosts: all
tasks:
- name: create infofile
template:
src: templates/info.j2
dest: /mnt/hostinfo
[devops@server1 ansible]$ cat templates/info.j2
主机名: {{ ansible_facts['hostname'] }}
boot分区: {{ ansible_facts['devices']['sda']['partitions']['sda1']['size'] }}
内核: {{ ansible_facts['kernel'] }}
内存空闲: {{ ansible_facts['memfree_mb'] }}
编译运行

在server2上查看

本文详细介绍使用Ansible编写YAML文件实现HTTPD服务的自动化安装、配置与启动过程,包括复制配置文件、启动服务、配置防火墙及远程测试访问。
562

被折叠的 条评论
为什么被折叠?



