playbook样例ping模块
创建 ping.yaml
文件内容如下
---
- hosts: all
tasks:
- name: test connection
action: ping
执行
ansible-playbook ping.yaml
语法检查
ansible-playbook playbook.yml --list-task #检查tasks任务
ansible-playbook playbook.yml --list-hosts #检查生效的主机
Playbook小试牛刀
搭建HTTP服务
要求: 在目标机器中安装http服务,修改主页,使其能显示自己的IP及主 机名,运行httpd并设置为开机启动
步骤:
1. 安装httpd包
2. 使用setup模块获取IP和主机名,并注册成变量
3. 定义好template名为index.html.j2 使用变量语法写入IP和主机名
4. 使用setup获取的变量,修改index.html的内容
5. 启动httpd服务
创建index.html.j2
hello ansible!
完成httpd_deploy.yaml
的编写。
---
- name: deploy web server
hosts: local
tasks:
# 安装httpd
- name: install httpd
yum:
name: httpd
state: present
- name: set web service enabled
service:
name: httpd
enabled: true
state: started
- name: update httpd config
template:
src: index.html.j2
dest: "/var/www/html/index.html"
- name: test httpd
uri:
url: http://127.0.0.1
status_code: 200
运行起来
ansible-playbook httpd_deploy.yaml
公众号
扫码订阅最新深度技术文,回复【资源】获取技术大礼包
评论