Ansible的基本使用

ansible初识

运维工作

系统安装:

   bare metal(实体机 ):pxe技术,cobbler技术

   virtural machine(虚拟机):VMware,批量部署操作

应用程序部署&configuration工具:

    puppet:ruby

    saltstack:python

    chef

    cfengine

command & control工具:

    fabric:python

    func

程序发布:

    人工智能(手动发布)

    shell脚本

    发布程序(运维程序)

发布要求:

    1.不影响用户体验

    2.系统不能停机

    3.不能够导致系统故障或者造成系统完全不可用

程序的发布要基于灰度模型:

       基于主机

       基于用户

灰度发布机制:

    发布路径:/webapps/tuangou----->/webapps/tuangou1.0------>/webapps/tuangou1.1......

    在调度器上下线一批机器(标记为维护模式)--->关闭服务---->部署新版本---->启动服务----->在调度器上启用这一批机器

监控系统:

    zabbix

运维工具的分类:

    agent:需要安装代理程序;例如

    无agent:  启动ssh连接管理向服务端发送请求,危险性较高,例如ansible,fabric

ansible:

    足够轻量化,

    能够提供configuration&command control基本功能

可以实现的功能:

    实现配置功能

    部署功能

    版本控制

    任务编排

特性:

    模块化:高度模块化,完成特定的任务

    基于Python语言实现,由paramiko,pyYAML和jinjia2三个关键组成

    部署简单:agentless

    支持自定义模块

    支持playbook

    幂等性:重复执行和一次执行的效果一致

组成部分:

ansible实战:

安装:

 
  1. [root@bogon ~]# rpm -ivh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-9.noarch.rpm #安装epel源
  2. Retrieving http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-9.noarch.rpm
  3. warning: /var/tmp/rpm-tmp.6BsPNz: Header V3 RSA/SHA256 Signature, key ID 352c64e5: NOKEY
  4. Preparing... ################################# [100%]
  5. Updating / installing...
  6. 1:epel-release-7-9 ################################# [100%]
  7. [root@bogon ~]# yum install ansible -y #yum 安装ansible

有用的配置文件:

 
  1. [root@bogon ~]# rpm -ql ansible |head
  2. /etc/ansible
  3. /etc/ansible/ansible.cfg #主配置文件
  4. /etc/ansible/hosts #host 主机列表
  5. /etc/ansible/roles #角色列表
  6. /usr/bin/ansible
  7. /usr/bin/ansible-console
  8. /usr/bin/ansible-doc #查看文档
  9. /usr/bin/ansible-galaxy
  10. /usr/bin/ansible-playbook
  11. /usr/bin/ansible-pull

配置管理主机的host文件:

 
  1. [root@bogon ansible]# vim /etc/ansible/hosts
  2. # Ex 1: Ungrouped hosts, specify before any group headers. #单个主机方式管理
  3. ## blue.example.com
  4. ## 192.168.100.1
  5. ## 192.168.100.10
  6. # Ex 2: A collection of hosts belonging to the 'webservers' group #分组方式管理
  7. ## [webservers]
  8. ## alpha.example.org
  9. ## beta.example.org
  10. ## 192.168.1.100
  11. ## 192.168.1.110
  12. # Ex 3: A collection of database servers in the 'dbservers' group #正则方式定义 ## db01.intranet.mydomain.net ## db02.intranet.mydomain.net ## 10.25.1.56 ## 10.25.1.57

 

根据主控端生成ssh密钥文件:

 
  1. [root@bogon ansible]# ssh-keygen -t rsa -P '' #空密码密钥文件的生成
  2. Generating public/private rsa key pair.
  3. Enter file in which to save the key (/root/.ssh/id_rsa):
  4. Created directory '/root/.ssh'.
  5. Your identification has been saved in /root/.ssh/id_rsa.
  6. Your public key has been saved in /root/.ssh/id_rsa.pub.
  7. The key fingerprint is:
  8. d7:dc:43:43:cb:ad:7b:7a:95:78:cb:b3:2c:fb:67:56 root@bogon
  9. The key's randomart image is:
  10. +--[ RSA 2048]----+
  11. | . |
  12. | o o |
  13. | = .|
  14. | o o o |
  15. | S . o = .|
  16. | . . =E|
  17. | + =|
  18. | ..*=|
  19. | .=B+|
  20. +-----------------+

添加信任到客户端:

 
  1. [root@bogon ansible]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.1.1.11 发送公钥文件到客户端
  2. The authenticity of host '172.1.1.11 (172.1.1.11)' can't be established.
  3. ECDSA key fingerprint is 48:6c:db:8c:c7:90:ec:3e:e6:ee:13:ae:cc:cb:a8:7b.
  4. Are you sure you want to continue connecting (yes/no)? yes
  5. /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
  6. /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
  7. root@172.1.1.11's password:
  8.  
  9. Number of key(s) added: 1
  10.  
  11. Now try logging into the machine, with: "ssh 'root@172.1.1.11'"
  12. and check to make sure that only the key(s) you wanted were added.

第一个测试命令:

 
  1. [root@bogon ansible]# ansible all -m ping #对所有分组机器执行ping测试
  2. 172.1.1.11 | SUCCESS => { #返回响应 "changed": false, "ping": "pong" } 172.1.1.10 | SUCCESS => { "changed": false, "ping": "pong" }

简单实用格式:

 
  1. ansible HOST-PATTERN -m MOD_NAME -a MOD_ARGS

 

ansible进阶

ansible配置文件介绍:

 
  1. [root@bogon ~]# vim /etc/ansible/ansible.cfg 打开配置文件
  2. [root@bogon ~]# egrep -v "^#|^$" /etc/ansible/ansible.cfg
  3. [defaults]
  4. inventory = /etc/ansible/hosts
  5. remote_tmp = ~/.ansible/tmp
  6. local_tmp = ~/.ansible/tmp
  7. forks = 5
  8. sudo_user = root
  9. transport = smart
  10. module_lang = C
  11. gathering = implicit
  12. gather_timeout = 10
  13. sudo_exe = sudo
  • ansible的简单实用格式:

ansible HOST-PATTERN -m MODULE_NAME -a MODULE_ARGS

ansible的常用模块介绍

  • 查看ansible模块的命令:ansible-doc-l
 
  1. [root@bogon ~]# ansible-doc -l
  2. a10_server Manage A10 Networks AX/SoftAX/Thunder/vThunder devices
  3. a10_service_group Manage A10 Networks devices' service groups
  4. a10_virtual_server Manage A10 Networks devices' virtual servers
  5. acl Sets and retrieves file ACL information.
  6. add_host add a host (and alternatively a group) to the ansible-playbook in-memory inventory
  7. airbrake_deployment Notify airbrake about app deployments
  8. alternatives Manages alternative programs for common commands
  9. apache2_mod_proxy Set and/or get members' attributes of an Apache httpd 2.4 mod_proxy balancer pool
  10. apache2_module enables/disables a module of the Apache2 webserver
  11. apk Manages apk packages  
  12. 还有很多。。。。。。。
  • ansible命令行调用模块的语法如下:

           ansible 操作目标 -m 模块名称 -a 模块参数

  • 下面是常用的模块:
    • hostname模块:指定主机名
    • known_hosts
    • command模块:用于执行命令

实战:给所有主机添加user1用户,并给添加user1当做密码

 
  1. [root@bogon ~]# ansible webservers -m command -a "useradd user1"
  2. 172.1.1.7 | SUCCESS | rc=0 >>
  3. 172.1.1.12 | SUCCESS | rc=0 >>
  4. [root@bogon ~]# ansible webservers -m shell -a "echo user1|passwd --stdin user1"
  5. 172.1.1.7 | SUCCESS | rc=0 >>
  6. user1|passwd --stdin user1
  7. 172.1.1.12 | SUCCESS | rc=0 >>
  8. user1|passwd --stdin user1
  • setup模块:可以获取ansible客户端详细信息
 
  1. [root@bogon ~]# ansible 172.1.1.7 -m setup
  2. 172.1.1.7 | SUCCESS => {
  3. "ansible_facts": {
  4. "ansible_all_ipv4_addresses": [
  5. "172.1.1.7",
  6. "10.70.12.76"
  7. ],
  8. "ansible_all_ipv6_addresses": [
  9. "fe80::20c:29ff:fe18:9f1c",
  10. "fe80::20c:29ff:fe18:9f26"
  11. ],
  12. "ansible_architecture": "x86_64",
  13. "ansible_bios_date": "07/02/2015",
  14. "ansible_bios_version": "6.00",
  15. "ansible_cmdline": {
  16. "KEYBOARDTYPE": "pc",
  17. "KEYTABLE": "us",
  18. "LANG": "en_US.UTF-8",
  19. "SYSFONT": "latarcyrheb-sun16",
  20. "quiet": true,
  21. "rd_LVM_LV": "vg_centos6/lv_root",
  22. "rd_NO_DM": true,
  23. "rd_NO_LUKS": true,
  24. 。。。。。太多了省略一部分
  • copy模块:该模块可以实现从ansible的主控端向被控端传送文件的功能,类似于scp。记得关闭所有机器的selinux,否则会出错
 
  1. [root@bogon ~]# ansible webservers -m copy -a "src=/opt/test.py dest=/opt/ owner=root group=root mode=0755 force=yes"
  2. 172.1.1.7 | SUCCESS => { #成功标识
  3. "changed": true,
  4. "checksum": "acfd655fda04d0ace7c2fefb627455939df6ea1d",
  5. "dest": "/opt/test.py",
  6. "gid": 0,
  7. "group": "root",
  8. "md5sum": "d7627cf18914922953c203ee8dd20e14",
  9. "mode": "0755",
  10. "owner": "root",
  11. "size": 99,
  12. "src": "/root/.ansible/tmp/ansible-tmp-1487355248.58-122971534024237/source",
  13. "state": "file",
  14. "uid": 0
  15. }
  16. 172.1.1.12 | SUCCESS => {
  17. "changed": true,
  18. "checksum": "acfd655fda04d0ace7c2fefb627455939df6ea1d",
  19. "dest": "/opt/test.py",
  20. "gid": 0,
  21. "group": "root",
  22. "md5sum": "d7627cf18914922953c203ee8dd20e14",
  23. "mode": "0755",
  24. "owner": "root",
  25. "size": 99,
  26. "src": "/root/.ansible/tmp/ansible-tmp-1487355249.12-32499945891444/source",
  27. "state": "file",
  28. "uid": 0
  29. }
  30. [root@bogon ~]# ansible webservers -m copy -a "content='hello word' dest=/opt/test"
  31. 172.1.1.7 | SUCCESS => {
  32. "changed": true,
  33. "checksum": "e0738b87e67bbfc9c5b77556665064446430e81c",
  34. "dest": "/opt/test",
  35. "gid": 0,
  36. "group": "root",
  37. "md5sum": "13574ef0d58b50fab38ec841efe39df4",
  38. "mode": "0644",
  39. "owner": "root",
  40. "size": 10,
  41. "src": "/root/.ansible/tmp/ansible-tmp-1487508106.35-273692325355048/source",
  42. "state": "file",
  43. "uid": 0
  44. }
  45. 172.1.1.12 | SUCCESS => {
  46. "changed": true,
  47. "checksum": "e0738b87e67bbfc9c5b77556665064446430e81c",
  48. "dest": "/opt/test",
  49. "gid": 0,
  50. "group": "root",
  51. "md5sum": "13574ef0d58b50fab38ec841efe39df4",
  52. "mode": "0644",
  53. "owner": "root",
  54. "size": 10,
  55. "src": "/root/.ansible/tmp/ansible-tmp-1487508106.84-111192117940766/source",
  56. "state": "file",
  57. "uid": 0
  58. }

 

如果报错的话就在主控端执行:

 
  1. [root@bogon ~]# ansible webservers -m command -a "yum install libselinux-python -y"

命令分析:ansible webservers-m copy -a"src=/opt/test.py dest=/opt/ owner=root group=root mode=0755 force=yes"

-m:指定模块

-a:指定参数

src:原文件位置

dest:目标位置

owner:属主

group:属组

mode:权限

force:如果目标主机包含此文件,但内容不同则设置yes会强制覆盖,设置为no的时候只有当目标主机不包含此文件时候才会复制该文件到目标主机

backup:在覆盖之前保留源文件,备份源文件

复制整个目录里的文件就不需要再src目录之后加/

  • sychronize模块:使用sychronize模块会调用rsync命令首先要记得安装好rsync软件包
 
  1.  
  2. [root@bogon ~]# ansible webservers -m command -a "yum install rsync -y"
  3. 172.1.1.12 | SUCCESS | rc=0 >>
  4. Loaded plugins: fastestmirror
  5. Loading mirror speeds from cached hostfile
  6. * base: mirrors.yun-idc.com
  7. * epel: mirrors.tuna.tsinghua.edu.cn
  8. * extras: mirrors.tuna.tsinghua.edu.cn
  9. * updates: mirrors.tuna.tsinghua.edu.cn
  10. Package rsync-3.0.9-17.el7.x86_64 already installed and latest version
  11. Nothing to do
  12.  
  13. 172.1.1.7 | SUCCESS | rc=0 >>
  14. Loaded plugins: fastestmirror, refresh-packagekit, security
  15. Loading mirror speeds from cached hostfile
  16. * epel: mirrors.tuna.tsinghua.edu.cn
  17. Setting up Install Process
  18. Resolving Dependencies
  19. --> Running transaction check
  20. ---> Package rsync.x86_64 0:3.0.6-9.el6_4.1

sychronize模块是将ansible主控机器的指定目录push到客户端的指定目录操作如下:

 
  1. [root@bogon opt]# ansible 172.1.1.12 -m synchronize -a "src=/opt/scripts/ dest=/opt/scripts/ delete=yes compress=yes"
  2. 172.1.1.12 | SUCCESS => {
  3. "changed": true,
  4. "cmd": "/usr/bin/rsync --delay-updates -F --compress --delete-after --archive --rsh 'ssh -S none -o StrictHostKeyChecking=no' --out-format='<<CHANGED>>%i %n%L' \"/opt/scripts/\" \"172.1.1.12:/opt/scripts/\"",
  5. "msg": ".d..t...... ./\n<f+++++++++ anaconda-ks.cfg\n<f+++++++++ test.py\n<f+++++++++ test.yaml\n<f+++++++++ test2.py\n<f+++++++++ test2.yaml\n<f+++++++++ test3.py\n<f+++++++++ test3.yaml\n<f+++++++++ test4.py\n<f+++++++++ test4.yaml\n",
  6. "rc": 0,
  7. "stdout_lines": [
  8. ".d..t...... ./",
  9. "<f+++++++++ anaconda-ks.cfg",
  10. "<f+++++++++ test.py",
  11. "<f+++++++++ test.yaml",
  12. "<f+++++++++ test2.py",
  13. "<f+++++++++ test2.yaml",
  14. "<f+++++++++ test3.py",
  15. "<f+++++++++ test3.yaml",
  16. "<f+++++++++ test4.py",
  17. "<f+++++++++ test4.yaml"
  18. ]
  19. }

ansible172.1.1.12-m synchronize -a"src=/opt/scripts/ dest=/opt/scripts/ delete=yes compress=yes"  

   其中:delete实现自动对比删除文件实现对文件同步的一致性,compress是压缩传输的意思

  • file模块:主要用来设置文件或者目录的属性
    • group:定义属组
    • mode:定义权限
    • owner:定义属主
    • path:定义文件或者目录的路径
    • recurse:递归设置目录的属性
    • src:被链接的原文件位置
    • dest:被链接的目标位置
    • force:强制创建软连接,有yes和no两个选项
    • state:后面连接文件的各种状态,如下:
      • link:创建软连接
      • hard:创建硬连接
      • directory:如果目录不存在,就创建目录
      • file:即使文件不存在,也不会被创建
      • absent:删除目录、文件、或者链接文件
      • touch:如果文件不存在则会创建一个新的文件,如果文件或者目录存在就会更新最后的修改时间

实例一:    将客户机的172.1.1.12的/opt/scripts/test.py 软连接到/tmp/test.py下,查看并删除:

 
  1. [root@bogon opt]# ansible 172.1.1.12 -m file -a "src=/opt/scripts/test.py dest=/tmp/test.py state=link" #创建软连接
  2. 172.1.1.12 | SUCCESS => {
  3. "changed": true,
  4. "dest": "/tmp/test.py",
  5. "gid": 0,
  6. "group": "root",
  7. "mode": "0777",
  8. "owner": "root",
  9. "size": 20,
  10. "src": "/opt/scripts/test.py",
  11. "state": "link",
  12. "uid": 0
  13. }
  14. [root@bogon opt]# ansible 172.1.1.12 -m command -a "ls -l /tmp" #查看连接时否存在
  15. 172.1.1.12 | SUCCESS | rc=0 >>
  16. total 4
  17. drwx------ 2 root root 63 Feb 18 14:18 ansible_VlEoeC
  18. drwxr-xr-x 2 root root 6 Feb 18 14:16 cfp
  19. -rwx------. 1 root root 827 Feb 15 17:16 ks-script-c9emev
  20. lrwxrwxrwx 1 root root 20 Feb 18 14:15 test.py -> /opt/scripts/test.py
  21. -rw-------. 1 root root 0 Feb 15 17:00 yum.log
  22. [root@bogon opt]# ansible 172.1.1.12 -m file -a "src=/opt/scripts/test.py dest=/tmp/test.py state=absent" #删除软连接
  23. 172.1.1.12 | SUCCESS => {
  24. "changed": true,
  25. "path": "/tmp/test.py",
  26. "state": "absent"
  27. }

实例二:在所有的被控端的机器上创建/test.txt文件属主和数组均为root,权限为0755,然后查看和删除

 
  1. [root@bogon opt]# ansible all -m file -a "path=/test.txt group=root owner=root state=touch mode=0755" #创建test.txt文件
  2. 172.1.1.12 | SUCCESS => {
  3. "changed": true,
  4. "dest": "/test.txt",
  5. "gid": 0,
  6. "group": "root",
  7. "mode": "0755",
  8. "owner": "root",
  9. "size": 0,
  10. "state": "file",
  11. "uid": 0
  12. }
  13. 172.1.1.7 | SUCCESS => {
  14. "changed": true,
  15. "dest": "/test.txt",
  16. "gid": 0,
  17. "group": "root",
  18. "mode": "0755",
  19. "owner": "root",
  20. "size": 0,
  21. "state": "file",
  22. "uid": 0
  23. }
  24. [root@bogon opt]# ansible all -m command -a "ls -l /test.txt " #查看文件
  25. 172.1.1.12 | SUCCESS | rc=0 >>
  26. -rwxr-xr-x 1 root root 0 Feb 18 14:24 /test.txt
  27.  
  28. 172.1.1.7 | SUCCESS | rc=0 >>
  29. -rwxr-xr-x 1 root root 0 Jan 17 05:23 /test.txt
  30. [root@bogon opt]# ansible all -m file -a "path=/test.txt group=root owner=root state=absent mode=0755" #删除文件
  31. 172.1.1.7 | SUCCESS => {
  32. "changed": true,
  33. "path": "/test.txt",
  34. "state": "absent"
  35. }
  36. 172.1.1.12 | SUCCESS => {
  37. "changed": true,
  38. "path": "/test.txt",
  39. "state": "absent"
  40. }

实例三:在webservers创建/test目录,属主和属组均为root,权限为0755,然后查看并删除

 
  1. [root@bogon opt]# ansible webservers -m file -a "path=/test group=root owner=root mode=0755 state=directory" #创建目录
  2. 172.1.1.7 | SUCCESS => {
  3. "changed": true,
  4. "gid": 0,
  5. "group": "root",
  6. "mode": "0755",
  7. "owner": "root",
  8. "path": "/test",
  9. "size": 4096,
  10. "state": "directory",
  11. "uid": 0
  12. }
  13. 172.1.1.12 | SUCCESS => {
  14. "changed": true,
  15. "gid": 0,
  16. "group": "root",
  17. "mode": "0755",
  18. "owner": "root",
  19. "path": "/test",
  20. "size": 6,
  21. "state": "directory",
  22. "uid": 0
  23. }
  24. [root@bogon opt]# ansible webservers -m command -a "ls -l /test" #查看目录
  25. 172.1.1.7 | SUCCESS | rc=0 >>
  26. total 0
  27.  
  28. 172.1.1.12 | SUCCESS | rc=0 >>
  29. total 0
  30. [root@bogon opt]# ansible webservers -m file -a "path=/test state=absent" #删除目录
  31. 172.1.1.7 | SUCCESS => {
  32. "changed": true,
  33. "path": "/test",
  34. "state": "absent"
  35. }
  36. 172.1.1.12 | SUCCESS => {
  37. "changed": true,
  38. "path": "/test",
  39. "state": "absent"
  40. }
  • ping模块  :测试是否可以ping  
 
  1. [root@bogon opt]# ansible all -m ping
  2. 172.1.1.7 | SUCCESS => {
  3. "changed": false,
  4. "ping": "pong"
  5. }
  6. 172.1.1.12 | SUCCESS => {
  7. "changed": false,
  8. "ping": "pong"
  9. }
  • group模块:可以在创建节点上创建自己定义的组

实例:在webservers组建立test组定义gid=666,然后再查看和删除:

 
  1. [root@bogon opt]# ansible webservers -m group -a "gid=666 name=test" #创建组
  2. 172.1.1.7 | SUCCESS => {
  3. "changed": true,
  4. "gid": 666,
  5. "name": "test",
  6. "state": "present",
  7. "system": false
  8. }
  9. 172.1.1.12 | SUCCESS => {
  10. "changed": true,
  11. "gid": 666,
  12. "name": "test",
  13. "state": "present",
  14. "system": false
  15. }
  16. [root@bogon opt]# ansible webservers -m shell -a "cat /etc/group|grep test" #查看,此处用shell是因为command不支持管道
  17. 172.1.1.7 | SUCCESS | rc=0 >>
  18. test:x:666:
  19. 172.1.1.12 | SUCCESS | rc=0 >>
  20. test:x:666:、
  21. [root@bogon opt]# ansible all -m group -a "name=test gid=666 state=absent" #删除组
  22. 172.1.1.7 | SUCCESS => {
  23. "changed": false,
  24. "name": "test",
  25. "state": "absent"
  26. }
  27. 172.1.1.12 | SUCCESS => {
  28. "changed": false,
  29. "name": "test",
  30. "state": "absent"
  31. }
    • user模块:系统用户的创建

实例:在所有的机器上创建test用户,然后再将其加入test组,最后查看组合用户状态

 
  1. [root@bogon opt]# ansible all -m user -a "name=test group=test" #创建用户并添加到组
  2. 172.1.1.7 | SUCCESS => {
  3. "changed": true,
  4. "comment": "",
  5. "createhome": true,
  6. "group": 666,
  7. "home": "/home/test",
  8. "name": "test",
  9. "shell": "/bin/bash",
  10. "state": "present",
  11. "system": false,
  12. "uid": 503
  13. }
  14. 172.1.1.12 | SUCCESS => {
  15. "changed": true,
  16. "comment": "",
  17. "createhome": true,
  18. "group": 666,
  19. "home": "/home/test",
  20. "name": "test",
  21. "shell": "/bin/bash",
  22. "state": "present",
  23. "system": false,
  24. "uid": 1000
  25. }
  26. [root@bogon opt]# ansible all -m shell -a "cat /etc/passwd|grep test" #检查存在性
  27. 172.1.1.7 | SUCCESS | rc=0 >>
  28. test:x:503:666::/home/test:/bin/bash
  29.  
  30. 172.1.1.12 | SUCCESS | rc=0 >>
  31. test:x:1000:666::/home/test:/bin/bash
  32.  
  33. [root@bogon opt]# ansible all -m user -a "name=test state=absent remove=yes" #删除用户
  34. 172.1.1.7 | SUCCESS => {
  35. "changed": true,
  36. "force": false,
  37. "name": "test",
  38. "remove": true,
  39. "state": "absent"
  40. }
  41. 172.1.1.12 | SUCCESS => {
  42. "changed": true,
  43. "force": false,
  44. "name": "test",
  45. "remove": true,
  46. "state": "absent"
  47. }
  48. [root@bogon opt]# ansible all -m shell -a "cat /etc/passwd|grep test" #查看
  49. 172.1.1.7 | FAILED | rc=1 >>
  50.  
  51.  
  52. 172.1.1.12 | FAILED | rc=1 >>
    • shell模块:可以运行被控制端所有的shell命令,并能通过管道执行命令

实例:执行被控端机器下的shell脚本

 
  1. [root@bogon opt]# ansible all -m shell -a "/tmp/a.sh" #确定好a.sh可执行,否则就ansible all -m shell -a "chmod +x /tmp/a.sh" 加权限
  2. 172.1.1.7 | SUCCESS | rc=0 >>
  3. hello world
  4.  
  5. 172.1.1.12 | SUCCESS | rc=0 >>
  6. hello world
    • script模块:用于执行ansible主控机器上的脚本到被控端机器

实例:执行ansbile的/root/a.sh到被控端机器上:

 
  1. [root@bogon opt]# vim /root/a.sh
  2. [root@bogon opt]# chmod +x /root/a.sh
  3. [root@bogon opt]# ansible all -m script -a "/root/a.sh"
  4. 172.1.1.7 | SUCCESS => {
  5. "changed": true,
  6. "rc": 0,
  7. "stderr": "Shared connection to 172.1.1.7 closed.\r\n",
  8. "stdout": "hello world\r\n",
  9. "stdout_lines": [
  10. "hello world"
  11. ]
  12. }
  13. 172.1.1.12 | SUCCESS => {
  14. "changed": true,
  15. "rc": 0,
  16. "stderr": "Shared connection to 172.1.1.12 closed.\r\n",
  17. "stdout": "hello world\r\n",
  18. "stdout_lines": [
  19. "hello world"
  20. ]
  21. }
  • get_url模块:实现在远程主机上下载url到本地

实例:下载http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-9.noarch.rpm到被控端服务器的tmp下:

 
  1. [root@bogon opt]# ansible all -m get_url -a "url=http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-9.noarch.rpm dest=/tmp/" #执行下载文件
  2. 172.1.1.12 | SUCCESS => {
  3. "changed": true,
  4. "checksum_dest": null,
  5. "checksum_src": "7a524ad0ed6a57367980ea57a82298c04384c2d5",
  6. "dest": "/tmp/epel-release-7-9.noarch.rpm",
  7. "gid": 0,
  8. "group": "root",
  9. "md5sum": "a189bccb58e896c1501572e18e98d2eb",
  10. "mode": "0644",
  11. "msg": "OK (14704 bytes)",
  12. "owner": "root",
  13. "size": 14704,
  14. "src": "/tmp/tmpn3Vj46",
  15. "state": "file",
  16. "uid": 0,
  17. "url": "http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-9.noarch.rpm"
  18. }
  19. 172.1.1.7 | SUCCESS => {
  20. "changed": true,
  21. "checksum_dest": null,
  22. "checksum_src": "7a524ad0ed6a57367980ea57a82298c04384c2d5",
  23. "dest": "/tmp/epel-release-7-9.noarch.rpm",
  24. "gid": 0,
  25. "group": "root",
  26. "md5sum": "a189bccb58e896c1501572e18e98d2eb",
  27. "mode": "0644",
  28. "msg": "OK (14704 bytes)",
  29. "owner": "root",
  30. "size": 14704,
  31. "src": "/tmp/tmpSi85Qv",
  32. "state": "file",
  33. "uid": 0,
  34. "url": "http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-9.noarch.rpm"
  35. }
  36. [root@bogon opt]# ansible all -m command -a "ls -l /tmp" #查看下载文件
  • yum模块:顾名思义就是用来管理yum仓库的:
    • config_file:yum的配置文件
    • disable_gpg_check:关闭gpg_check
    • disablerepo:不启用某个yum源
    • enablerepo:启用某个yum源
    • name:要执行软件包的名字,可以传递一个本地url或者rpm包的路径
    • state:执行状态如下
      • absent|present|latest

实例:使用nginx的yum源安装nginx软件包,并进行检查

  1. 首先将nginx.repo文件写到被控制服务器的/etc/yum.repos.d/下:
  2.  
    1. [root@bogon tmp]# vim /etc/yum.repos.d/nginx.repo
    2. [nginx]
    3. name=nginx repo
    4. baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
    5. gpgcheck=0
    6. enabled=1

 

3.然后再ansible服务器端执行:

 
  1. [root@bogon opt]# ansible 172.1.1.12 -m yum -a "name=nginx enablerepo=nginx state=present" #安装
  2. 172.1.1.12 | SUCCESS => {
  3. "changed": true,
  4. "msg": "",
  5. "rc": 0,
  6. "results": [
  7. "Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\n * base: mirrors.yun-idc.com\n * epel: mirrors.tuna.tsinghua.edu.cn\n * extras: mirrors.tuna.tsinghua.edu.cn\n * updates: mirrors.tuna.tsinghua.edu.cn\nResolving Dependencies\n--> Running transaction check\n---> Package nginx.x86_64 1:1.10.3-1.el7.ngx will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n nginx x86_64 1:1.10.3-1.el7.ngx nginx 673 k\n\nTransaction Summary\n================================================================================\nInstall 1 Package\n\nTotal download size: 673 k\nInstalled size: 2.3 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Installing : 1:nginx-1.10.3-1.el7.ngx.x86_64 1/1 \n----------------------------------------------------------------------\n\nThanks for using nginx!\n\nPlease find the official documentation for nginx here:\n* http://nginx.org/en/docs/\n\nCommercial subscriptions for nginx are available on:\n* http://nginx.com/products/\n\n----------------------------------------------------------------------\n Verifying : 1:nginx-1.10.3-1.el7.ngx.x86_64 1/1 \n\nInstalled:\n nginx.x86_64 1:1.10.3-1.el7.ngx \n\nComplete!\n"
  8. ]
  9. }
  10. [root@bogon opt]# ansible 172.1.1.12 -m shell -a "yum list installed|grep nginx" #查看安装
  11. 172.1.1.12 | SUCCESS | rc=0 >>
  12. nginx.x86_64 1:1.10.3-1.el7.ngx @nginx
  13. [root@bogon opt]# ansible 172.1.1.12 -m shell -a "yum remove nginx -y" #卸载nginx
  14. 172.1.1.12 | SUCCESS | rc=0 >>
  15. Loaded plugins: fastestmirror
  16. Resolving Dependencies
  17. --> Running transaction check
  18. ---> Package nginx.x86_64 1:1.10.3-1.el7.ngx will be erased
  19. --> Finished Dependency Resolution
  20.  
  21. Dependencies Resolved
  22.  
  23. ================================================================================
  24. Package Arch Version Repository Size
  25. ================================================================================
  26. Removing:
  27. nginx x86_64 1:1.10.3-1.el7.ngx @nginx 2.3 M
  28.  
  29. Transaction Summary
  30. ================================================================================
  31. Remove 1 Package
  32.  
  33. Installed size: 2.3 M
  34. Downloading packages:
  35. Running transaction check
  36. Running transaction test
  37. Transaction test succeeded
  38. Running transaction
  39. Erasing : 1:nginx-1.10.3-1.el7.ngx.x86_64 1/1
  40. Verifying : 1:nginx-1.10.3-1.el7.ngx.x86_64 1/1
  41.  
  42. Removed:
  43. nginx.x86_64 1:1.10.3-1.el7.ngx
  44.  
  45. Complete!
  • cron模块:定时任务模块

实战:定义webservers服务器组在每晚凌晨一点更新时间:

 

 
  1. [root@bogon opt]# ansible all -m cron -a '"name=synctime" minute="*/5" job="/sbin/ntpdate time.windows.com >> /dev/null"'
  2. 172.1.1.7 | FAILED! => {
  3. "changed": false,
  4. "failed": true,
  5. "msg": "unsupported parameter for module: \"name"
  6. }
  7. 172.1.1.12 | FAILED! => {
  8. "changed": false,
  9. "failed": true,
  10. "msg": "unsupported parameter for module: \"name"
  11. }
  12. [root@bogon ~]# ansible all -m cron -a "state=absent name=synctime" #删除计划任务
  13.  

详细语法参考:ansible-doc cron

 

  • service模块:服务启动关闭模块

实战:开启172.1.1.12的nginx服务

 
  1. [root@bogon ~]# ansible 172.1.1.12 -m service -a "name=nginx state=started enable"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值