Ansible远程执行脚本

1.05 Ansible远程执行脚本

 首先创建一个shell脚本
 vim  /tmp/test.sh  //加入内容
 #!/bin/bash
 echo `date` > /tmp/ansible_test.txt
 然后把该脚本分发到各个机器上
 ansible testhost -m copy -a "src=/tmp/test.sh dest=/tmp/test.sh mode=0755"
 最后是批量执行该shell脚本
 ansible testhost -m shell -a "/tmp/test.sh"
 shell模块,还支持远程执行命令并且带管道
 ansible testhost -m shell -a "cat /etc/passwd|wc -l "
[root@Dasoncheng ~]# ansible testhost -m copy -a 'src=/tmp/test.sh dest=/tmp/test.sh mode=755'
127.0.0.1 | SUCCESS => {
    "changed": true, 
    "checksum": "1a6e4af02dba1bda6fc8e23031d4447efeba0ade", 
    "dest": "/tmp/test.sh", 
    "gid": 0, 
    "group": "root", 
    "mode": "0755", 
    "owner": "root", 
    "path": "/tmp/test.sh", 
    "size": 48, 
    "state": "file", 
    "uid": 0
}
192.168.60.12 | SUCCESS => {
    "changed": true, 
    "checksum": "1a6e4af02dba1bda6fc8e23031d4447efeba0ade", 
    "dest": "/tmp/test.sh", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "edfaa4371316af8c5ba354e708fe8a97", 
    "mode": "0755", 
    "owner": "root", 
    "size": 48, 
    "src": "/root/.ansible/tmp/ansible-tmp-1524218004.15-84846665116766/source", 
    "state": "file", 
    "uid": 0
}
cdn002 | SUCCESS => {
    "changed": false, 
    "checksum": "1a6e4af02dba1bda6fc8e23031d4447efeba0ade", 
    "dest": "/tmp/test.sh", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "edfaa4371316af8c5ba354e708fe8a97", 
    "mode": "0755", 
    "owner": "root", 
    "size": 48, 
    "src": "/root/.ansible/tmp/ansible-tmp-1524218004.2-83967531119849/source", 
    "state": "file", 
    "uid": 0
}
[root@Dasoncheng ~]# ansible 127.0.0.1,cdn002 -m shell -a '/tmp/test.sh'
cdn002 | SUCCESS | rc=0 >>  
127.0.0.1 | SUCCESS | rc=0 >>
[root@Dasoncheng ~]# ansible cdn002 -m shell -a 'cat /tmp/ansible_test.txt'
cdn002 | SUCCESS | rc=0 >>
Fri Apr 20 17:54:57 CST 2018
[root@Dasoncheng ~]# ansible cdn002 -m shell -a 'cat /etc/passwd | wc -l'
cdn002 | SUCCESS | rc=0 >>
23

1.06 ansible管理任务计划

 ansible testhost -m cron -a "name='test cron' job='/bin/touch /tmp/1212.txt'  weekday=6"

 若要删除该cron 只需要加一个字段 state=absent 
 ansible testhost -m cron -a "name='test cron' state=absent"

 其他的时间表示:分钟 minute 小时 hour 日期 day 月份 month
[root@Dasoncheng ~]# ansible testhost -m cron -a "name='test_cron' job='/bin/touch /tmp/1212.txt' weekday=6"
127.0.0.1 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "test_cron"
    ]
}
192.168.60.12 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "test_cron"
    ]
}
cdn002 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "test_cron"
    ]
}
[root@Dasoncheng ~]# ansible cdn002 -m command -a 'crontab -l'
cdn002 | SUCCESS | rc=0 >>
#Ansible: test_cron
* * * * 6 /bin/touch /tmp/1212.txt

[root@Dasoncheng ~]# ansible cdn002 -m cron -a "name='test_cron' state=absent"
cdn002 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": []
}
[root@Dasoncheng ~]# ansible cdn002 -m command -a 'crontab -l'
cdn002 | SUCCESS | rc=0 >>

1.07 Ansible安装rpm包/管理服务

 ansible testhost -m yum -a "name=httpd" 
 在name后面还可以加上state=installed/removed
 ansible testhost -m service -a "name=httpd state=started enabled=yes" 
 这里的name是centos系统里的服务名,可以通过chkconfig --list查到。
 Ansible文档的使用
 ansible-doc -l   列出所有的模块
 ansible-doc cron  查看指定模块的文档
[root@Dasoncheng ~]# ansible cdn002 -m yum -a "name=httpd state=installed"
cdn002 | SUCCESS => {
    "changed": true, 
    "msg": "", 
    "rc": 0, 
    "results": 
……
……
[root@Dasoncheng ~]# ansible cdn002 -m service -a "name=httpd state=started enabled=yes"
cdn002 | SUCCESS => {
    "changed": true, 
    "enabled": true, 
    "name": "httpd", 
    "state": "started", 
    "status": {
……
……
[root@Dasoncheng ~]# ansible cdn002 -m command -a 'systemctl status httpd'
cdn002 | SUCCESS | rc=0 >>
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2018-04-20 18:37:55 CST; 8s ago
……
……

1.08 Ansible文档的使用

[root@Dasoncheng ~]# ansible-doc -l
a10_server                                           Manage A10 Networks AX/SoftAX/Thunder/vThund...
a10_server                                           Manage A10 Networks AX/SoftAX/Thunder/vThund...
a10_server_axapi3                                    Manage A10 Networks AX/SoftAX/Thunder/vThund...
[root@Dasoncheng ~]# ansible-doc cron
> CRON    (/usr/lib/python2.7/site-packages/ansible/modules/system/cron.py)

        Use this module to manage crontab and environment variables entries. This
        module allows you to create environment variables and named crontab
        entries, update, or delete them. When crontab jobs are managed: the
        module includes one line with the description of the crontab entry
        `"#Ansible: <name>"' corresponding to the "name" passed to the module,
        which is used by future ansible/module calls to find/check the state. The
        "name" parameter should be unique, and changing the "name" value will
        result in a new cron task being created (or a different one being
        removed). When environment variables are managed: no comment line is
        added, but, when the module needs to find/check the state, it uses the
        "name" parameter to find the environment variable definition line. When
        using symbols such as %, they must be properly escaped.

OPTIONS (= is mandatory):

- backup
        If set, create a backup of the crontab before it is modified. The
        location of the backup is returned in the `backup_file' variable by this
        module.
        [Default: no]
        type: bool

- cron_file
        If specified, uses this file instead of an individual user's crontab. If
        this is a relative path, it is interpreted with respect to /etc/cron.d.
        (If it is absolute, it will typically be /etc/crontab). Many linux
        distros expect (and some require) the filename portion to consist solely
        of upper- and lower-case letters, digits, underscores, and hyphens. To
        use the `cron_file' parameter you must specify the `user' as well.
        [Default: (null)]
……
……

转载于:https://my.oschina.net/u/3651233/blog/1798644

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 使用Ansible执行shell脚本可以通过在Ansible Playbook中使用`shell`模块来实现。具体步骤如下: 1. 创建一个Ansible Playbook文件,并在其中定义一个任务,该任务使用`shell`模块来执行shell脚本。 例如,下面的代码演示了如何使用`shell`模块执行一个简单的shell脚本: ``` - name: 执行shell脚本 hosts: your_host tasks: - name: 运行脚本 shell: /path/to/your/script.sh ``` 在上面的示例中,`/path/to/your/script.sh`是要执行shell脚本的路径。可以将此路径替换为自己的脚本路径。 2. 保存并运行Ansible Playbook文件。可以使用`ansible-playbook`命令来运行Playbook文件,例如: ``` ansible-playbook your_playbook.yml ``` 在执行过程中,Ansible将连接到远程主机执行指定的shell脚本。 注意:在执行shell脚本时,请确保有足够的权限来运行脚本,并且脚本本身是可执行的。此外,还应确保在Playbook文件中正确指定了主机和用户名等必要的信息。 ### 回答2: Ansible是一个使用Python语言编写的自动化工具,它可以管理多台远程主机,批量执行指定的任务。Ansible可以通过配置文件和模块来实现各种自动化任务,其中执行Shell脚本就是其中一种任务。 执行Shell脚本Ansible自动化工具的一项重要功能。通过Ansible执行一个Shell脚本,可以批量地运行一系列命令,并且可以避免手动在远程主机执行任务的风险和不可控性。 要在Ansible执行Shell脚本,首先需要编写一个playbook文件,包含以下基本元素:主机列表、任务列表、变量、模块以及条件控制结构。下面将逐一介绍这些基本元素。 1. 主机列表 主机列表指定需要执行Shell脚本远程主机。可以通过IP地址、主机名或标记来指定主机列表。主机列表可以单独指定,也可以通过变量定义。 示例: ``` --- - name: Execute shell scripts on remote hosts hosts: all become: true tasks: - name: Run script on remote hosts shell: | /bin/bash /path/to/script.sh ``` 2. 任务列表 任务列表包含要在远程主机执行的任务。任务列表可以包含多个任务,每个任务都有自己的名称、模块、参数和变量。 示例: ``` tasks: - name: Run script on remote hosts shell: | /bin/bash /path/to/script.sh ``` 3. 变量 变量允许向playbook传递自定义参数和设置。最常见的变量类型是主机参数和组参数。 示例: ``` vars: script_name: script.sh tasks: - name: Run script on remote hosts shell: | /bin/bash /path/to/{{ script_name }} ``` 4. 模块 模块是Ansible中最常用的元素之一,它定义了可以执行远程任务的操作。例如,如果要在远程主机执行Shell脚本,则需要使用“shell”模块。 示例: ``` tasks: - name: Run script on remote hosts shell: | /bin/bash /path/to/script.sh ``` 5. 条件控制结构 条件控制结构允许根据主机或组的条件来选择要执行的任务。 示例: ``` tasks: - name: Run script on remote hosts shell: | /bin/bash /path/to/script.sh when: "'db' in group_names" ``` 总的来说,在Ansible执行Shell脚本是一个非常常见和有用的任务。通过编写一个playbook文件,按照上述基本元素进行配置,即可实现批量执行Shell脚本的自动化过程。在执行过程中,需要注意一些技巧,例如使用“shell”模块来指定脚本路径和参数,使用条件控制结构来选择要运行的主机,以及使用变量来传递自定义参数等。 ### 回答3: Ansible是一款自动化工具,它可用于管理、协调和自动化IT基础设施配置。Ansible执行多个任务时非常有用,这些任务需要在一个设每台主机运行,而且需要一些脚本来管理它们。 在Ansible执行shell脚本可以通过使用Shell Module来实现。Shell Module是Ansible的一个核心模块,它允许用户在远程主机执行shell脚本或命令。 在Ansible Playbook中使用Shell Module执行脚本的基本语法如下: ```yaml - name: Execute script on remote host hosts: remote-server gather_facts: no tasks: - name: Run script shell: /path/to/script.sh ``` 在上面的示例中,playbook将在`remote-server`主机执行`/path/to/script.sh`脚本。`gather_facts`选项已经设置为no,因为该选项允许收集有关主机的事实信息。在执行脚本时,您可能不需要这些信息。 您还可以使用`args`参数来传递脚本的参数。下面是一个示例: ```yaml - name: Execute script on remote host hosts: remote-server gather_facts: no tasks: - name: Run script shell: /path/to/script.sh args: arg1: value1 arg2: value2 ``` 在上面的示例中,如果你的脚本需要两个参数,那么你可以使用args参数来传递这两个值。这使得在一行中完整传递所有参数变得更加容易。 总的来说,使用Ansible执行shell脚本是非常简单的。您只需要使用Shell Module,并提供要在目标主机执行脚本的路径即可。 无论您是执行基本shell命令还是执行复杂的脚本Ansible都是一个高效且易于使用的工具,帮助您管理、协调和自动化IT基础设施。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值