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:有个特点是跨平台。

相关推荐
元气满满-樱14 小时前
Ansible-Playbook
ansible
元气满满-樱18 小时前
ansible部署nfs
ansible
码界奇点19 小时前
基于Django与Ansible的自动化运维管理系统设计与实现
运维·python·django·毕业设计·ansible·源代码管理
嘻哈baby2 天前
Ansible自动化运维入门:从手工到批量部署
运维·自动化·ansible
Warren982 天前
面试和投简历闲聊
网络·学习·docker·面试·职场和发展·eureka·ansible
乾元2 天前
Syslog / Flow / Telemetry 的 AI 聚合与异常检测实战(可观测性)
运维·网络·人工智能·网络协议·华为·自动化·ansible
我是koten3 天前
用Ansible查找文件并记录文件名的playbook
linux·运维·centos·ssh·ansible·find·playbook
jcsx3 天前
采用ansible收集多个centos6主机的一个特定日志文件vsftpd.log的后3000行
ansible
广目软件4 天前
GM DC Monitor一体化监控预警平台部署手册2025-12-10
运维·自动化·ansible·zabbix·运维开发·prometheus
tianyuanwo5 天前
Ansible构建节点管理:Koji与Mock构建节点的自动化运维实践
运维·自动化·ansible