Ansible案例

系统初始化
ansible-playbook /root/ansible/yaml/initos-playbook.yaml --tags="initos" --list-hosts

[root@prome-01 file]# cat  /root/ansible/yaml/initos-playbook.yaml
- hosts: initos                                            #定义需要执行主机
  remote_user: root                                        #远程用户
  vars:
    file: /opt/initos/
  tags: initos
  tasks:                                                   #定义一个任务的开始
    - name: create new dir                                 #定义任务的名称
      file: name=/opt/initos state=directory               #调用模块,具体要做的事情
    - name: "copy files"
      copy:
        src: "{{ item.src }}"
        dest: "{{ item.dest }}"
        owner: root
        group: root
        mode: 755
      with_items:
        - {src: "/etc/ansible/file/360-wins(192.66.255.174_8080_8090).exe", dest: "/opt/initos/" }
        - {src: "/etc/ansible/file/360linuxc(192.66.255.174_8080_8090).sh", dest: "/opt/initos/" }
        - {src: "/etc/ansible/file/jgjclvcentos7.sh", dest: "/opt/initos/" }
        - {src: "/etc/ansible/file/clv1.sh", dest: "/opt/initos/" }
        - {src: "/etc/ansible/file/yum.sh", dest: "/opt/initos/" }
    - name: yuminstall
      tags: yum
      shell: cd /opt/initos/;/bin/sh yum.sh
    - name: exroot
      tags: exroot
      shell: cd /opt/initos/;/bin/sh clv1.sh
    - name: datemount
      tags: datemount
      shell: cd /opt/initos/;/bin/sh jgjclvcentos7.sh


- hosts: openssh                                            #定义需要执行主机
  tags: openssh
  remote_user: root                                         #远程用户
  tasks:                                                    #定义一个任务的开始
    - name: create new dir                                  #定义任务的名称
      file: name=/opt/openssh state=directory               #调用模块,具体要做的事情
    - name: "copy files"
      copy:
        src: "{{ item.src }}"
        dest: "{{ item.dest }}"
        owner: root
        group: root
        mode: 755
      with_items:
        - {src: "/etc/ansible/file/openssh-9.6p1.tar.gz", dest: "/opt/openssh/" }
        - {src: "/etc/ansible/file/openssl-1.1.1q.tar.gz", dest: "/opt/openssh/" }
        - {src: "/etc/ansible/file/zlib-1.3.tar.gz", dest: "/opt/openssh/" }
        - {src: "/etc/ansible/file/openssh.sh", dest: "/opt/openssh/" }
    - name: Execute the script
      shell: cd /opt/openssh/;sh openssh.sh

批量安装软件包

[root@manager ~]# cat f7.yml
---
- hosts: all
  remote_user: root
  tasks:
    - name: Installed Pkg
      yum: name={{ item }} state=present
      with_items:
        - wget
        - tree
        - lrzsz

标准循环使用场景-批量创建用户

[root@manager ~]# cat f7.yml
- hosts: all
  remote_user: root
  tasks:
    - name: Add Users
      user: name={{ item.name }} groups={{ item.groups }} state=present
      with_items:
        - { name: 'testuser1', groups: 'bin' }
        - { name: 'testuser2', groups: 'root' }

标准循环使用场景-拷贝多个目录

[root@manager ~]# cat f7.yml
- hosts: all
  remote_user: root
  tasks:
    - name: Configure Rsync Server
      copy: src={{ item.src }} dest=/etc/{{ item.dest }} mode={{ item.mode }}
      with_items:
        - {src: "rsyncd.conf", dest: "rsyncd.conf", mode: "0644"}
        - {src: "rsync.passwd", dest: "rsync.passwd", mode: "0600"}
posted @   *一炁化三清*  阅读(8)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
点击右上角即可分享
微信分享提示