本文转自(http://blog.51cto.com/changfei/1657221)
ansible 官网-中文(https://ansible-tran.readthedocs.io/en/latest/docs/faq.html)
ansible安装

  1. [root@ju ~]# yum install ansible -y

定义主机与组

  1. [root@ju ~]# vim /etc/ansible/hosts #添加主机,并在所配置的主机上与ansible建立互信
  2. 192.168.116.138
  3. 192.168.116.139
  4. 192.168.116.139:7022 #定义一个ssh端口为7022的主机
  5. juserver ansible_ssh_port=22 ansible_ssh_host=192.168.116.25 #利用别名定义一个主机,使用的时候直接使用juserver这个别名即可
  6. [webserver] #建立分组
  7. 192.168.116.2
  8. 192.168.116.3
  9. 192.168.116.4
  10. www[01:50].example.com #支持通配符匹配www01 www02 ...www50
  11. [dbserver]
  12. 192.168.116.5
  13. 192.168.116.6
  14. db-[a:f].example.com #支持字母匹配a b c...f
  15. [weballserver:children] #组嵌套,不过这个只能用在playbook中,ansible命令行中使用不了
  16. webserver
  17. dbserver
  18. [myserver]
  19. 192.168.116.7 http_port=8000 maxRequestsPerChild=808 #可以为每个主机单独指定一些变量,这些变量可以在playbooks中使用
  20. 192.168.116.8 http_port=303 maxRequestsPerChild=909
  21. [weixinserver]
  22. 192.168.116.9
  23. 192.168.116.10
  24. [weixinserver:vars] #也可以为一个组指定变量,组内每个主机都可以使用该变量
  25. ntp_server=ntp.weixinserver.example.com
  26. proxy=proxy.weixinserver.example.com

ansible保留主机变量

  1. #ansible_ssh_host:指定主机别名对应的真实IP,如:251 ansible_ssh_host=192.168.116.251,随后连接该主机无须指定完整IP,只需指定251 就行
  2. #ansible_ssh_port:指定连接到这个主机的ssh 端口,默认22
  3. #ansible_ssh_user:连接到该主机的ssh用户
  4. #ansible_ssh_pass:连接到该主机的ssh密码(连-k 选项都省了),安全考虑还是建议使用私钥或在命令行指定-k 选项输入
  5. #ansible_sudo_pass:sudo 密码
  6. #ansible_sudo_exe(v1.8+的新特性):sudo 命令路径
  7. #ansible_connection:连接类型,可以是local、ssh 或paramiko,ansible1.2 之前默认为paramiko
  8. #ansible_ssh_private_key_file:私钥文件路径
  9. #ansible_shell_type:目标系统的shell类型,默认为sh,如果设置csh/fish,那么命令需要遵循它们语法
  10. #ansible_python_interpreter:python 解释器路径,默认是/usr/bin/python,但是如要要连freeBSD系统的话,就需要该指令修改python路径
  11. #ansible_*_interpreter:这里的"*"可以是ruby或perl或其他语言的解释器,作用和ansible_python_interpreter类似

分离主机和组的变量定义

  1. #为host 和group 定义一些比较复杂的变量时(如array、hash),可以用单独文件保存host和group 变量,以YAML 格式书写变量,避免都写在hosts 文件显得混乱,如果hosts 文件路径为:
  2. /etc/ansible/hosts
  3. #则host 和group 变量目录结构:
  4. /etc/ansible/host_vars/all #host_vars 目录用于存放host 变量,all 文件对所有主机有效
  5. /etc/ansible/host_vars/foosball #文件foosball 要和hosts 里面定义的主机名一样,表示只对foosball 主机有效
  6. /etc/ansible/group_vars/all #group_vars 目录用于存放group 变量,all 文件对所有组有效
  7. /etc/ansible/group_vars/raleigh #文件raleigh 要和hosts 里面定义的组名一样,表示对raleigh 组下的所有主机有效
  8. #这里/etc/ansible/group_vars/raleigh 格式如下,YAML 格式要求:
  9. ntp_server: acme.example.org #变量名:变量值
  10. database_server: storage.example.org

主机匹配方式

  1. #表示通配inventory 中的所有主机
  2. all
  3. '*' #星号必须引起来
  4. #也可以指定具有规则特征的主机或者主机名
  5. one.example.com
  6. one.example.com:two.example.com
  7. 192.168.1.50
  8. 192.168.1.*
  9. #意思是这两个组中的所有主机
  10. webservers:dbservers
  11. #非模式匹配:表示在webservers 组不在phoenix 组的主机
  12. webservers:!phoenix
  13. #交集匹配:表示同时都在webservers 和staging 组的主机
  14. webservers:&staging

命令行简单举例

  1. #ansible <pattern_goes_here> -m <module_name> -a <arguments>
  2. [root@ju ~]# ansible 192.168.116.138 -m ping #对单台主机测试ping
  3. [root@ju ~]# ansible all -m ping #对/etc/ansible/hosts中所有主机测试ping
  4. [root@ju ~]# ansible webserver -a "/bin/echo hello" #运行命令
  5. [root@ju ~]# ansible all -a "uptime"
  6. [root@ju ~]# ansible dbserver -m copy -a "src=/tmp/ansible dest=/tmp/ansible_1" #copy文件
  7. [root@ju ~]# ansible 192.168.116.138 -m file -a "dest=/tmp/ansible_1 mode=600 owner=ju group=ju" #改变文件属性
  8. [root@ju ~]# ansible 192.168.116.138 -m service -a "name=httpd state=running" #启动服务,或者放到开机启动的同时运行
  9. [root@ju ~]# ansible all -m setup #打印主机的清单,将输出用于描述每一台主机的JSON对象,其中包括总体内存、已使用内存、CPU、网络、磁盘信息、操作系统版本以及内核版本等等。

查看模块帮助

  1. [root@db ansible]# ansible-doc ping
  2. > PING
  3. A trivial test module, this module always returns `pong' on successful contact. It does not make sense in playbooks, but it is useful from `/usr/bin/ansible'
  4. #Test 'webservers' status
  5. ansible webservers -m ping

ansible常用模块

并行性和shell命令command|script|shell

  1. #重启webservers主机组的所有机器,每次重启10 台
  2. ansible webservers -a "/sbin/reboot" -f 10
  3. #以ju 用户身份在webservers组的所有主机运行foo 命令
  4. ansible webservers -a "/usr/bin/foo" -u ju
  5. #以ju 用户身份sudo 执行命令foo(--ask-sudo-pass (-K) 如果有sudo 密码请使用此参数)
  6. ansible webservers -a "/usr/bin/foo" -u ju --sudo [--ask-sudo-pass]
  7. #也可以sudo 到其他用户执行命令非root
  8. ansible webservers -a "/usr/bin/foo" -u username -U otheruser [--ask-sudo-pass]
  9. #默认情况下,ansible 使用的module 是command,这个模块并不支持shell 变量和管道等,若想使用shell 来执行模块,请使用-m 参数指定shell 模块
  10. #使用shell 模块在远程主机执行命令或脚本
  11. ansible dbservers -m shell -a 'echo $TERM'
  12. ansible dbservers -m shell -a '/tmp/test.sh'
  13. #script命令模块,在远程主机执行主控端本地的脚本文件,相当于scp+shell
  14. ansible dbservers -m script -a '/tmp/test.sh 111 222'

传输文件copy|file

  1. #拷贝本地的/etc/hosts 文件到myserver主机组所有主机的/tmp/hosts(空目录除外),如果使用playbooks 则可以充分利用template 模块
  2. ansible myserver -m copy -a "src=/etc/hosts dest=/tmp/hosts mode=600 owner=ju group=ju"
  3. #file 模块允许更改文件的用户及权限
  4. ansible webservers -m file -a "dest=/srv/foo/a.txt mode=600"
  5. ansible webservers -m file -a "dest=/srv/foo/b.txt mode=600 owner=ju group=ju"
  6. #使用file 模块创建目录,类似mkdir -p
  7. ansible webservers -m file -a "dest=/path/to/c mode=755 owner=ju group=ju state=directory"
  8. #使用file 模块删除文件或者目录
  9. ansible webservers -m file -a "dest=/path/to/c state=absent"

获取远程文件信息stat

  1. ansible webservers -m stat -a "path=/etc/password"

下载指定定url到远程主机get_url

  1. ansible webservers -m get_url -a "url= mode=0440 force=yes"

管理软件包yum

  1. #确保acme 包已经安装,但不更新
  2. ansible webservers -m yum -a "name=acme state=present"
  3. #确保安装包到一个特定的版本
  4. ansible webservers -m yum -a "name=acme-1.5 state=present"
  5. #确保一个软件包是最新版本
  6. ansible webservers -m yum -a "name=acme state=latest"
  7. #确保一个软件包没有被安装
  8. ansible webservers -m yum -a "name=acme state=absent"
  9. #Ansible 支持很多操作系统的软件包管理,使用时-m 指定相应的软件包管理工具模块,如果没有这样的模块,可以自己定义类似的模块或者使用command 模块来安装软件包

用户和用户组user

  1. #使用user 模块对于创建新用户和更改、删除已存在用户非常方便
  2. ansible all -m user -a "name=foo password=<crypted password here>"
  3. ansible all -m user -a "name=foo state=absent"
  4. 生成加密密码方法
  5. mkpasswd --method=SHA-512
  6. pip install passlib
  7. python -c "from passlib.hash import sha512_crypt; import getpass; print sha512_crypt.encrypt(getpass.getpass())"

服务管理service

  1. #确保webservers 组所有主机的httpd 是启动的
  2. ansible webservers -m service -a "name=httpd state=started"
  3. #重启webservers 组所有主机的httpd 服务
  4. ansible webservers -m service -a "name=httpd state=restarted"
  5. #确保webservers 组所有主机的httpd 是关闭的
  6. ansible webservers -m service -a "name=httpd state=stopped192.168.116"

后台运行

  1. #长时间运行的操作可以放到后台执行,ansible 会检查任务的状态;在主机上执行的同一个任务会分配同一个job ID
  2. #后台执行命令3600s,-B 表示后台执行的时间
  3. ansible all -B 3600 -a "/usr/bin/long_running_operation --do-stuff"
  4. #检查任务的状态
  5. ansible all -m async_status -a "jid=123456789"
  6. #后台执行命令最大时间是1800s 即30 分钟,-P 每60s 检查下状态默认15s
  7. ansible all -B 1800 -P 60 -a "/usr/bin/long_running_operation --do-stuff"

搜集系统信息setup

  1. #通过命令获取所有的系统信息
  2. #搜集主机的所有系统信息
  3. ansible all -m setup
  4. #搜集系统信息并以主机名为文件名分别保存在/tmp/facts 目录
  5. ansible all -m setup --tree /tmp/facts
  6. #搜集和内存相关的信息
  7. ansible all -m setup -a 'filter=ansible_*_mb'
  8. #搜集网卡信息
  9. ansible all -m setup -a 'filter=ansible_eth[0-2]'

计划任务cron

  1. ansible all -m cron -a 'name="jutest" hour="5" job="/bin/bash /tmp/test.sh"'
  2. 效果如下:
  3. * 5 * * * /bin/bash /tmp/test.sh

挂载模块mount

  1. ansible all -m mount -a 'name=/mnt src=/dev/sda5 fstype=ext4 opts=rostate=present