ansible大全详解【帮助你从playbook小白一跃成为playbook专家】 ...

打印 上一主题 下一主题

主题 893|帖子 893|积分 2679

使用ansible运行任务

目录

两种方式:

  • ad-hoc:类似于直接在shell终端敲打命令,执行简朴的任务
  • playbook:剧本,类似于shell脚本,执行复杂的任务
【注:导航器也可以执行任务,但只可以执行playbook,不可以执行ad-hoc】
ansible-1:192.168.96.202
ansible-2:192.168.96.203
ansible-3:192.168.96.204
ad-hoc

ad-hoc执行任务格式
  1. ansible 主机/主机组 -m 模块 -a ‘模块的参数’  ansbile的参数
  2. eg: ansbile all -m shell -a 'useradd devops' -u root -k
  3.   -u 指定用户
  4.   -k 使用密码认证   
复制代码
在配置完免密后 -u 和-k 可以不加
测试管控
  1. [root@localhost .ssh]# ansible all -m ping
  2. ansible-2 | SUCCESS => {
  3.     "ansible_facts": {
  4.         "discovered_interpreter_python": "/usr/bin/python"
  5.     },
  6.     "changed": false,
  7.     "ping": "pong"
  8. }
  9. ansible-3 | SUCCESS => {
  10.     "ansible_facts": {
  11.         "discovered_interpreter_python": "/usr/bin/python"
  12.     },
  13.     "changed": false,
  14.     "ping": "pong"
  15. }
  16. ansible-1 | SUCCESS => {
  17.     "ansible_facts": {
  18.         "discovered_interpreter_python": "/usr/bin/python"
  19.     },
  20.     "changed": false,
  21.     "ping": "pong"
  22. }
复制代码
查询模块

差别安装方式的模块数量差别,ansible-core最少
第三方安装:https://galaxy.ansible.com/
查看当前系统全部的模块数量
  1. [root@localhost .ssh]# ansible-doc -l | wc -l
  2. [WARNING]: template parsing did not produce documentation.
  3. [WARNING]: win_template parsing did not produce documentation.
  4. 3681
复制代码
在这其中,可以看到包罗cloud 、huawei、vmware、win等的模块
查看模块详细参数
  1. [root@localhost .ssh]#  ansible-doc -s user
  2. - name: Manage user accounts
  3.   user:
  4.       append:                # If `yes', add the user to the groups specified in `groups'. If `no', user will only be added to the groups specified in `groups', removing them from
  5.                                all other groups. Mutually exclusive with `local'
  6.       authorization:         # Sets the authorization of the user. Does nothing when used with other platforms. Can set multiple authorizations using comma separation. To delete all
  7.                                authorizations, use `authorization='''. Currently supported on Illumos/Solaris.
  8. ...【以下省略】
复制代码
查看模块详细参数及其案例
  1. [root@localhost .ssh]# ansible-doc  user
复制代码
常用模块

命令执行模块

一共有四种模块:

  • command
  • shell
  • raw
  • script
command

通过执行ansible-doc command 可以看到,conmmand模块的使用跟正常shell命令差别不大。
  1. [root@localhost ~]# ansible all -m command -a 'touch /opt/file1'
  2. [WARNING]:  Consider using the file module  with state=touch rather than running 'touch'.
  3. If you need to use command because file is insufficient you can add 'warn: false' to this
  4. command task or set ' command_warnings=False ' in ansible.cfg to get rid of this message.
  5. 【此处warning警告是说,创建文件这个命令推荐使用file模块,而不是command模块,但这并不影响正常使用,如果想关闭这个警告,在【default】下添加 command_warnings=False 即可】
  6. ansible-2 | CHANGED | rc=0 >>
  7. ansible-1 | CHANGED | rc=0 >>
  8. ansible-3 | CHANGED | rc=0 >> command
复制代码
command模块有几样是不可用的,【重定向】 | 【管道符】   & 【and符号】
  1. [root@localhost ~]# ansible all -m command -a 'echo hellp > /opt/file4'
  2. ansible-1 | CHANGED | rc=0 >>
  3. hellp > /opt/file4
  4. ansible-2 | CHANGED | rc=0 >>
  5. hellp > /opt/file4
  6. ansible-3 | CHANGED | rc=0 >>
  7. hellp > /opt/file4
  8. [root@localhost ~]# ansible all -m command -a 'echo hello | echo hell0'
  9. ansible-2 | CHANGED | rc=0 >>
  10. hello | echo hell0
  11. ansible-3 | CHANGED | rc=0 >>
  12. hello | echo hell0
  13. ansible-1 | CHANGED | rc=0 >>
  14. hello | echo hell0
  15. [root@localhost ~]# ansible all -m command -a 'echo hello & echo hell0'
  16. ansible-3 | CHANGED | rc=0 >>
  17. hello & echo hell0
  18. ansible-1 | CHANGED | rc=0 >>
  19. hello & echo hell0
  20. ansible-2 | CHANGED | rc=0 >>
  21. hello & echo hell0
复制代码
如上所示,如果使用>&|,那么command会默认将其以为是要输出的字符。
拓展-ansbile默认模块
  1. [root@localhost ~]# ansible all -a "echo a"
  2. ansible-2 | CHANGED | rc=0 >>
  3. a
  4. ansible-3 | CHANGED | rc=0 >>
  5. a
  6. ansible-1 | CHANGED | rc=0 >>
  7. a
复制代码
这里可以看到,没有指定模块,但他仍旧有结果返回。
可以在ansible.cfg中看到
  1. # default module name for /usr/bin/ansible
  2. #module_name = command
  3. 这里设定了在不指定模块的情况下,默认使用的是什么模块
复制代码
shell

跟正常使用shell命令没区别,并且还拥有一些高级特性,chdir、creates
  1. [root@localhost ~]# ansible all -m shell -a 'echo hello & echo hell0'
  2. ansible-3 | CHANGED | rc=0 >>
  3. hell0
  4. hello
  5. ansible-2 | CHANGED | rc=0 >>
  6. hell0
  7. hello
  8. ansible-1 | CHANGED | rc=0 >>
  9. hell0
  10. hello
  11. 正常使用
复制代码
高级特性

chdir

  1. 在执行后续命令前,会更改工作目录。【因为主控执行的命令,默认是在被控的家目录下执行的,使用chdir,可以修改执行的工作目录】
  2. [root@localhost ~]# ansible all -m shell -a 'chdir=/opt touch example-1'
  3. ansible-1 | CHANGED | rc=0 >>
  4. ansible-3 | CHANGED | rc=0 >>
  5. ansible-2 | CHANGED | rc=0 >>
  6. [root@localhost ~]# ansible all -m shell -a 'chdir=/opt  ls'
  7. ansible-2 | CHANGED | rc=0 >>
  8. example-1
  9. ansible-1 | CHANGED | rc=0 >>
  10. example-1
  11. ansible-3 | CHANGED | rc=0 >>
  12. example-1
  13. chdir
复制代码

creates
  1. 当文件存在,命令不执行
  2. [root@localhost ~]# ansible all -m shell -a "creates=/etc/shadow ls /opt/"
  3. ansible-3 | SUCCESS | rc=0 >>
  4. skipped, since /etc/shadow exists
  5. ansible-2 | SUCCESS | rc=0 >>
  6. skipped, since /etc/shadow exists
  7. ansible-1 | SUCCESS | rc=0 >>
  8. skipped, since /etc/shadow exists
  9. 可以看到因为/etc/shadow存在,所以后面的步骤被跳过了
  10. [root@localhost ~]# ansible all -m shell -a "creates=/etc/shadows ls /opt/"
  11. ansible-1 | CHANGED | rc=0 >>
  12. example-1
  13. ansible-3 | CHANGED | rc=0 >>
  14. example-1
  15. ansible-2 | CHANGED | rc=0 >>
  16. example-1
  17. 如果不存在,则正常执行后面的命令
  18. creates
复制代码
removes

  1. 当文件存在,命令执行
  2. [root@localhost ~]# ansible all -m shell -a "removes=/etc/shadow ls /opt/"
  3. ansible-1 | CHANGED | rc=0 >>
  4. example-1
  5. ansible-2 | CHANGED | rc=0 >>
  6. example-1
  7. ansible-3 | CHANGED | rc=0 >>
  8. example-1
  9. 当文件不存在时,命令不执行【跳过】
  10. [root@localhost ~]# ansible all -m shell -a "removes=/etc/shadows ls /opt/"
  11. ansible-2 | SUCCESS | rc=0 >>
  12. skipped, since /etc/shadows does not exist
  13. ansible-3 | SUCCESS | rc=0 >>
  14. skipped, since /etc/shadows does not exist
  15. ansible-1 | SUCCESS | rc=0 >>
  16. skipped, since /etc/shadows does not exist
  17. removes
复制代码
raw

用法与shell一模一样,只是不支持chdir、creates、removes。但支持|&
  1. [root@localhost ~]# ansible all -m raw -a "chdir=/opt ls  "
  2. ansible-2 | CHANGED | rc=0 >>
  3. Shared connection to ansible-2 closed.
  4. ansible-3 | CHANGED | rc=0 >>
  5. ansible-example
  6. Shared connection to ansible-3 closed.
  7. ansible-1 | CHANGED | rc=0 >>
  8. Shared connection to ansible-1 closed.
  9. 可以看到他并没有在/opt下执行,而是出现在了用户的家目录下。
  10. [root@localhost ~]# ansible all -m raw -a "echo hello > /opt/file1 |cat /opt/file1  "
  11. ansible-1 | CHANGED | rc=0 >>
  12. hello
  13. Shared connection to ansible-1 closed.
  14. ansible-3 | CHANGED | rc=0 >>
  15. hello
  16. Shared connection to ansible-3 closed.
  17. ansible-2 | CHANGED | rc=0 >>
  18. hello
  19. Shared connection to ansible-2 closed.
  20. raw可以支持<>|&
复制代码
script

将管理端的shell脚本中的指令放到被控节点执行。
【但这并不是执行脚本,而是将脚本中的命令单独拎出来执行,只是形式上像是执行了shell脚本,所以脚本也就不需要可执行权限】
  1. #!/bin/bash
  2. useradd anssh
  3. rm -rf /opt/*
  4. echo hello> /opt/hello.txt
  5. 脚本
复制代码
  1. [root@localhost ~]# ansible all -m script -a "ans.sh"
  2. ansible-1 | CHANGED => {
  3.     "changed": true,
  4.     "rc": 0,
  5.     "stderr": "Shared connection to ansible-1 closed.\r\n",
  6.     "stderr_lines": [
  7.         "Shared connection to ansible-1 closed."
  8.     ],
  9.     "stdout": "\r\n",
  10.     "stdout_lines": [
  11.         ""
  12.     ]
  13. }
  14. ansible-3 | CHANGED => {
  15.     "changed": true,
  16.     "rc": 0,
  17.     "stderr": "Shared connection to ansible-3 closed.\r\n",
  18.     "stderr_lines": [
  19.         "Shared connection to ansible-3 closed."
  20.     ],
  21.     "stdout": "",
  22.     "stdout_lines": []
  23. }
  24. ansible-2 | CHANGED => {
  25.     "changed": true,
  26.     "rc": 0,
  27.     "stderr": "Shared connection to ansible-2 closed.\r\n",
  28.     "stderr_lines": [
  29.         "Shared connection to ansible-2 closed."
  30.     ],
  31.     "stdout": "",
  32.     "stdout_lines": []
  33. }
  34. script
复制代码
  1. [root@localhost ~]# ansible all -m shell -a "id anssh ; ls /opt/ ; cat /opt/hello.txt "
  2. ansible-3 | CHANGED | rc=0 >>
  3. uid=1001(anssh) gid=1001(anssh) groups=1001(anssh)
  4. hello.txt
  5. hello
  6. ansible-2 | CHANGED | rc=0 >>
  7. uid=1001(anssh) gid=1001(anssh) groups=1001(anssh)
  8. hello.txt
  9. hello
  10. ansible-1 | CHANGED | rc=0 >>
  11. uid=1001(anssh) gid=1001(anssh) groups=1001(anssh)
  12. hello.txt
  13. hello
  14. 验证
复制代码
文件相干模块

file

file模块中有两个选项尤为重要
1、path
2、state
  1. path 指定文件和目录的详细路径
  2. state 指定动作
  3.    file【默认动作】查看文件或者目录的属性信息
  4.    touch 创建文件和更新时间戳
  5.    directory 创建目录
  6.    absent 删除目录,删除文件,取消链接文件
  7.    hard 硬链接
  8.    link 软链接
  9.      force 强制创建
  10. src 创建链接文件时指定源文件路径
  11. dest 创建链接文件时指定链接文件路径
  12. mode 指定权限
  13. owner 指定拥有人
  14. group 指定拥有组 file模块的选项
复制代码
path


  1. [root@localhost ~]# ansible all -m file -a "path=/etc/passwd  "
  2. ansible-3 | SUCCESS => {
  3.     "ansible_facts": {
  4.         "discovered_interpreter_python": "/usr/bin/python"
  5.     },
  6.     "changed": false,
  7.     "gid": 0,
  8.     "group": "root",
  9.     "mode": "0644",
  10.     "owner": "root",
  11.     "path": "/etc/passwd",
  12.     "secontext": "system_u:object_r:passwd_file_t:s0",
  13.     "size": 1035,
  14.     "state": "file",
  15.     "uid": 0
  16. }
  17. ansible-1 | SUCCESS => {
  18.     "ansible_facts": {
  19.         "discovered_interpreter_python": "/usr/bin/python"
  20.     },
  21.     "changed": false,
  22.     "gid": 0,
  23.     "group": "root",
  24.     "mode": "0644",
  25.     "owner": "root",
  26.     "path": "/etc/passwd",
  27.     "secontext": "system_u:object_r:passwd_file_t:s0",
  28.     "size": 1035,
  29.     "state": "file",
  30.     "uid": 0
  31. }
  32. ansible-2 | SUCCESS => {
  33.     "ansible_facts": {
  34.         "discovered_interpreter_python": "/usr/bin/python"
  35.     },
  36.     "changed": false,
  37.     "gid": 0,
  38.     "group": "root",
  39.     "mode": "0644",
  40.     "owner": "root",
  41.     "path": "/etc/passwd",
  42.     "secontext": "system_u:object_r:passwd_file_t:s0",
  43.     "size": 1035,
  44.     "state": "file",
  45.     "uid": 0
  46. }
复制代码
可以看到默认指定的就是state=file ,查看文件的详细信息
state
  1. [root@localhost ~]# ansible all -m file -a 'path=/opt/file.txt state=touch mode=777 owner=devops group=devops '
  2. ansible-3 | CHANGED => {
  3.     "ansible_facts": {
  4.         "discovered_interpreter_python": "/usr/bin/python"
  5.     },
  6.     "changed": true,
  7.     "dest": "/opt/file.txt",
  8.     "gid": 1000,
  9.     "group": "devops",
  10.     "mode": "0777",
  11.     "owner": "devops",
  12.     "secontext": "unconfined_u:object_r:usr_t:s0",
  13.     "size": 0,
  14.     "state": "file",
  15.     "uid": 1000
  16. }
  17. ansible-1 | CHANGED => {
  18.     "ansible_facts": {
  19.         "discovered_interpreter_python": "/usr/bin/python"
  20.     },
  21.     "changed": true,
  22.     "dest": "/opt/file.txt",
  23.     "gid": 1000,
  24.     "group": "devops",
  25.     "mode": "0777",
  26.     "owner": "devops",
  27.     "secontext": "unconfined_u:object_r:usr_t:s0",
  28.     "size": 0,
  29.     "state": "file",
  30.     "uid": 1000
  31. }
  32. ansible-2 | CHANGED => {
  33.     "ansible_facts": {
  34.         "discovered_interpreter_python": "/usr/bin/python"
  35.     },
  36.     "changed": true,
  37.     "dest": "/opt/file.txt",
  38.     "gid": 1000,
  39.     "group": "devops",
  40.     "mode": "0777",
  41.     "owner": "devops",
  42.     "secontext": "unconfined_u:object_r:usr_t:s0",
  43.     "size": 0,
  44.     "state": "file",
  45.     "uid": 1000
  46. }
  47. 创建文件
复制代码
在这里,我们不止使用了path和state,还使用了mode、owner、group,分别对应着权限、拥有人、拥有组。
mode-owner-group

<blockquote>

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

科技颠覆者

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表