ansible playbook使用

复制代码
- hosts: home  #对应/etc/ansible/hosts的组或者ip
  remote_user: root  #执行用户
  tasks: #任务
  - name: install httpd  #任务名
    yum: name=httpd state=present
  - name: install net-tools
    yum: name=net-tools state=present
  - name: install tree
    yum: name=tree state=present
  - name: restart httpd
    service: name=httpd state=restarted
复制代码

notify和handlers的使用

复制代码
- hosts: home
  remote_user: root
  tasks:
  - name: install httpd
    yum: name=httpd state=present
    notify: #执行完install httpd后,再触发下一个任务事件
       - restart httpd #任务事件名
  - name: install net-tools
    yum: name=net-tools state=present
  - name: install tree
    yum: name=tree state=present
  handlers: 被触发的事件,对应上面notify
  - name: restart httpd
    service: name=httpd state=restarted
    tags: rehttpd
复制代码

tags使用

复制代码
- hosts: home
  remote_user: root
  tasks:
  - name: install httpd
    yum: name=httpd state=present
    notify:
       - restart httpd
  - name: install net-tools
    yum: name=net-tools state=present
  - name: install tree
    yum: name=tree state=present
  handlers:
  - name: restart httpd
    service: name=httpd state=restarted
    tags: rehttpd #标记这个任务,名字可以自定义
复制代码
ansible-playbook --tags=rehttpd cs.yml #只执行文件里面rehttpd的任务
ansible-playbook --skip-tags=rehttpd cs.yml #排除rehttpd任务,执行其他任务

 使用sudo执行任务

tasks: 
  - name: run df -h
    sudo_user: test  #账号
    sudo: yes  #使用sudo执行
    shell: name=df -h

when使用

复制代码
- hosts: gs
  remote_user: root
  tasks:
  - name: install httpd
    yum: name=httpd state=installed
    notify:
       - restart httpd
    when:
      - ansible_distribution == "Centos" #满足条件系统为centos
      - ansible_distribution_major_version == "6" #满足条件系统为centos6,两个条件都满足才能执行这个任务
  - name: install net-tools
    yum: name=net-tools state=installed
  - name: install tree
    yum: name=tree state=installed
  handlers:
  - name: restart httpd
    service: name=httpd state=restarted
    tags: rehttpd
复制代码
复制代码
- hosts: gs
  remote_user: root
  tasks:
  - name: install httpd
    yum: name=httpd state=installed
    notify:
       - restart httpd
    when: (ansible_distribution ==  "CentOS" and ansible_distribution_major_version == "7") or (ansible_distribution ==  "Centos" and ansible_distribution_major_version == "6") #满足系统是centos7或者centos6就可以执行
  - name: install net-tools
    yum: name=net-tools state=installed
  - name: install tree
    yum: name=tree state=installed
  handlers:
  - name: restart httpd
    service: name=httpd state=restarted
    tags: rehttpd
复制代码

整个任务里面有出现重复调用同个模板,可以使用with_items让用同个模板的任务在一个任务里面完成

- name: "Add groups"  #创建用户组
  group: name={{ item.name }} state=present
  with_items:
    - { name: "zhang" }  #创建用户组zhang
    - { name: "zhao" }   #创建用户组zhao
  tags:
  - Add_group
- name: "Add Users" #创建账号
  user: name={{ item.name }} state=present groups={{ item.groups }}
  with_items:
    - { name: "hu", groups: "zhang" }
    - { name: "zhi", groups: "zhao" }
  tags:
  - Add_users
复制代码
- name: "userdel user" #删除账号
  user: name={{ item.name }} state=absent  remove=yes
  with_items:
    - { name: "hu" }
    - { name: "zhi" }
    - { name: "zhangsan" }
    - { name: "lisi" }
    - { name: "zhaosi" }
    - { name: "zhangsi" }
    - { name: "test1" }
  tags:
  - Del_Users
复制代码

 

posted @   菜鸟web  阅读(260)  评论(0编辑  收藏  举报
编辑推荐:
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
· 对象命名为何需要避免'-er'和'-or'后缀
· SQL Server如何跟踪自动统计信息更新?
· AI与.NET技术实操系列:使用Catalyst进行自然语言处理
阅读排行:
· dotnet 源代码生成器分析器入门
· 官方的 MCP C# SDK:csharp-sdk
· 一款 .NET 开源、功能强大的远程连接管理工具,支持 RDP、VNC、SSH 等多种主流协议!
· 一文搞懂MCP协议与Function Call的区别
· 一次Java后端服务间歇性响应慢的问题排查记录
点击右上角即可分享
微信分享提示