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

相关推荐
早睡冠军候选人2 天前
Ansible学习----Ansible Playbook
运维·服务器·学习·云原生·容器·ansible
yourkin6665 天前
Ansible
ansible
Ribou5 天前
ansible 自动安装软件,实现列表选择软件的方法
ansible
文静小土豆5 天前
Ansible 自动化部署K8S1.34.1
kubernetes·自动化·ansible
K_i1347 天前
Helm 与 Ansible 深度对比解析文档
自动化·ansible
zmjjdank1ng8 天前
什么是Ansible 清单
服务器·自动化·ansible
Yyyy4828 天前
ansible role配apt源
ansible
K_i13414 天前
Ansible实战:VMware下K8s自动化部署指南
kubernetes·自动化·ansible
许泽宇的技术分享14 天前
Ansible核心架构深度剖析:从源码看IT自动化的“简单“哲学
python·ansible·自动化运维·devops·it基础设施
荣光波比14 天前
Ansible(三)—— 使用Ansible自动化部署LNMP环境实战指南
运维·自动化·云计算·ansible