ansile(2)模块之script

ansible script模块用来为远端目标主机运行管理机(本地)的脚本

参数 是否必须 默认 选项 说明
creates no 当文件存在,这步不运行
free_form yes 运行参数
removes no 当文件不存在时,这步不允许
  1. 运行脚本示例
# cat ckl.sh 
#!/bin/bash
ps -ef | grep httpd | grep -v grep
date
echo "*******************"
# ansible webserver -m script -a "/root/ckl.sh"                                      
172.16.110.48 | SUCCESS => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to 172.16.110.48 closed.\r\n", 
    "stdout": "root      44242      1  0 22:30 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND\r\napache    44243  44242  0 22:30 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND\r\napache    44244  44242  0 22:30 ?        00:00:00
/usr/sbin/httpd -DFOREGROUND\r\napache    44245  44242  0 22:30 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND\r\napache    44246  44242  0 22:30 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND\r\napache    44247  44242  0 22:30 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND\r\nWed Dec 28 23:09:14 CST 2016\r\n*******************\r\n", 
    "stdout_lines": [
        "root      44242      1  0 22:30 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND", 
        "apache    44243  44242  0 22:30 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND", 
        "apache    44244  44242  0 22:30 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND", 
        "apache    44245  44242  0 22:30 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND", 
        "apache    44246  44242  0 22:30 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND", 
        "apache    44247  44242  0 22:30 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND", 
        "Wed Dec 28 23:09:14 CST 2016", 
        "*******************"
    ]
}
172.16.110.47 | SUCCESS => {
    "changed": true, 
    "rc": 0,
    "stderr": "Shared connection to 172.16.110.47 closed.\r\n", 
    "stdout": "root      44301      1  0 22:30 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND\r\napache    44303  44301  0 22:30 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND\r\napache    44304  44301  0 22:30 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND\r\napache    44305  44301  0 22:30 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND\r\napache    44306  44301  0 22:30 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND\r\napache    44307  44301  0 22:30 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND\r\nWed Dec 28 23:09:14 CST 2016\r\n*******************\r\n", 
    "stdout_lines": [
        "root      44301      1  0 22:30 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND", 
        "apache    44303  44301  0 22:30 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND", 
        "apache    44304  44301  0 22:30 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND", 
        "apache    44305  44301  0 22:30 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND", 
        "apache    44306  44301  0 22:30 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND", 
        "apache    44307  44301  0 22:30 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND", 
        "Wed Dec 28 23:09:14 CST 2016", 
        "*******************"
    ]
}

2.添加参数的脚本

# cat ckl.sh 
#!/bin/bash
ARG=$1
ps -ef | grep $1 | grep -v grep
date
echo "*******************"
# ansible webserver -m script -a "/root/ckl.sh sshd free_form=yes"
172.16.110.47 | SUCCESS => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to 172.16.110.47 closed.\r\n", 
    "stdout": "root       1023      1  0 Dec27 ?        00:00:00 /usr/sbin/sshd\r\nroot      10338   1023  0 Dec27 ?        00:00:00 sshd: [email protected]/0\r\nroot      46439   1023  0 23:15 ?        00:00:00 sshd: [email protected]/1\r\nroot      46517  46439  0 23:15 pts/1    00:00:00 /bin/sh -c  /root/.ansible/tmp/ansible-tmp-1482938153.95-199337961978722/ckl.sh sshd free_form=yes && sleep 0\r\nroot      46529  46517  0 23:15 pts/1    00:00:00 /bin/bash /root/.ansible/tmp/ansible-tmp-1482938153.95-199337961978722/ckl.sh sshd free_form=yes\r\nWed Dec 28 23:15:54 CST 2016\r\n*******************\r\n", 
    "stdout_lines": [
        "root       1023      1  0 Dec27 ?        00:00:00 /usr/sbin/sshd", 
        "root      10338   1023  0 Dec27 ?        00:00:00 sshd: [email protected]/0", 
        "root      46439   1023  0 23:15 ?        00:00:00 sshd: [email protected]/1", 
        "root      46517  46439  0 23:15 pts/1    00:00:00 /bin/sh -c  /root/.ansible/tmp/ansible-tmp-1482938153.95-199337961978722/ckl.sh sshd free_form=yes && sleep 0", 
        "root      46529  46517  0 23:15 pts/1    00:00:00 /bin/bash /root/.ansible/tmp/ansible-tmp-1482938153.95-199337961978722/ckl.sh sshd free_form=yes", 
        "Wed Dec 28 23:15:54 CST 2016", 
        "*******************"
    ]
}
172.16.110.48 | SUCCESS => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to 172.16.110.48 closed.\r\n", 
    "stdout": "root       1019      1  0 Dec27 ?        00:00:00 /usr/sbin/sshd\r\nroot      43902   1019  0 21:43 ?        00:00:00 sshd: [email protected]/0\r\nroot      46357   1019  0 23:15 ?        00:00:00 sshd: [email protected]/1\r\nroot      46435  46357  0 23:15 pts/1    00:00:00 /bin/sh -c  /root/.ansible/tmp/ansible-tmp-1482938153.97-107422068359222/ckl.sh sshd free_form=yes && sleep 0\r\nroot      46447  46435  0 23:15 pts/1    00:00:00 /bin/bash /root/.ansible/tmp/ansible-tmp-1482938153.97-107422068359222/ckl.sh sshd free_form=yes\r\nroot      46449  46447  0 23:15 pts/1    00:00:00 /bin/bash /root/.ansible/tmp/ansible-tmp-1482938153.97-107422068359222/ckl.sh sshd free_form=yes\r\nroot      46450  46447  0 23:15 pts/1    00:00:00 /bin/bash /root/.ansible/tmp/ansible-tmp-1482938153.97-107422068359222/ckl.sh sshd free_form=yes\r\nWed Dec 28 23:15:54 CST 2016\r\n*******************\r\n", 
    "stdout_lines": [
        "root       1019      1  0 Dec27 ?        00:00:00 /usr/sbin/sshd", 
        "root      43902   1019  0 21:43 ?        00:00:00 sshd: [email protected]/0", 
        "root      46357   1019  0 23:15 ?        00:00:00 sshd: [email protected]/1", 
        "root      46435  46357  0 23:15 pts/1    00:00:00 /bin/sh -c  /root/.ansible/tmp/ansible-tmp-1482938153.97-107422068359222/ckl.sh sshd free_form=yes && sleep 0", 
        "root      46447  46435  0 23:15 pts/1    00:00:00 /bin/bash /root/.ansible/tmp/ansible-tmp-1482938153.97-107422068359222/ckl.sh sshd free_form=yes", 
        "root      46449  46447  0 23:15 pts/1    00:00:00 /bin/bash /root/.ansible/tmp/ansible-tmp-1482938153.97-107422068359222/ckl.sh sshd free_form=yes", 
        "root      46450  46447  0 23:15 pts/1    00:00:00 /bin/bash /root/.ansible/tmp/ansible-tmp-1482938153.97-107422068359222/ckl.sh sshd free_form=yes", 
        "Wed Dec 28 23:15:54 CST 2016", 
        "*******************"
    ]
}
时间: 2024-08-08 08:14:46

ansile(2)模块之script的相关文章

ansile(2)模块之cron

ansible cron 模块就是计划任务,很简单 backup:操作之前先备份目标主机上的原计划任务 cron_file:如果指定该选项,则用该文件替换远程主机上的cron.d目录下的用户的任务计划 day: 指定日(1-31,*,*/1,--) hour:指定小时(0-23,*,*/2,--) minute:指定分钟(0-59,*,*/3,--) month:指定月(1-12,*,*/4,--) weekday:指定周(0-7,*,--) job:要执行的任务,依赖于state=presen

ansile(2)模块之get_url

ansible  get_url将文件从HTTP,HTTPS或FTP下载到节点 参数 是否必须 默认 选项 说明 attributes no None yes no 文件或目录的属性应该是.要获得支持的标志,请查看taget系统上chattr的手册页. backup no no 创建一个包括时间戳信息的备份文件,这样你可以得到原始文件,如果你不正确地弄错了. checksum no 如果将校验和传递给此参数,则将在下载目标文件的摘要后计算摘要,以确保其完整性,并验证传输是否成功完成.格式:<al

ansile(2)模块之iptable

ansible iptables 模块说明 Iptables用于设置,维护和检查Linux内核中的IP包过滤规则的表.此模块不处理保存和/或加载规则,而是仅处理存在于内存中的当前规则.这与该模块在内部使用的"iptables"和"ip6tables"命令的行为相同. 以列表参数均来自官网,如果有出入,请参考官方文档 参数 是否必需 默认 选项 说明 action no append append insert 规则是应附加在底部还是插入在顶部.如果规则已经存在,则不

ansile(2)模块之copy

ansible copy模块用来文件复制,说明如下: 参数名 选项 必须 参数说明 backup yes/no no 备份远程节点上的原始文件,在拷贝之前.如果发生什么意外,原始文件还能使用. content yes/no no 用来替代src,用于将指定文件的内容,拷贝到远程文件内 dest  yes/no yes 用于定位远程节点上的文件,需要绝对路径.如果src指向的是文件夹,这个参数也必须是指向文件夹 directory_mode  yes/no no 这个参数只能用于拷贝文件夹时候,这

ansile(2)模块之service

ansible service模块 参数选项来自官网,如果有出入,以官网为主 参数 是否必须 默认 选项 说明 arguments no 命令行参数 enabled no yes no 设置服务是否开机启动 name yes 服务名称 pattern no 定义一个模式,如果通过status指令来查看服务的状态时,没有响应,就会通过ps指令在进程中根据该模式进行查找,如果匹配到,则认为该服务依然在运行 runlevel no default 服务运行级别 sleep no 设置start和sto

ansile(2)模块之group

ansible group模块与系统命令使用相似 参数 是否必要 默认 选项 说明 gid no 选择需要设置的GID name yes 需要配置的组名 state no present present absent 添加或删除组 system no no yes no yes时添加的是系统组 添加组 # ansible dbserver -m group -a "name=zld state=present" 172.16.110.49 | SUCCESS => {     &

ansile(2)模块之file

ansbile file模块大体功能同copy模块,下面看看参数 参数 选项 必须 说明 follow yes/no no 如果原来的文件是link,拷贝后依旧是link follow yes/no no 强制文件操作 recurse yes/no no 递归设置目录的属性 group yes/no no 设定一个群组拥有拷贝到远程节点的文件权限 mode  yes/no no 等同于chmod,参数可以为"u+rwx or u=rw,g=r,o=r" owner yes/no no

ansile(2)模块之synchronize

ansible synchronize作用跟rsync很相似,具体参数如下: 参数内容,均来自官网,如果有出入,以官网为主 参数 是否必须 默认 选项 说明 archive no yes yes no rsync镜像递归标志,启用递归,链接,权限,时间,属主,属组标志和 -D checksum no no yes no 跳过基本的校验 compress no yes yes no 在传输过程中压缩文件数据.在大多数情况下,请保持启用,除非它导致问题. copy_links no no yes n

ansile(2)模块之yum

ansible yum 使用yum软件包管理器安装,升级,删除和列出软件包和组. 参数来自官网,如有出入,以官网为准 参数 是否必须 默认 选项 说明 conf_file no 用于事务的远程yum配置文件. disable_gpg_check no yes no 是否禁用GPG检查正在安装的软件包的签名.只有当状态存在或最新时才有效. disablerepo no 复制存储库以禁用安装/更新操作.这些回购将不会持续到事务之外.指定多个回购时,请用","分隔它们. enablerepo