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

相关推荐
拾心215 小时前
【运维进阶】Ansible 角色管理
运维·ansible
phoenix09815 小时前
ansible部署lnmp-allinone
linux·运维·ansible
Lovyk1 天前
基于 Ansible 与 Jinja2 模板的 LNMP 环境及 WordPress 自动化部署实践
linux·运维·服务器·自动化·ansible
LLLLYYYRRRRRTT1 天前
WordPress (LNMP 架构) 一键部署 Playbook
linux·架构·ansible·mariadb
拾心211 天前
【运维进阶】LNMP + WordPress 自动化部署实验
运维·自动化·ansible·mariadb
G_H_S_3_1 天前
【网络运维】Playbook项目实战:基于 Ansible Playbook 一键部署 LNMP 架构服务器
linux·运维·服务器·网络·ansible
焄塰2 天前
Ansible 管理变量和事实
学习·centos·ansible
IT成长日记2 天前
【自动化运维神器Ansible】Playbook中的when条件判断:精细化控制任务执行
运维·自动化·ansible·playbook·when·条件判断
key_Go2 天前
4.Ansible自动化之-部署文件到主机
ansible