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
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 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后端服务间歇性响应慢的问题排查记录