Ansible学习笔记13

bash 复制代码
[root@localhost playbook]# cat example.yaml
---
- hosts: group1
  remote_user: root
  tasks:
  - name: ensure apache is at the latest version
    yum: name=httpd,httpd-devel state=latest
 
  - name: write the apache config file
    copy: src=/etc/httpd/conf/httpd.conf dest=/etc/httpd/conf/httpd.conf
 
    notify:
    - restart apache
 
  - name: ensure apache is running (and enable it at boot)
    service: name=httpd state=started enabled=yes
 
  handlers:
    - name: restart apache
      service: name=httpd state=restarted

说明:

1)hosts:用于指定要执行的主机,其可以是一个或多个由冒号分割的主机组。

2)remote_user: 用于指定远程主机上的执行任务的用户。

3)tasks:任务列表,按顺序执行任务。

如果一个host执行task失败,整个tasks都会回滚,修正playbook中的错误,然后重新执行即可。

4)handlers:类似task, 但是需要notify通知调用。

不管多少个通知者进行notify,等到play中的所有task执行完成之后,handlers也只会被执行一次。

handlers最佳的应用场景是用来重启服务,或者触发系统重启操作,除此之外很少用到了。

练习:将httpd的端口改成8080,然后重新执行playbook。

说明:Gathering Facts:收集服务器信息。将其进行分开处理。

有一个变量的例子:

bash 复制代码
---
- hosts: group1
  remote_user: root
  tasks:
  - name: install vsftpd
    yum: name=vsftpd state=present

  - name: sync vsftpd.conf file
    copy: src=/etc/vsftpd/vsftpd.conf dest=/etc/vsftpd/vsftpd.conf

    notify: restart vsftpd

  - name: start vsftpd and enable it in boot
    service: name=vsftpd state=started enabled=yes

  handlers:
  - name: restart vsftpd
    service: name=vsftpd state=restarted

通过这个练习,我们要多写这种yaml格式的文件。注意下缩进的问题。

bash 复制代码
[root@localhost ~]# ansible-playbook /etc/ansible/playbook/vsftpd_playbook.yaml

PLAY [group1] *************************************************************************************

TASK [Gathering Facts] ****************************************************************************
ok: [192.168.17.105]
ok: [192.168.17.106]

TASK [install vsftpd] *****************************************************************************
ok: [192.168.17.106]
ok: [192.168.17.105]

TASK [sync vsftpd.conf file] **********************************************************************
ok: [192.168.17.105]
ok: [192.168.17.106]

TASK [start vsftpd and enable it in boot] *********************************************************
changed: [192.168.17.105]
changed: [192.168.17.106]

PLAY RECAP ****************************************************************************************
192.168.17.105             : ok=4    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
192.168.17.106             : ok=4    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

将playbook进行修改,使用shell模块,直接调用命令进行文件修改。

bash 复制代码
---
- hosts: group1
  remote_user: root
  tasks:
  - name: install vsftpd
    yum: name=vsftpd state=present

#  - name: sync vsftpd.conf file
#    copy: src=/etc/vsftpd/vsftpd.conf dest=/etc/vsftpd/vsftpd.conf
  - name: modify vsftpd.conf
    shell: sed -i '/^anonymous_enable/s/NO/YES/' /etc/vsftpd/vsftpd.conf

    notify: restart vsftpd

  - name: start vsftpd and enable it in boot
    service: name=vsftpd state=started enabled=yes

  handlers:
  - name: restart vsftpd
    service: name=vsftpd state=restarted

python:有个特点是跨平台。

相关推荐
初学者_xuan9 小时前
零基础新手小白快速了解掌握服务集群与自动化运维(十八)Ansible自动化模块--安装与入门
运维·自动化·ansible
Empty_77711 小时前
Ansible进行Nginx编译安装的详细步骤
linux·nginx·ansible
Lynnxiaowen18 小时前
今天我们开始学习Linux自动化运维Ansible基础
linux·运维·学习·自动化·云计算·ansible
落日漫游3 天前
Ansible主机清单:自动化管理的核心基石
运维·ansible
YJlio5 天前
自动化实践(7.25):把 PsTools 接入 PowerShell / 批处理 / Ansible
microsoft·自动化·ansible
开源Linux12 天前
Ansible高频面试题详解:30个问题从入门到精通
ansible
我爱钱因此会努力14 天前
ansible实战-不同的用户登录不同的主机
linux·运维·服务器·ansible
我爱钱因此会努力14 天前
ansible实战- 关机
linux·运维·服务器·centos·自动化·ansible
运维李哥不背锅14 天前
Ansible 模块详解:高效管理你的 IT 基础设施
服务器·网络·ansible
K_i13414 天前
Ansible模块分类与实战应用指南
ansible