service 模塊:
示例一、啟動httpd服務
# ansible 192.168.85.251 -m service -a "name=httpd state=started" -i hosts
示例二、重載httpd服務
# ansible 192.168.85.251 -m service -a "name=httpd state=reloaded" -i hosts
示例三、重啟Httpd服務
ansible 192.168.85.251 -m service -a "name=httpd state=restarted" -i hosts
示例四、停止httpd服務
ansible 192.168.85.251 -m service -a "name=httpd state=stopped" -i hosts
示例五、 啟動httpd服務,並加入開機自啟
ansible 192.168.85.251 -m service -a "name=httpd state=started enabled=yes" -i hosts
[root@blog ~/project1 23:48:05]$ansible 192.168.85.251 -m service -a "name=httpd enabled=yes" -i hosts
[root@blog ~/project1 23:48:05]$ansible 192.168.85.251 -m service -a "name=httpd enabled=no" -i hosts
[root@sh_proxy html]# systemctl is-enabled httpd
disabled
[root@sh_proxy html]# systemctl is-enabled httpd
enabled
[root@sh_proxy html]#
[root@blog ~/project1 23:52:11]$vim first.yml
[root@blog ~/project1 23:58:29]$cat first.yml
---
- hosts: 192.168.85.251
tasks:
- name: Install httpd Server
yum: name=httpd state=present
# name: httpd
# state: present
- name: Configure Httpd Server
copy: src=./httpd.conf dest=/etc/httpd/conf/httpd.conf backup=yes
notify: Restart Httpd Server
- name: Systemd Httpd server
service: name=httpd state=started enabled=yes
handlers:
- name: Restart Httpd Server
service: name=httpd state=restarted
[root@blog ~/project1 23:59:32]$vim httpd.conf
[root@blog ~/project1 23:59:51]$grep -i ‘^listen httpd.conf
[root@blog ~/project1 00:00:13]$grep -i ‘^listen’ httpd.conf
[root@blog ~/project1 00:00:19]$grep -i ‘^^Csten’ httpd.conf
[root@blog ~/project1 00:00:31]$vim httpd.conf
[root@blog ~/project1 00:00:39]$grep -i '^listen' httpd.conf
Listen 9990
[root@blog ~/project1 00:00:52]$ansible-playbook first.yml -i hosts
PLAY [192.168.85.251] *******************************************************************************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************************************************************************************
ok: [192.168.85.251]
TASK [Install httpd Server] *************************************************************************************************************************************************************************************
ok: [192.168.85.251]
TASK [Configure Httpd Server] ***********************************************************************************************************************************************************************************
changed: [192.168.85.251]
TASK [Systemd Httpd server] *************************************************************************************************************************************************************************************
changed: [192.168.85.251]
RUNNING HANDLER [Restart Httpd Server] **************************************************************************************************************************************************************************
changed: [192.168.85.251]
PLAY RECAP ******************************************************************************************************************************************************************************************************
192.168.85.251 : ok=5 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@blog ~/project1 00:01:30]$