Chinaunix首页 | 论坛 | 博客
  • 博客访问: 382376
  • 博文数量: 112
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 800
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-29 13:41
文章分类

全部博文(112)

文章存档

2020年(1)

2018年(10)

2017年(27)

2016年(18)

2015年(31)

2014年(25)

分类: 系统运维

2016-05-10 16:50:27

Ad-Hoc 是指ansible下临时执行的一条命令,并且不需要保存的命令。Ad-Hoc通过模块的方式来完成一些远程的管理工作。Ad-Hoc不指定模块执行时,默认使用command模块,可以修改/etc/ansible/ansible.cnf配置中的“#module_name = command”字段。

点击(此处)折叠或打开

  1. 查看所有模块
  2. #ansible-doc -l
  3. 查看某个某块的参数具体用法
  4. #ansible-doc -s module
  5. 查看该模块更详细的信息
  6. #ansible-doc help module
1、setup(用于获取主机信息)
     在playbook里经常会用到的一个参数gather_facts与该模块相关。
       常用的参数filter。

点击(此处)折叠或打开

  1. 远程主机的一些基本信息
  2. #ansible test -m setup

  3. #ansible -i /etc/ansible/hosts test -m setup
  4. #ansible web1 -m setup -a 'filter=ansible_*_mb'   #查看主机内存信息。
  5. #ansible web1 -m setup -a 'filter=ansible_eth[0-1]'    #查看网络接口为eth0-eth1的网卡信息。
  6. #ansible web1 -m setup --tree /tmp/facts    #将web1内主机信息输入到本地/tmp/facts目录下,每台主机的信息输入到主机名文件中(/etc/ansible/hosts里的主机名)。
  7. # ll /tmp/facts/
  8. -rw-r--r--  1 root root 8844 6月   9 17:15 192.168.3.60
2、ping(测试主机连通性)
    

点击(此处)折叠或打开

  1. 测试远程主机的运行状态
  2. #ansible test -m ping
  3. SSH password:
  4. 192.168.3.253 | SUCCESS => {
  5.     "changed": false,
  6.     "ping": "pong"
  7. }
  8. *与all一样
  9. # ansible "*" -m ping
3、file(设置文件的属性)
    包含以下模块:
    force:需要在两种情况下强制创建软连接,一种是在源文件不存在但之后会建立的情况下;另一种是目标软连接已存在,需要先取消之前的软连接,再创建新的软连接,有两个选项:yes|no
    group:定义文件/目录属组
    mode:定义文件/目录权限
    owner:定义文件/目录属主
    path:必选项,定义文件/目录路径
    recurse:递归设置文件的属性,只对目录有效
    src:要被链接的源文件的路径,只应用于state=link情况
    dest:被链接到的路径,只应用于state=link情况
    state:file即使文件不存在,也不会被创建;
             directory:如果目录不存在,创建目录;
             link创建软连接;directory如果目录不存在,创建目录;
             hard创建硬链接;
             touch如果文件不存在,创建一个新文件,如果文件或目录存在,则更新其最后修改时间;
             absent删除目录、文件或者取消链接文件。
    

点击(此处)折叠或打开

  1. /root/test.sh文件软连接到test机器上的/tmp/test1.sh
  2. #ansible test -m file -a "force=yes src=/root/test.sh dest=/tmp/test1.sh state=link"
  3. SSH password: 
    192.168.3.253 | SUCCESS => {
        "changed": true, 
        "dest": "/tmp/test1.sh", 
        "src": "/root/test.sh", 
        "state": "absent"
    }

  4. 取消软连接文件/tmp/test2
  5. #ansible test -m file -a "path=/tmp/test2 state=absent"
  6. SSH password: 
    192.168.3.253 | SUCCESS => {
        "changed": true, 
        "path": "/tmp/test2", 
        "state": "absent"
    }

  7. 在被控机test上创建TEST文件
  8. #ansible test -m file -a "path=/tmp/TEST state=touch"
  9. SSH password: 
    192.168.3.253 | SUCCESS => {
        "changed": true, 
        "dest": "/tmp/TEST", 
        "gid": 500, 
        "group": "kylinyunwei", 
        "mode": "0664", 
        "owner": "kylinyunwei", 
        "size": 0, 
        "state": "file", 
        "uid": 500
    }

  10. 在被控机test上创建TEST1目录并添加属主、属组、权限。
  11. #ansible test -m file -a "path=/tmp/TEST1 state=directory owner=root group=root mode=777"
  12. SSH password: 
    192.168.3.253 | SUCCESS => {
        "changed": true, 
        "gid": 500, 
        "group": "kylinyunwei", 
        "mode": "0775", 
        "owner": "kylinyunwei", 
        "path": "/tmp/TEST1", 
        "size": 4096, 
        "state": "directory", 
        "uid": 500
    }

4、copy(复制文件到远程主机)
    包含模块如下:
    backup:在覆盖之前将原文件备份,备份文件包含时间信息。有两个选项:yes|no;
    content:用于替代“src”,可以直接指定文件的值;  
     dest:必选项,要将源文件复制到的远程主机的绝对路径,如果源文件是一个目录,那么该路径也必须是个目录;
    directory_mode:递归的设定目录的权限,默认为系统默认权限;
    force:如果目标主机包含该文件,但内容不同,如果设置为yes,则强制覆盖,如果为no,则只有当目标主机位置不存在该文件时才复制。默认为yes;
    others:所有的file模块里的选项都可以在这里使用;
    src:要复制到远程主机的文件在本地的地址,可以是绝对路径,也可以是相对路径。如果路径是一个目录,它将递归复制。在这种情况下,如果路径使用“/”来结尾,则只复制目录里的内容,如果没有使用“/”来结尾,则包含目录在内的整个内容全部复制,类似于rsync;
     validate: 复制前是否需要验证目的地
      

点击(此处)折叠或打开

  1. copy文件到远程主机test
  2. #ansible test -m copy -a "src=/tmp/epel-release-6-8.noarch.rpm dest=/tmp/epel-release-6-8.noarch.rpm owner=ansible group=ansbible mode=0644"
  3. SSH password: 
    192.168.3.253 | SUCCESS => {
        "changed": true, 
        "checksum": "2b2767a5ae0de30b9c7b840f2e34f5dd9deaf19a", 
        "dest": "/tmp/epel-release-6-8.noarch.rpm", 
        "gid": 500, 
        "group": "ansbible", 
        "md5sum": "2cd0ae668a585a14e07c2ea4f264d79b", 
        "mode": "0644", 
        "owner": "ansbible", 
        "size": 14540, 
        "src": "/home/ansbible/.ansible/tmp/ansible-tmp-1462874038.53-70872648382321/source", 
        "state": "file", 
        "uid": 500
    }
  4. #ansible test -m copy -a "src=/root/test.sh dest=/tmp/test.sh owner=kylinyunwei group=kylinyunwei mode=0644 backup=yes"
  5. SSH password: 
    192.168.3.253 | SUCCESS => {
        "changed": true, 
        "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", 
        "dest": "/tmp/test.sh", 
        "gid": 500, 
        "group": "kylinyunwei", 
        "md5sum": "d41d8cd98f00b204e9800998ecf8427e", 
        "mode": "0664", 
        "owner": "kylinyunwei", 
        "size": 0, 
        "src": "/home/kylinyunwei/.ansible/tmp/ansible-tmp-1462947628.32-110767092116356/source", 
        "state": "file", 
        "uid": 500
    }

  6. #ansible test -m copy -a "src=/tmp/test dest=/tmp/test validate='visudo -cf %s'"
  7. SSH password: 
    192.168.3.253 | SUCCESS => {
        "changed": false, 
        "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", 
        "dest": "/tmp/test", 
        "gid": 500, 
        "group": "kylinyunwei", 
        "mode": "0664", 
        "owner": "kylinyunwei", 
        "path": "/tmp/test", 
        "size": 0, 
        "state": "file", 
        "uid": 500
    }


5、command(在远程主机上执行命令)
    模块选项如下:
     creates:一个文件名,当该文件存在,则该命令不执行;
     free_form:要执行的linux指令;
     chdir:在执行指令之前,先切换到该指定的目录;
     removes:一个文件名,当该文件不存在,则该选项不执行;
     executable:切换shell来执行指令,该执行路径必须是一个绝对路径

 

点击(此处)折叠或打开

  1. 查看被控机test
  2. #ansible test -a "ls /tmp"

  3. 查看被控机test的test.sh文件内容
  4. # ansible test -a "more /tmp/test.sh"
  5. SSH password: 
    192.168.3.253 | SUCCESS | rc=0 >>
    "hello world"
    "err.txt"
  6. 将被控机test上的压缩包解压到/tmp目录下
  7. #ansible test -a 'tar -zxvf /opt/sh.tar.gz -C /tmp/'
  8. SSH password: 
    192.168.3.253 | SUCCESS | rc=0 >>
    eo.sh
    test1.sh
    test.sh

  9. 对192.168.3.253服务器以root执行sleep 10,设置最大连接超时时长为2s,且设置为后台运行模式,执行过程每2s输出一次进度,如5s还未执行完则中止该任务。
  10. #ansible 192.168.3.253 -B 5 -P 2 -T 2 -m command -a 'sleep 10' -u root

  11. 开启防火墙3306访问权限
  12. #ansible db -m command -a "iptables -A INPUT -s 192.168.3.0/24 -p tcp -m tcp --dport 3306 -j ACCEPT"
  13. #ansible web1 -m command -a 'chdir=/tmp sh prestart.sh'
  14. #ansible web1 -a 'creates=/tmp/test1 uptime'
  15. #ansible web1 -a 'removes=/tmp/test1 uptime'

6、shell(切换到某个shell执行指令,参数与command相同)
    

点击(此处)折叠或打开

  1. 执行被控机test上的脚本hello.sh并追加到文件hello.txt(hello.txt文件在ansible指定的远程用户的家目录下面)
  2. #ansible test -m shell -a "/tmp/hello.sh >> hello.txt"
  3. SSH password: 
    192.168.3.253 | SUCCESS | rc=0 >>
  4. 批量查看远程主机内存使用情况
  5. #ansible test -m shell -a "free -m"
  6. #ansible web1 -m shell -a 'chdir=/tmp sh prestart.sh'
  7. #ansible lvse -u kylinyunwei -m shell -a "chdir=/Game/Script/Update sh check_version.sh"
  8. #如果脚本里需要启动程序,必须在启动程序前加上nohup命令执行,否则ansible执行结束后程序也会挂掉。

7、service(用于服务管理)
    包含如下选项:
     arguments:给命令提供一些选项;
     enabled:是否开机启动 yes|no;
     name:必选项,服务名称;
     pattern:定义一个模式,如果通过status指令来查看服务的状态时,没有响应,就会通过ps指令在进程中根据该模式进行查找,如果匹配到,则认为该服务依然在运行;
     runlevel:运行级别;
     sleep:如果执行了restarted,则在stop和start之间沉睡几秒钟;
     state:对当前服务执行started、stopped、restarted、reloaded等操作;
     

点击(此处)折叠或打开

  1. 关闭被控机test的httpd服务
  2. #ansible test -m service -a "name=httpd state=stopped"
  3. SSH password:
  4. 192.168.3.253 | SUCCESS => {
  5.     "changed": true,
  6.     "name": "httpd",
  7.     "state": "stopped"
  8. }
  9. # ansible test -m service -a "name=httpd state=restarted"
  10. SSH password: 
    192.168.3.253 | SUCCESS => {
        "changed": true, 
        "name": "httpd", 
        "state": "started"
    }
    使用root远程关闭nginx服务
  11. # ansible test1 -m service -a "name=nginx state=stopped" -u root
  12. SSH password: 
    192.168.3.248 | SUCCESS => {
        "changed": true, 
        "name": "nginx", 
        "state": "stopped"
    }

  13. 使用root远程开启nginx服务
  14. # ansible test1 -m service -a "name=nginx state=started" -u root
  15. SSH password: 
    192.168.3.248 | SUCCESS => {
        "changed": true, 
        "name": "nginx", 
        "state": "started"
    }

  16. 启动ntp服务,并设置为开机启动
  17. #ansible test1 -m service -a “name=ntpd state=started enabled=yes

8、cron(管理计划任务)
    选项如下:
     backup:对远程主机上的原计划任务内容修改之前做备份;
     cron_file:如果指定该选项,则用该文件替换远程主机上的cron.d目录下的用户的计划任务;
     minute、hour、day、month、weekday;
     job:要执行的任务,依赖于state=present;
     name:该任务的描述;
     special_time:指定什么时候执行,参数:reboot、yearly、annually、monthly、weekly、daily、hourly;
      state:确认该计划任务是创建还是删除;
      user:以哪个用户的身份执行;
      

点击(此处)折叠或打开

  1. #在被控机test上添加一条计划任务说明“#Ansible: echo hello”;每一分钟执行一次echo hello追加到txt文件“1 * * * * echo hello >> txt”
  2. #ansible test -m cron -a 'name="echo hello" minute="1" job="echo hello >> txt"'
  3. SSH password: 
    192.168.3.253 | SUCCESS => {
        "changed": true, 
        "jobs": [
            "echo hello"
        ]
    }
    在被控机test上添加一条计划任务 每天0点30分执行eo.sh脚本
  4. # ansible test -m cron -a 'name="sh /tmp/eo.sh" minute="30" hour="0" job="/tmp/eo.sh"'
  5. SSH password: 
    192.168.3.253 | SUCCESS => {
        "changed": true, 
        "jobs": [
            "echo hello", 
            "sh /tmp/eo.sh"
        ]
    }

  6. # ansible web2 -m cron  -a 'backup="True" name="test" minute="0" hour="2" job="ls -alh > /dev/null"'
  7. #crontab -l    #web2上查看计划任务
  8. #Ansible: test
  9. 0 2 * * * ls -alh > /dev/null
9、filesystem(在块设备上创建文件系统)
    选项如下:
    dev:目标块设备;
    force:在一个已有文件系统的设备上强制创建;
    fstype:文件系统的类型;
    opts:传递给mkfs命令的选项;
    
10、yum(使用yum包管理器管理软件包)
      选项如下:
       config_file:yum的配置文件;
       disable_gpg_check:关闭gpg_check;
       disablerepo:不启用某个源;
       enablerepo:启用某个源;
       list
           name:要进行操作的软件包名字,也可以传递一个url或者一个本地rpm包的路径;
           state:状态(present,absent,latest)

点击(此处)折叠或打开

  1. 远程安装nginx
  2. #ansible test -a 'sudo yum install nginx -y'
  3. #ansible test -'yum install nginx -y' --sudo

  4. # ansible test1 -m yum -a "name=httpd state=present" -u root
  5. SSH password: 
    192.168.3.248 | SUCCESS => {
        "changed": true, 
        "msg": " [Errno 14] PYCURL ERROR 6 - \"Couldn't resolve host 'nginx.org'\"\nTrying other mirror.\nwarning: rpmts_HdrFromFdno: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY\nImporting GPG key 0xC105B9DE:\n Userid : CentOS-6 Key (CentOS 6 Official Signing Key) \n Package: centos-release-6-5.el6.centos.11.1.x86_64 (@anaconda-CentOS-201311272149.x86_64/6.5)\n From   : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6\n", 
        "rc": 0, 
        "results": [
            "Loaded plugins: fastestmirror, security\nLoading mirror speeds from cached hostfile\n * base: mirrors.163.com\n * extras: mirror.bit.edu.cn\n * updates: mirror.bit.edu.cn\nSetting up Install Process\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.2.15-47.el6.centos.4 will be installed\n--> Processing Dependency: httpd-tools = 2.2.15-47.el6.centos.4 for package: httpd-2.2.15-47.el6.centos.4.x86_64\n--> Processing Dependency: apr-util-ldap for package: httpd-2.2.15-47.el6.centos.4.x86_64\n--> Running transaction check\n---> Package apr-util-ldap.x86_64 0:1.3.9-3.el6_0.1 will be installed\n---> Package httpd-tools.x86_64 0:2.2.15-47.el6.centos.4 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package            Arch        Version                      Repository    Size\n================================================================================\nInstalling:\n httpd              x86_64      2.2.15-47.el6.centos.4       updates      831 k\nInstalling for dependencies:\n apr-util-ldap      x86_64      1.3.9-3.el6_0.1              base          15 k\n httpd-tools        x86_64      2.2.15-47.el6.centos.4       updates       77 k\n\nTransaction Summary\n================================================================================\nInstall       3 Package(s)\n\nTotal download size: 924 k\nInstalled size: 3.1 M\nDownloading Packages:\n--------------------------------------------------------------------------------\nTotal                                           171 kB/s | 924 kB     00:05     \nRetrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6\nRunning rpm_check_debug\nRunning Transaction Test\nTransaction Test Succeeded\nRunning Transaction\n\r  Installing : apr-util-ldap-1.3.9-3.el6_0.1.x86_64                         1/3 \n\r  Installing : httpd-tools-2.2.15-47.el6.centos.4.x86_64                    2/3 \n\r  Installing : httpd-2.2.15-47.el6.centos.4.x86_64                          3/3 \n\r  Verifying  : httpd-2.2.15-47.el6.centos.4.x86_64                          1/3 \n\r  Verifying  : httpd-tools-2.2.15-47.el6.centos.4.x86_64                    2/3 \n\r  Verifying  : apr-util-ldap-1.3.9-3.el6_0.1.x86_64                         3/3 \n\nInstalled:\n  httpd.x86_64 0:2.2.15-47.el6.centos.4                                         \n\nDependency Installed:\n  apr-util-ldap.x86_64 0:1.3.9-3.el6_0.1                                        \n  httpd-tools.x86_64 0:2.2.15-47.el6.centos.4                                   \n\nComplete!\n"
        ]
    }

  6. yum指定某版本安装,其name参数指定具体版本地址。yum支持网络安装或本地安装。
  7. 网络安装:
  8. #ansible test -m yum -a "name=nginx-release-centos-6-0.el6.ngx.noarch.rpm state=present"
  9. 本地安装:
  10. #ansible test -m yum -a "name=/usr/local/src/nginx-release-centos-6-0.el6.ngx.noarch.rpm state=present"



        
11、user(用户管理)
         home、groups、uid、name、createhome、system、remove、state、shell、append等;
          password:后面指定的密码不能是明文,这一串密码会被直接传送到被管理主机的/etc/shadow文件中,而登陆时候输入的密码会被hash加密以后再去与/etc/shadow中存放的密码做对比,会不一致。需要先将密码字符串进行加密处理:openssl passwd -salt -1 “111111”,得到的字符串放到password中即可。
        

点击(此处)折叠或打开

  1. '111111'代表密码的长度
  2. # openssl passwd -1 -salt '111111'
  3. Password:                         #输入密码
  4. $1$5423839$FVAA9INAgPYTX3CIJquxm.
  5. grub-md5-crypt
  6. Password:                         #输入密码
    Retype password:                  #再输一遍密码
    $1$bSySn$I.PWao16tBxyGWH8q5g0q/

  7. 使用root权限修改被控机test1的密码
  8. # ansible test1 -m user -a "name=nagios password='$1$iE8Tn$MQ/HbvalM91FgB5Hqw0mf.'" -u root
  9. SSH password: 
    192.168.3.248 | SUCCESS => {
        "append": false, 
        "changed": true, 
        "comment": "", 
        "group": 501, 
        "home": "/home/nagios", 
        "move_home": false, 
        "name": "nagios", 
        "password": "NOT_LOGGING_PASSWORD", 
        "shell": "/sbin/nologin", 
        "state": "present", 
        "uid": 501
    }

  10. # ansible test -m user -a "name=mysql password='$1$iE8Tn$MQ/HbvalM91FgB5Hqw0mf.'" --sudo
  11. SSH password: 
    192.168.3.253 | SUCCESS => {
        "append": false, 
        "changed": false, 
        "comment": "", 
        "group": 504, 
        "home": "/home/mysql", 
        "move_home": false, 
        "name": "mysql", 
        "password": "NOT_LOGGING_PASSWORD", 
        "shell": "/bin/bash", 
        "state": "present", 
        "uid": 504
    }

  12. 新增用户dba,使用bash shell,附加组为admins,dbagroup,家目录为/home/dba。
  13. #ansible db -m user -a "name=dba shell=/bin/bash groups=admins,dbagroup append=yes home=/home/dba/ state=present"
  14. 修改用户属组
  15. #ansible db -m user -a "name=dba groups=dbagroup append=no"
  16. 删除用户test
  17. #ansible web2 -m user -a "name=test state=absent remove=yes"




12、group(组管理)
      

点击(此处)折叠或打开

  1. 远程添加组mysql
  2. # ansible test1 -m group -a "gid=306 system=yes name=mysql" -u root
  3. SSH password: 
    192.168.3.248 | SUCCESS => {
        "changed": true, 
        "gid": 306, 
        "name": "mysql", 
        "state": "present", 
        "system": true
    }
    远程机上查看新添加的组mysql
  4. # cat /etc/group |grep mysql
  5. mysql:x:306:



13、synchronize(rsync同步文件)
      参数    
      archive 参数默认启用,该参数默认开启了recursive、links、perms、times、owner、group、-D参数;
    checksum 校验;
   cpmpress 是否开启压缩;
   copy_links:复制链接文件,默认为no,
    delete=yes 使两边的内容一样;
    dest 目的地址;
    src 源地址;
    dest_port 目的端口;
   dirs 传输目录不进行递归,默认为no,即进行目录递归;
    existing_only 不接收创建新文件;
    mode(push推送,pull拉取) 更改推送模式、rsync_path、;
      

点击(此处)折叠或打开

  1. 将主控机/root/sysinit文件同步到到被控机/tmp下
  2. # ansible test1 -m synchronize -a 'src=/root/sysinit dest=/tmp/ compress=yes' -u root
  3. SSH password: 
    Enter passphrase for key '/root/.ssh/authorized_keys': 
    Enter passphrase for key '/root/.ssh/authorized_keys': 
    Enter passphrase for key '/root/.ssh/authorized_keys': 
    root@192.168.3.248's password: 
    root@192.168.3.248's password: 
    192.168.3.248 | SUCCESS => {
        "changed": true, 
        "cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh '/usr/bin/ssh -i /root/.ssh/authorized_keys -S none -o StrictHostKeyChecking=no -o Port=8020' --out-format='<>%i %n%L' \"/root/sysinit\" \"root@192.168.3.248:/tmp/\"", 
        "msg": "
  4. # ansible test1 -m synchronize -a 'mode=pull src=/tmp/192.168.3.248 dest=/opt/' -u root
  5. SSH password: 
    Enter passphrase for key '/root/.ssh/authorized_keys': 
    Enter passphrase for key '/root/.ssh/authorized_keys': 
    Enter passphrase for key '/root/.ssh/authorized_keys': 
    root@192.168.3.248's password: 
    192.168.3.248 | SUCCESS => {
        "changed": true, 
        "cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh '/usr/bin/ssh -i /root/.ssh/authorized_keys -S none -o StrictHostKeyChecking=no -o Port=8020' --out-format='<>%i %n%L' \"root@192.168.3.248:/tmp/192.168.3.248\" \"/opt/\"", 
        "msg": ">f+++++++++ 192.168.3.248\n", 
        "rc": 0, 
        "stdout_lines": [
            ">f+++++++++ 192.168.3.248"
        ]
    }




14、mount(配置挂载点)
      选项:
       dump
       fstype:必选项,挂载文件的类型;
       name:必选项,挂载点;
       opts:传递给mount命令的参数;
       passno
       src:必选项,要挂载的文件;
       state:必选项;
       present:只处理fstab中的配置;
       absent:删除挂载点;
       mounted:自动创建挂载点并挂载;
       umounted:卸载;
    

点击(此处)折叠或打开

  1. #ansible web1 -m mount 'name=/mnt/dvd src=/dev/sr0 fstype=iso9660 opts=ro state=present'
  2. #ansible test -a 'dd if=/dev/zero of=/disk.img bs=4k count=1024'
  3. #ansible test -m mount 'name=/mnt src=/dev/loop0 fstype=ext4 state=mounted opts=rw'
15、raw(类似command,但可以传递管道)
      

点击(此处)折叠或打开

  1. 给被控机test目录tmp下的test文件执行权限
  2. # ansible test -m raw -a 'chmod +x /tmp/test'
  3. SSH password: 
    192.168.3.253 | SUCCESS | rc=0 >>


16、pip安装
   

点击(此处)折叠或打开

  1. 安装Django
  2. #ansible test -m pip -a "name=django state=present"
  3. #ansible test -m easy_install -a "name=django"

  4. 检查Django安装是否正常,django依赖python2.7+版本。
  5. #ansible app -m command -a "python -c 'import django; print django.get_version()'"


17、win_user(windows用户管理模块)
  

点击(此处)折叠或打开

  1. 新增用户test,密码为123456,属组为Administrators
  2. #ansible windows -m win_user -a "name=test passwd=123456 group=Administrators"


 
18、mysql_user(数据库用户管理)

点击(此处)折叠或打开

  1. 新增mysql用户db1,设置登录密码为123456,对zabbix.*表有ALL权限。
  2. #ansible db -m mysql_user -a 'login_host=localhost login_password=123456 login_user=root name=db1 password=123456 priv=zabbix.*:ALL state=present'
19、script(ansible控制端脚本执行到被控端。)
      

点击(此处)折叠或打开

  1. # cat script.sh
  2. #!/bin/bash
  3. uptime
  4. # ansible web1 -m script -a 'script.sh'
  5. # ansible web1 -m script -a 'script.sh'|egrep '>>|stdout'   #输出结果仅过滤stdout部分。
20、lineinfile(针对文件特殊行,使用后端引用的正则表达式替换)
      替换,移除文件的单行。
hosts内容:

点击(此处)折叠或打开

  1. [web2]
  2. 192.168.3.61 ansible_ssh_port=8020 ansible_ssh_user=root ansible_ssh_pass=123456
  3. [web2:vars]
  4. web2_hostname=song

playbook内容如下:

点击(此处)折叠或打开

  1. #修改主机名。
  2. lineinfile:
  3.        dest: /etc/sysconfig/network
  4.        regexp: '^HOSTNAME='
  5.        line: 'HOSTNAME={{ web2_hostname }}'
  6.     tags: hostname
21、replace(批量替换文件行,使用正则匹配相应字符串)     
   

点击(此处)折叠或打开

  1. #远程服务器web2配置/tmp/test
  2. #cat /tmp/test
  3. 1be4d39eecf48ced89e1e95452e75092   test
  4. #将远程服务器web2配置文件/tmp/test中所有e替换成E
  5. #ansible web2 -m replace -a "dest=/tmp/test regexp='e' replace='E'"

22、unarchive(解压模块)
      用于解压文件。
      copy:在解压文件之前,是否先将文件复制到远程主机,默认为yes。若为no,目标主机上压缩包必须存在。
      creates:指定一个文件名,当该文件存在时,则解压指令不执行。
      src:如果copy为yes,需要指定文件的源路径。
      dest:远程主机上的一个路径,即文件解压路径。
      group:解压后的目录或文件的属组。
      list_files:如果为yes,则会列出压缩包里的文件,默认为no。
      mode:解压后的权限。
      owener:解压后文件或目录属主。
      

点击(此处)折叠或打开

  1. ansible web2 -m unarchive -a "src=test.zip dest=/tmp copy=yes"



      

23、get_url(类似于wget)
       主要用于从http、ftp、https服务器上下载文件。
       sha256sum:下载完成后进行sha256 check;
       timeout:下载超时时间,默认10s;
       url:下载的url;
       url_password、url_username:主要用于需要用户密码进行验证的情况;
       use_proxy:使用代理,代理需要事先在环境变更中定义。


点击(此处)折叠或打开

  1. #ansible web2 -m get_url -a 'url=http://192.168.3.59/test/test.txt dest=/tmp/ mode=0440'
  2. 192.168.3.61 | SUCCESS => {
        "changed": true, 
        "checksum_dest": null, 
        "checksum_src": "51ed4c692bd91b2f1bac9313cbf50e284527fc8b", 
        "dest": "/tmp/test.txt", 
        "gid": 0, 
        "group": "root", 
        "md5sum": "60cbd18458b8fb96e5af3f3811bb9238", 
        "mode": "0440", 
        "msg": "OK (51 bytes)", 
        "owner": "root", 
        "size": 51, 
        "src": "/tmp/tmp12DYAo", 
        "state": "file", 
        "status_code": 200, 
        "uid": 0, 
        "url": ""
    }


阅读(2484) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~