• Ansible学习笔记13


    1. [root@localhost playbook]# cat example.yaml
    2. ---
    3. - hosts: group1
    4. remote_user: root
    5. tasks:
    6. - name: ensure apache is at the latest version
    7. yum: name=httpd,httpd-devel state=latest
    8. - name: write the apache config file
    9. copy: src=/etc/httpd/conf/httpd.conf dest=/etc/httpd/conf/httpd.conf
    10. notify:
    11. - restart apache
    12. - name: ensure apache is running (and enable it at boot)
    13. service: name=httpd state=started enabled=yes
    14. handlers:
    15. - name: restart apache
    16. 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:收集服务器信息。将其进行分开处理。

    有一个变量的例子:

    1. ---
    2. - hosts: group1
    3. remote_user: root
    4. tasks:
    5. - name: install vsftpd
    6. yum: name=vsftpd state=present
    7. - name: sync vsftpd.conf file
    8. copy: src=/etc/vsftpd/vsftpd.conf dest=/etc/vsftpd/vsftpd.conf
    9. notify: restart vsftpd
    10. - name: start vsftpd and enable it in boot
    11. service: name=vsftpd state=started enabled=yes
    12. handlers:
    13. - name: restart vsftpd
    14. service: name=vsftpd state=restarted

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

    1. [root@localhost ~]# ansible-playbook /etc/ansible/playbook/vsftpd_playbook.yaml
    2. PLAY [group1] *************************************************************************************
    3. TASK [Gathering Facts] ****************************************************************************
    4. ok: [192.168.17.105]
    5. ok: [192.168.17.106]
    6. TASK [install vsftpd] *****************************************************************************
    7. ok: [192.168.17.106]
    8. ok: [192.168.17.105]
    9. TASK [sync vsftpd.conf file] **********************************************************************
    10. ok: [192.168.17.105]
    11. ok: [192.168.17.106]
    12. TASK [start vsftpd and enable it in boot] *********************************************************
    13. changed: [192.168.17.105]
    14. changed: [192.168.17.106]
    15. PLAY RECAP ****************************************************************************************
    16. 192.168.17.105 : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
    17. 192.168.17.106 : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

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

    1. ---
    2. - hosts: group1
    3. remote_user: root
    4. tasks:
    5. - name: install vsftpd
    6. yum: name=vsftpd state=present
    7. # - name: sync vsftpd.conf file
    8. # copy: src=/etc/vsftpd/vsftpd.conf dest=/etc/vsftpd/vsftpd.conf
    9. - name: modify vsftpd.conf
    10. shell: sed -i '/^anonymous_enable/s/NO/YES/' /etc/vsftpd/vsftpd.conf
    11. notify: restart vsftpd
    12. - name: start vsftpd and enable it in boot
    13. service: name=vsftpd state=started enabled=yes
    14. handlers:
    15. - name: restart vsftpd
    16. service: name=vsftpd state=restarted

    python:有个特点是跨平台。

  • 相关阅读:
    高通平台开发系列讲解(AI篇)SNPE工作流程介绍
    【文末福利】半导体封装率先国产化,400+封装厂商最新名单汇总
    【云原生&微服务>SCG网关篇十一】Spring Cloud Gateway解决跨域问题
    搭建好自己的PyPi服务器后怎么使用
    Nacos整合Gateway实现动态路由
    Vue ref 和 v-for 结合(ref 源码解析)
    JAVA计算机毕业设计新生入学报到管理系统Mybatis+系统+数据库+调试部署
    【DTEmpower案例操作教程】向导式建模
    甲壳素晶须/羟丁基壳聚糖温敏水凝胶/羟乙基壳聚糖/透明质酸双网络水凝胶的制备
    java毕业设计新闻稿件管理系统Mybatis+系统+数据库+调试部署
  • 原文地址:https://blog.csdn.net/chang_chunhua/article/details/132591376