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

相关推荐
我的运维人生21 小时前
利用Python与Ansible实现高效网络配置管理
网络·python·ansible·运维开发·技术共享
qlau20071 天前
基于kolla-ansible在AnolisOS8.6上部署all-in-one模式OpenStack-Train
ansible·openstack
Shenqi Lotus1 天前
Ansible——Playbook基本功能
运维·ansible·playbook
qlau20074 天前
基于kolla-ansible在openEuler 22.03 SP4上部署OpenStack-2023.2
ansible·openstack
水彩橘子4 天前
Semaphore UI --Ansible webui
ui·ansible
happy_king_zi5 天前
ansible企业实战
运维·ansible·devops
码上飞扬5 天前
深入浅出 Ansible 自动化运维:从入门到实战
运维·ansible·自动化运维
theo.wu5 天前
Ansible自动化部署kubernetes集群
kubernetes·自动化·ansible
xidianjiapei0015 天前
Ubuntu Juju 与 Ansible的区别
linux·ubuntu·云原生·ansible·juju
打败4045 天前
ansible_find模块
linux·ansible