RUNDECK+ANSIBLE实现本地脚本上传并在各目标机器上面执行

ansible 2.6.4  + rundeck-3.0.7-20181008.war

一、ansible剧本

[root@V71 rundeck3]# cat /home/ansible/playbooks/gfp_svn/execcmds.yml
---
- hosts: "{{ dsthosts }}"
  remote_user: apps
  become: "{{ becomeyesorno }}"
  vars:
    # 应用服务器发版的主机,等待RUNDECK传参
    dsthosts: 192.168.156.71
     
    #是否sudo到root执行
    becomeyesorno: no
     
    # 执行的命令
    cmds: "sh /tmp/sh2.sh"
    
  tasks:
  - name: copyshfiles     | 拷贝文件至目标待执行的机器
    copy:
      src: "/tmp/sh1.sh"
      dest: "/tmp/sh2.sh"
 
  - name: execcmds        | 执行命令
    shell: "{{ cmds }}"
    register: cmd_out
    
  - name: echo cmd_out    | 打印命令执行结果
    debug: var=cmd_out

 

 

 

二、rundeck作业,为方便查看和迁移部署,可导出为xml格式

 

<joblist>
  <job>
    <context>
      <options preserveOrder='true'>
        <option name='dsthosts' values='192.168.156.71'>
          <description>执行命令的主机</description>
        </option>
        <option name='cmds' value='"sh /tmp/sh2.sh"'>
          <description>执行的上传的脚本,脚本统一存放为/tmp/sh2.sh,默认不加参数,可以手工添加</description>
        </option>
        <option name='becomeyesorno' value='no' values='no,yes' />
        <option name='shellfiles' type='file'>
          <description>shellfiles</description>
        </option>
      </options>
    </context>
    <defaultTab>summary</defaultTab>
    <description>EXECCMD执行命令可用于关停或启动服务</description>
    <executionEnabled>true</executionEnabled>
    <id>41656e80-9339-4b16-81b4-ee4a1199056f</id>
    <loglevel>INFO</loglevel>
    <name>EXEC脚本执行命令</name>
    <nodeFilterEditable>false</nodeFilterEditable>
    <scheduleEnabled>true</scheduleEnabled>
    <sequence keepgoing='false' strategy='node-first'>
      <command>
        <node-step-plugin type='copyfile'>
          <configuration>
            <entry key='destinationPath' value='/tmp/sh1.sh' />
            <entry key='echo' value='true' />
            <entry key='recursive' value='false' />
            <entry key='sourcePath' value='${file.shellfiles}' />
          </configuration>
        </node-step-plugin>
      </command>
      <command>
        <exec>ansible-playbook  /home/ansible/playbooks/gfp_svn/execcmds.yml --extra-vars "dsthosts=${option.dsthosts} cmds=${option.cmds} becomeyesorno=${option.becomeyesorno}" </exec>
      </command>
    </sequence>
    <uuid>41656e80-9339-4b16-81b4-ee4a1199056f</uuid>
  </job>
</joblist>

 

实现脚本文件的上传主要使用了rundeck的File类型的参数,并添加Copy File步骤调用这个参数实现

 

 

三、执行效果

 

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页