ver
- name: Service Nfs Server
service: name=nfs-server state=started enabled=yes
tags: start_nfs-server
#正常执行playbook
[root@manager ~]# ansible-playbook f10.yml
PLAY [all] ********************************************************************************************************************************
TASK [Gathering Facts] ********************************************************************************************************************
ok: [10.0.0.30]
TASK [Install Nfs Server] *****************************************************************************************************************
ok: [10.0.0.30]
TASK [Service Nfs Server] *****************************************************************************************************************
ok: [10.0.0.30]
PLAY RECAP ********************************************************************************************************************************
10.0.0.30 : ok=3 changed=0 unreachable=0 failed=0
使用-t指定tags执行, 多个tags使用逗号隔开即可
[root@manager ~]# ansible-playbook -t install_nfs-server f10.yml
PLAY [all] ********************************************************************************************************************************
TASK [Gathering Facts] ********************************************************************************************************************
ok: [10.0.0.30]
TASK [Install Nfs Server] *****************************************************************************************************************
ok: [10.0.0.30]
PLAY RECAP ********************************************************************************************************************************
10.0.0.30 : ok=2 changed=0 unreachable=0 failed=0
使用--skip-tags排除不执行的tags
[root@manager ~]# ansible-playbook --skip-tags install_nfs-server f10.yml
PLAY [all] ********************************************************************************************************************************
TASK [Gathering Facts] ********************************************************************************************************************
ok: [10.0.0.30]
TASK [Service Nfs Server] *****************************************************************************************************************
ok: [10.0.0.30]
PLAY RECAP ********************************************************************************************************************************
10.0.0.30 : ok=2 changed=0 unreachable=0 failed=0
8.Playbook Handlers
playbook安装Apache示例
[root@m01 ~]# cat webserver.yml
- hosts: web
remote_user: root
#1.定义变量,在配置文件中调用
vars:
http_port: 8881
#2.安装httpd服务
tasks:
- name: Install Httpd Server
yum: name=httpd state=present
#3.使用template模板,引用上面vars定义的变量至配置文件中
- name: Configure Httpd Server
template: src=./httpd.conf dest=/etc/httpd/conf/httpd.conf
notify: Restart Httpd Server
#4.启动Httpd服务
- name: Start Httpd Server
service: name=httpd state=started enabled=yes
#5.检查Httpd服务当前的运行的端口状态
- name: Get Httpd Server Port
shell: netstat -lntp|grep httpd
register: Httpd_Port
#6.输出Httpd运行的状态至面板
- name: Out Httpd Server Status
debug: msg={{ Httpd_Port.stdout_lines }}
ignore_errors