收藏文章 楼主

Ansible playbook 自动化集群服务器管理示例说明和案例

版块:ansible.playbook.yml   类型:普通   作者:小绿叶技术博客   查看:1863   回复:0   获赞:0   时间:2021-04-01 16:35:25

 任务

# 案例

# cat a1.yml

---

- hosts: eisc                 # 代表eisc分组的主机被执行,all 是所有主机

  remote_user: root     #以root身份执行

  tasks:

    - name: 查看磁盘

      shell: df -h; echo " --------------------------------------------------------------"; free -m; echo "启动 ansible 写入文件" >> ansible.txt; date

      register: disk

                                    # 执行 shell 语句命令的开头不能为echo 语句,否则后面的语句直接打印出来不会被执行


    - name: 进程

      command: ps -u root

      register: ps


    - debug: msg="{{disk.stdout,ps.stdout}}"

                                    # 读取执行后的结果

    - local_action: copy content={{disk.stdout,ps.stdout}} dest=/root/aplaybook.txt

                                    # 将结果信息写入文件




# 说明

cat playbook.yml

---                                           #固定格式

- hosts: 192.168.1.31              #定义需要执行的主机或主机组

  remote_user: root                #远程用户

  vars:                                     #定义变量

    http_port: 8088                 #变量

  tasks:                                   #定义一个任务

    - name: create new file          #定义任务的名称

      file: name=/tmp/playtest.txt state=touch   #调用模块,具体要做的事情

    - name: install package

      yum: name=httpd

    - name: config httpd

      template: src=./httpd.conf dest=/etc/httpd/conf/httpd.conf

      notify:                              #触发器,当条件触发后需要做的操作,配合handlers使用

        - restart apache            #需要引用的handlers的名字

    - name: copy index.html

      copy: src=/var/www/html/index.html dest=/var/www/html/index.html

    - name: start httpd

      service: name=httpd state=started

  handlers:                            #notify定义的触发执行相应的处理动作

    - name: restart apache                     

                                             #要与notify定义的内容相同

      service: name=httpd state=restarted      

                                            #触发要执行的动作

# 相关链接

centos 安装 ansible 集群管理运用:https://www.eisc.cn/index.php?c=read&id=484&page=1



提供企业建站服务,免费网防系统,提交信息登录 http://yundun.ddoss.cn 邮箱: proposal@ddoss.cn 
回复列表
默认   热门   正序   倒序

回复:Ansible playbook 自动化集群服务器管理示例说明和案例

头像

用户名:

粉丝数:

签名:

资料 关注 好友 消息