ansible testhost -m copy -a "src=/etc/ansible  dest=/tmp/ansibletest owner=root group=root mode=0755"
 
注意:源目录会放到目标目录下面去,如果目标指定的目录不存在,它会自动创建。如果拷贝的是文件,dest指定的名字和源如果不同,并且它不是已经存在的目录,相当于拷贝过去后又重命名。但相反,如果dest是目标机器上已经存在的目录,则会直接把文件拷贝到该目录下面。
 
ansible testhost -m copy -a "src=/etc/passwd dest=/tmp/1.txt"
这里的/tmp/123和源机器上的/etc/passwd是一致的,但如果目标机器上已经有/tmp/123目录,则会再/tmp/123目录下面建立passwd文件
 
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 "
 
Ansible实现任务计划
ansible web10.aming.com -m cron -a "name=test_cron job='/bin/touch /tmp/1212.txt' weekday=6"
 
crontab -l 客户端查看
若要删除该cron 只需要加一个字段 state=absent
 
ansible testhost -m cron -a "name=test_cron state=absent"
其他的时间表示:分钟 minute 小时 hour 日期 day 月份 month
 
Ansible安装rpm包/管理服务
ansible testhost -m yum -a "name=httpd "
在name后面还可以加上state=installed
 
ansible testhost -m service -a "name=httpd state=started enabled=yes"
这里的name是centos系统里的服务名,可以通过chkconfig --list查到。
 
Ansible文档的使用
ansible-doc -l   列出所有的模块
ansible-doc cron  查看指定模块的文档
Ansible playbook的使用
 
相当于把模块写入到配置文件里面,例:
vim  /etc/ansible/test.yml
---
  remote_user: root
  tasks:
    - name: test_playbook
      shell: touch /tmp/lishiming.txt
 
说明: hosts参数指定了对哪些主机进行参作;
user参数指定了使用什么用户登录远程主机操作;
tasks指定了一个任务,其下面的name参数同样是对任务的描述,在执行过程中会打印出来。
执行:ansible-playbook test.yml
 
 
再来一个创建用户的例子:
vim  /etc/ansible/create_user.yml
---
- name: create_user
  hosts: web10.aming.com
  user: root
  gather_facts: false
  vars:
    - user: "test"
  tasks:
    - name: create user
      user: name="{{ user }}"
 
 
说明: name参数对该playbook实现的功能做一个概述,后面执行过程中,会打印 name变量的值 ,可以省略;gather_facts参数指定了在以下任务部分执行前,是否先执行setup模块获取主机相关信息,这在后面的task会使用到setup获取的信息时用到;vars参数,指定了变量,这里指字一个user变量,其值为test ,需要注意的是,变量值一定要用引号引住;user提定了调用user模块,name是user模块里的一个参数,而增加的用户名字调用了上面user变量的值。
 
客户端查看 创建的用户test   grep test /etc/passwd
 
Ansible playbook中的循环
---
- hosts: testhost
  user: root
  tasks:
    - name: change mode for files
      file: path=/tmp/{{ item }} mode=600
      with_items:
        - 1.txt
        - 2.txt
                 - 3.txt
 
需要在web9, web10 机器上先创建文件 touch /tmp/{1.txt,2.txt,3.txt} 才能执行修改权限操作。

ansible实践2-拷贝文件或目录的更多相关文章

  1. Linux centosVMware 自动化运维Ansible介绍、Ansible安装、远程执行命令、拷贝文件或者目录、远程执行脚本、管理任务计划、安装rpm包/管理服务、 playbook的使用、 playbook中的循环、 playbook中的条件判断、 playbook中的handlers、playbook实战-nginx安装、管理配置文件

    一.Ansible介绍 不需要安装客户端,通过sshd去通信 基于模块工作,模块可以由任何语言开发 不仅支持命令行使用模块,也支持编写yaml格式的playbook,易于编写和阅读 安装十分简单,ce ...

  2. linux cp 拷贝文件或目录

    cp 拷贝文件或目录 默认不能拷贝目录 常用来备份: [root@MongoDB ~]# cp a.txt /tmp/ [root@MongoDB ~]# cp /root/a.txt /tmp/ c ...

  3. Ansible 拷贝文件或目录

    写法如下: [root@localhost ~]$ ansible 192.168.119.134 -m copy -a "src=/etc/passwd dest=/tmp/passwd ...

  4. Ansible 删除多个文件或目录

    翻译和转载该网页内容 http://www.mydailytutorials.com/ansible-delete-multiple-files-directories-ansible/ 背景 ans ...

  5. Linux 命令 - cp: 拷贝文件和目录

    命令格式 cp [OPTION]... [-T] SOURCE DEST cp [OPTION]... SOURCE... DIRECTORY cp [OPTION]... -t DIRECTORY ...

  6. Linux 使用 scp 命令远程拷贝文件和目录

    使用方法: $ scp --help scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file] [-l limit] [-o s ...

  7. ansible 文件和目录操作

    ansible file 模块参考: refer to https://docs.ansible.com/ansible/latest/modules/file_module.html?highlig ...

  8. Linux 文件与目录管理

    Linux 文件与目录管理 我们知道Linux的目录结构为树状结构,最顶级的目录为根目录 /. 其他目录通过挂载可以将它们添加到树中,通过解除挂载可以移除它们. 在开始本教程前我们需要先知道什么是绝对 ...

  9. AIR文件操作:使用文件对象操作文件和目录 .

    来源:http://blog.csdn.net/zdingxin/article/details/6635376 在AIR中可以方便的对本地文件操作,不过上次做了个项目,发现还是有不少不方便的地方,比 ...

随机推荐

  1. 图像变换之Census变换

    图像的Census变换 Census变换属于非参数图像变换的一种,它能够较好地检测出图像中的局部结构特征,如边缘.角点特征等.传统Census变换的基本思想是:在图像区域定义一个矩形窗口,用这个矩形窗 ...

  2. 【开发技术】refactor 重构----实现文件改名

    当我们要改类名或接口名时,可能会遇到该类(接口)在其它文件中也有使用的情况,如一个个找比较麻烦也容易漏,这里推荐使用右键refactor->rename进行修改.

  3. Python random模块(获取随机数)

    1.random.random 随机生成一个0到1的随机浮点数: 0 <= n < 1.0 In [2]: print random.random() 0.544824016934 2.r ...

  4. BOM基础知识

    1.什么是BOM      BOM(Browser Object Document)即浏览器对象模型.      BOM提供了独立于内容 而与浏览器窗口进行交互的对象:      由于BOM主要用于管 ...

  5. java8-新特性--(接口的默认方法与静态方法)

    Java 8用默认方法与静态方法这两个新概念来扩展接口的声明. public interface Inte{ void method(); default void defaultMethod(){ ...

  6. linux mysql 修改 UTF-8编码

    版本大于5.5 [mysqld]下添加的应该为:   character-set-server=utf8   collation-server=utf8_general_ci 版本小于5.5 [cli ...

  7. CSS深入理解学习笔记之z-index

    1.z-index基础 z-index含义:指定了元素及其子元素的"z顺序",而"z顺序"可以决定元素的覆盖顺序.z-index值越大越在上面. z-index ...

  8. 关于MyEclipse启动报错:Error starting static Resources;下面伴随Failed to start component [StandardServer[8005]]; A child container failed during start.的错误提示解决办法.

    最后才发现原因是Tomcat的server.xml配置文件有问题:apache-tomcat-7.0.67\conf的service.xml下边多了类似与 <Host appBase=" ...

  9. The man Command

    The man command is used to format and display the man pages. The man pages are a user manual that is ...

  10. 关于主机用户名显示为"-bash-4.1$"

    牢记  <1> .(小数点)==> 代表一个任意字符   <2> *(星)====> 重复前面一个字符0到无穷次 1.出现这中情况是因为:用户环境变量的文件没了,通 ...