layout: post
title: Ad-Hoc应用场景和实例
date: 2018-01-13
tags: [“Ansible”,”自动化运维工具”]


 

一、Ansible的配置

1、ansible的主配置文件配置

我们主控机的ip:172.18.0.253

  1. 1、我们先修改几出配置文件
  2. vim /etc/ansible/ansible.cfg
  3. host_key_checking = False #取消这一项的注释

2、ansible的INVENTORY配置

  1. [root@ansible /]# vim /etc/ansible/hosts
  2. 添加如下配置
  3. [webserver]
  4. 172.18.30.[1:2]
  5. [loadbalance]
  6. 172.18.30.2
  7. [mysql]
  8. 172.18.30.3
  9. [middle]
  10. 172.18.30.4

我们配置了4项主机组

二、Ad-Hoc演练

注意一点,主控机需要事先将pub-key传送到受控机,否则每次操作都会提示输入密码,相当繁琐

1)ping模块,测试检测主机的存活(改 模块虽然叫ping,但是其并不依赖icmp协议,当受控机器设置了忽略icmp时,不会影响这个操作)

  1. @172.18.30.253
  2. [root@ansible /]# cat /etc/ansible/hosts ' grep '^\[' #获取主机组标签
  3. [webserver]
  4. [loadbalance]
  5. [mysql]
  6. [middle]
  7.  
  8. [root@ansible /]# ansible "webserver" -m ping
  9. 172.18.30.1 ' SUCCESS => {
  10. "changed": false,
  11. "ping": "pong"
  12. }
  13. 172.18.30.2 ' SUCCESS => {
  14. "changed": false,
  15. "ping": "pong"
  16. }
  17.  
  18. 现在我们将172.18.30.2这台主机的网络服务关闭,再执行 ansible "webserver" -m ping
  19.  
  20. @172.18.30.2
  21. [root@localhost ~]# systemctl stop network
  22.  
  23. @172.18.30.253
  24. [root@ansible /]# ansible "webserver" -m ping
  25. 172.18.30.1 ' SUCCESS => {
  26. "changed": false,
  27. "ping": "pong"
  28. }
  29. 172.18.30.2 ' UNREACHABLE! => {
  30. "changed": false,
  31. "msg": "Failed to connect to the host via ssh: ssh: connect to host 172.18.30.2 port 22: Connection timed out\r\n",
  32. "unreachable": true
  33. }
  34.  
  2)command模块,在受控机器只想能够shell命令,但不支持远端的管道、变量引用等操作
  1. [root@ansible /]# ansible "*" -m command -a 'ls /app ' #查看所有机器下/app 目录的内容
  2. 172.18.30.4 ' SUCCESS ' rc=0 >>
  3.  
  4. 172.18.30.2 ' SUCCESS ' rc=0 >>
  5. f1.sh
  6.  
  7. 172.18.30.1 ' SUCCESS ' rc=0 >>
  8. f1.sh
  9.  
  10. 172.18.30.3 ' SUCCESS ' rc=0 >>
  11.  
  12. [root@ansible /]# ansible "~172.18.30.[12]" -a 'cat /app/f1.sh' #查看172.18.30.1和2机器上/app/f1.sh的内容 ,"~"表示使用正则表达式
  13. 172.18.30.2 ' SUCCESS ' rc=0 >>
  14. uname -r
  15. hostname
  16.  
  17. 172.18.30.1 ' SUCCESS ' rc=0 >>
  18. uname -r
  19. hostname
  20.  
  21. [root@ansible /]# ansible "~172.18.30.[12]" -a 'rm -rf /app/f1.sh' #删除172.18.30.1和2机器上的/app/f1.sh
  22. [WARNING]: Consider using file module with state=absent rather than running rm
  23. 这里有个提示,让我们使用absent来代替rm,后面我们会演示,这里虽然提示了,但是命令执行是成功的。
  24.  
  25. 172.18.30.2 ' SUCCESS ' rc=0 >>
  26.  
  27. 172.18.30.1 ' SUCCESS ' rc=0 >>
  28.  

3)shell模块(过于复杂的命令有时也会执行失败,建议写到脚本内,然后copy到受控机,然后远程后自行脚本)

  1. [root@ansible /]# ansible "~172.18.30.*" -m shell -a 'echo $HOSTNAME' #查看172.18.30.段下所有机器的主机名,注意,执行的命令引用了变量,必须用单引号引起来,否则改变量引用的是本地的变量,而不是远端的。
  2. 172.18.30.1 ' SUCCESS ' rc=0 >>
  3. localhost.localdomain
  4.  
  5. 172.18.30.2 ' SUCCESS ' rc=0 >>
  6. localhost.localdomain
  7.  
  8. 172.18.30.3 ' SUCCESS ' rc=0 >>
  9. localhost.localdomain
  10.  
  11. 172.18.30.4 ' SUCCESS ' rc=0 >>
  12. localhost.localdomain

4)script 模块(在远端执行本地脚本)

  1. @172.18.30.253
  2. [root@ansible shell]# cat scanip.sh
  3. > /app/ip_up.log
  4. > /app/ip_down.log
  5. #$1是netmask为255.255.255.0的网段
  6. net=$1
  7. for i in {1..254};
  8. do
  9. { if ping -c1 -W1 $net.$i &>/dev/null;then
  10. echo $net.$i is up >> ./ip_up.log
  11. else
  12. echo $net.$i is down >> ./ip_down.log
  13. fi
  14. } &
  15. done
  16. wait #退出
  17.  
  18. 我们准备执行这个脚本文件
  19.  
  20. [root@ansible shell]# ansible-doc -s script #获取script的选项
  21. - name: Runs a local script on a remote node after transferring it
  22. script:
  23. chdir: # cd into this directory on the remote node before
  24. running the
  25. script
  26. creates: # a filename, when it already exists, this step
  27. will *not* be
  28. run.
  29. decrypt: # This option controls the autodecryption of
  30. source files
  31. using vault.
  32. free_form: # (required) Path to the local script file
  33. followed by
  34. optional
  35. arguments. There
  36. is no parameter
  37. actually named
  38. 'free form'; see
  39. the examples!
  40. removes: # a filename, when it does not exist, this step
  41. will *not* be
  42. run.
  43.  
  44. 我们需要用到 chdir选项,否则脚本的log文件会不知道存放在哪里
  45. [root@ansible shell]# ansible '*' -m script -a 'chdir=/app /root/zsfile/shell/scanip.sh 172.18.30'
  46. 172.18.30.4 ' SUCCESS => {
  47. "changed": true,
  48. "rc": 0,
  49. "stderr": "Shared connection to 172.18.30.4 closed.\r\n",
  50. "stdout": "",
  51. "stdout_lines": []
  52. }
  53. 172.18.30.2 ' SUCCESS => {
  54. "changed": true,
  55. "rc": 0,
  56. "stderr": "Shared connection to 172.18.30.2 closed.\r\n",
  57. "stdout": "",
  58. "stdout_lines": []
  59. }
  60. 172.18.30.1 ' SUCCESS => {
  61. "changed": true,
  62. "rc": 0,
  63. "stderr": "Shared connection to 172.18.30.1 closed.\r\n",
  64. "stdout": "",
  65. "stdout_lines": []
  66. }
  67. 172.18.30.3 ' SUCCESS => {
  68. "changed": true,
  69. "rc": 0,
  70. "stderr": "Shared connection to 172.18.30.3 closed.\r\n",
  71. "stdout": "",
  72. "stdout_lines": []
  73. }
  74.  
  75. @172.18.30.1
  76. [root@localhost app]# cat /app/ip_up.log
  77. 172.18.30.1 is up
  78. 172.18.30.2 is up
  79. 172.18.30.3 is up
  80. 172.18.30.4 is up
  81. 172.18.30.253 is up
  82. 172.18.30.254 is up
  83.  
  84. 脚本已经在目标机器成功执行了

不管什么模块的子参数都可以通过ansible-doc 模块来查看

5)Copy模块,拷贝本地的文件到受控主机

  1. @172.18.30.253
  2. [root@ansible app]# ansible-doc -s copy
  3. - name: Copies files to remote locations
  4. copy:
  5. attributes: # Attributes the file or directory should have. To get supported flags look at the man page for `chattr' on the
  6. target system. This string should contain the attributes in the same order as
  7. the one displayed by `lsattr'.
  8. backup: # Create a backup file including the timestamp information so you can get the original file back if you somehow
  9. clobbered it incorrectly.
  10. content: # When used instead of `src', sets the contents of a file directly to the specified value. For anything
  11. advanced or with formatting also look at the template module.
  12. decrypt: # This option controls the autodecryption of source files using vault.
  13. dest: # (required) Remote absolute path where the file should be copied to. If `src' is a directory, this must be a
  14. directory too. If `dest' is a nonexistent path and if either `dest' ends with
  15. "/" or `src' is a directory, `dest' is created. If `src' and `dest' are files,
  16. the parent directory of `dest' isn't created: the task fails if it doesn't
  17. already exist.
  18. directory_mode: # When doing a recursive copy set the mode for the directories. If this is not set we will use the system
  19. defaults. The mode is only set on directories which are newly created, and
  20. will not affect those that already existed.
  21. follow: # This flag indicates that filesystem links in the destination, if they exist, should be followed.
  22. force: # the default is `yes', which will replace the remote file when contents are different than the source. If
  23. `no', the file will only be transferred if the destination does not exist.
  24. group: # Name of the group that should own the file/directory, as would be fed to `chown'.
  25. local_follow: # This flag indicates that filesystem links in the source tree, if they exist, should be followed.
  26. mode: # Mode the file or directory should be. For those used to `/usr/bin/chmod' remember that modes are actually
  27. octal numbers (like 0644). Leaving off the leading zero will likely have
  28. unexpected results. As of version 1.8, the mode may be specified as a symbolic
  29. mode (for example, `u+rwx' or `u=rw,g=r,o=r').
  30. owner: # Name of the user that should own the file/directory, as would be fed to `chown'.
  31. remote_src: # If `no', it will search for `src' at originating/master machine. If `yes' it will go to the remote/target
  32. machine for the `src'. Default is `no'. Currently `remote_src' does not
  33. support recursive copying.
  34. selevel: # Level part of the SELinux file context. This is the MLS/MCS attribute, sometimes known as the `range'.
  35. `_default' feature works as for `seuser'.
  36. serole: # Role part of SELinux file context, `_default' feature works as for `seuser'.
  37. setype: # Type part of SELinux file context, `_default' feature works as for `seuser'.
  38. seuser: # User part of SELinux file context. Will default to system policy, if applicable. If set to `_default', it
  39. will use the `user' portion of the policy if available.
  40. src: # Local path to a file to copy to the remote server; can be absolute or relative. If path is a directory, it is
  41. copied recursively. In this case, if path ends with "/", only inside contents
  42. of that directory are copied to destination. Otherwise, if it does not end
  43. with "/", the directory itself with all contents is copied. This behavior is
  44. similar to Rsync.
  45. unsafe_writes: # Normally this module uses atomic operations to prevent data corruption or inconsistent reads from the target
  46. files, sometimes systems are configured or just broken in ways that prevent
  47. this. One example are docker mounted files, they cannot be updated atomically
  48. and can only be done in an unsafe manner. This boolean option allows ansible
  49. to fall back to unsafe methods of updating files for those cases in which you
  50. do not have any other choice. Be aware that this is subject to race conditions
  51. and can lead to data corruption.
  52. validate: # The validation command to run before copying into place. The path to the file to validate is passed in via
  53. '%s' which must be present as in the example below. The command is passed
  54. securely so shell features like expansion and pipes won't work.
  55.  
  56. 模块的用法:主要的参数 src dest 分别代表原目录和目标目录,其中还可以使用mode指定权限,owner指定所有者,group所属组
  57.  
  58. [root@ansible app]# ansible 'webserver' -m copy -a 'remote_src=yes src=/app/fist.des3 dest=/app/fist.des3 mode=600 owner=root group=root'
  59. 172.18.30.1 ' FAILED! => {
  60. "changed": false,
  61. "msg": "Source /app/fist.des3 not found"
  62. }
  63. 172.18.30.2 ' FAILED! => {
  64. "changed": false,
  65. "msg": "Source /app/fist.des3 not found"
  66. }
  67.  
  68. 这次执行失败是因为"remote_src=yes "这个选项, 因为在受控机器并没有fist.des3这个文件
  69.  
  70. [root@ansible app]# ansible 'webserver' -m copy -a 'src=/app/fist.des3 dest=/app/fist.des3 mode=600 owner=root group=root'
  71. 172.18.30.2 ' SUCCESS => {
  72. "changed": true,
  73. "checksum": "e5fa44f2b31c1fb553b6021e7360d07d5d91ff5e",
  74. "dest": "/app/fist.des3",
  75. "gid": 0,
  76. "group": "root",
  77. "md5sum": "b026324c6904b2a9cb4b88d6d61c81d1",
  78. "mode": "0600",
  79. "owner": "root",
  80. "size": 2,
  81. "src": "/root/.ansible/tmp/ansible-tmp-1515830878.84-276590900255815/source",
  82. "state": "file",
  83. "uid": 0
  84. }
  85. 172.18.30.1 ' SUCCESS => {
  86. "changed": true,
  87. "checksum": "e5fa44f2b31c1fb553b6021e7360d07d5d91ff5e",
  88. "dest": "/app/fist.des3",
  89. "gid": 0,
  90. "group": "root",
  91. "md5sum": "b026324c6904b2a9cb4b88d6d61c81d1",
  92. "mode": "0600",
  93. "owner": "root",
  94. "size": 2,
  95. "src": "/root/.ansible/tmp/ansible-tmp-1515830878.81-263414101661294/source",
  96. "state": "file",
  97. "uid": 0
  98. }
  99.  
  100. @172.18.30.1
  101. [root@localhost app]# ll /app
  102. 总用量 16
  103. -rw------- 1 root root 2 1月 13 16:07 fist.des3 #权限已经改变了
  104. -rw-r--r-- 1 root root 5356 1月 13 15:36 ip_down.log
  105. -rw------- 1 root root 112 1月 13 15:36 ip_up.log

6)Cron 计划任务

支持时间:minute,hour,day,month,weekday

  1. @172.18.30.253
  2. [root@ansible app]# ansible-doc -s cron
  3. - name: Manage cron.d and crontab entries.
  4. cron:
  5. backup: # If set, create a backup of the crontab before it is modified. The location of the backup is returned in the
  6. `backup_file' variable by this module.
  7. cron_file: # If specified, uses this file instead of an individual user's crontab. If this is a relative path, it is
  8. interpreted with respect to /etc/cron.d. (If it is absolute, it will typically
  9. be /etc/crontab). Many linux distros expect (and some require) the filename
  10. portion to consist solely of upper- and lower-case letters, digits,
  11. underscores, and hyphens. To use the `cron_file' parameter you must specify
  12. the `user' as well.
  13. day: # Day of the month the job should run ( 1-31, *, */2, etc )
  14. disabled: # If the job should be disabled (commented out) in the crontab. Only has effect if state=present
  15. env: # If set, manages a crontab's environment variable. New variables are added on top of crontab. "name" and
  16. "value" parameters are the name and the value of environment variable.
  17. hour: # Hour when the job should run ( 0-23, *, */2, etc )
  18. insertafter: # Used with `state=present' and `env'. If specified, the environment variable will be inserted after the
  19. declaration of specified environment variable.
  20. insertbefore: # Used with `state=present' and `env'. If specified, the environment variable will be inserted before the
  21. declaration of specified environment variable.
  22. job: # The command to execute or, if env is set, the value of environment variable. The command should not contain
  23. line breaks. Required if state=present.
  24. minute: # Minute when the job should run ( 0-59, *, */2, etc )
  25. month: # Month of the year the job should run ( 1-12, *, */2, etc )
  26. name: # Description of a crontab entry or, if env is set, the name of environment variable. Required if state=absent.
  27. Note that if name is not set and state=present, then a new crontab entry will
  28. always be created, regardless of existing ones.
  29. reboot: # If the job should be run at reboot. This option is deprecated. Users should use special_time.
  30. special_time: # Special time specification nickname.
  31. state: # Whether to ensure the job or environment variable is present or absent.
  32. user: # The specific user whose crontab should be modified.
  33. weekday: # Day of the week that the job should run ( 0-6 for Sunday-Saturday, *, etc )
  34.  
  35. }
  36.  
  37. 添加定时任务
  38. [root@ansible app]# ansible '*' -m cron -a 'state=present user=root name=test minute=10 job="wall ok"'
  39. 172.18.30.3 ' SUCCESS => {
  40. "changed": true,
  41. "envs": [],
  42. "jobs": [
  43. "test"
  44. ]
  45. }
  46. 172.18.30.1 ' SUCCESS => {
  47. "changed": true,
  48. "envs": [],
  49. "jobs": [
  50. "test"
  51. ]
  52. }
  53. 172.18.30.4 ' SUCCESS => {
  54. "changed": true,
  55. "envs": [],
  56. "jobs": [
  57. "test"
  58. ]
  59. }
  60. 172.18.30.2 ' SUCCESS => {
  61. "changed": true,
  62. "envs": [],
  63. "jobs": [
  64. "test"
  65. ]
  66. }
  67.  
  68. @172.18.30.1
  69. [root@localhost app]# crontab -e
  70. #Ansible: test
  71. 10 * * * * wall ok
  72.  
  73. @172.18.30.253
  74. 取消刚才的定时任务
  75. [root@ansible app]# ansible '*' -m cron -a 'state=absent user=root name=test minute=10 job="wall ok"'
  76. 172.18.30.3 ' SUCCESS => {
  77. "changed": true,
  78. "envs": [],
  79. "jobs": []
  80. }
  81. 172.18.30.2 ' SUCCESS => {
  82. "changed": true,
  83. "envs": [],
  84. "jobs": []
  85. }
  86. 172.18.30.1 ' SUCCESS => {
  87. "changed": true,
  88. "envs": [],
  89. "jobs": []
  90. }
  91. 172.18.30.4 ' SUCCESS => {
  92. "changed": true,
  93. "envs": [],
  94. "jobs": []
  95. }
  96.  
  97. @172.18.30.1
  98. [root@localhost app]# crontab -e
  99. 已经没有定时任务

7)Fetch模块,将远程主机的指定文件复制到管理机的指定目录,与copy模块相反

  1. [root@ansible app]# ansible '*' -m fetch -a 'dest=/app src=/app/ip_down.log'
  2. 172.18.30.4 ' SUCCESS => {
  3. "changed": true,
  4. "checksum": "761cb53a1208b1e6a7004108a597e02082f3b630",
  5. "dest": "/app/172.18.30.4/app/ip_down.log",
  6. "md5sum": "3b1576b36d72897ecaf798faa7129635",
  7. "remote_checksum": "761cb53a1208b1e6a7004108a597e02082f3b630",
  8. "remote_md5sum": null
  9. }
  10. 172.18.30.3 ' SUCCESS => {
  11. "changed": true,
  12. "checksum": "8c946be6ff9777014f992cf77417231626adaca9",
  13. "dest": "/app/172.18.30.3/app/ip_down.log",
  14. "md5sum": "784d01045ed8fdbdd4ba59e092244573",
  15. "remote_checksum": "8c946be6ff9777014f992cf77417231626adaca9",
  16. "remote_md5sum": null
  17. }
  18. 172.18.30.1 ' SUCCESS => {
  19. "changed": true,
  20. "checksum": "2302bde1cb0aa030da62961348835d2eaaaff6ff",
  21. "dest": "/app/172.18.30.1/app/ip_down.log",
  22. "md5sum": "0ed208a07074c8db42749007360b3779",
  23. "remote_checksum": "2302bde1cb0aa030da62961348835d2eaaaff6ff",
  24. "remote_md5sum": null
  25. }
  26. 172.18.30.2 ' SUCCESS => {
  27. "changed": true,
  28. "checksum": "11515e50e0ab4dbe4d6783c6da35b7a1a798a5bb",
  29. "dest": "/app/172.18.30.2/app/ip_down.log",
  30. "md5sum": "58362c368725e2f4d85d85d8fa4118e5",
  31. "remote_checksum": "11515e50e0ab4dbe4d6783c6da35b7a1a798a5bb",
  32. "remote_md5sum": null
  33. }
  34.  
  35. 将远程主机的ip_down.log文件拷贝到本地的/app目录下
  36.  
  37. [root@ansible app]# tree /app
  38. /app
  39. ├── 172.18.30.1
  40. └── app
  41. └── ip_down.log
  42. ├── 172.18.30.2
  43. └── app
  44. └── ip_down.log
  45. ├── 172.18.30.3
  46. └── app
  47. └── ip_down.log
  48. ├── 172.18.30.4
  49. └── app
  50. └── ip_down.log
  51. ├── fist.des3
  52. ├── fist_encrypt
  53. ├── ip_down.log
  54. ├── ip_up.log
  55. ├── new_file
  56. ├── private_key
  57. ├── private_key.tmp
  58. ├── pub_key
  59. └── pub_key.tmp
  60.  
  61. 8 directories, 13 files
  62.  
  63. 完成之后,会按照主机的IP进行命名目录

8)File设置文件属性

  1. @172.18.30.253
  2. [root@ansible app]# ansible-doc -s file
  3. - name: Sets attributes of files
  4. file:
  5. attributes: # Attributes the file or directory should have. To get supported flags look at the man page for `chattr' on the
  6. target system. This string should contain the attributes in the same order as
  7. the one displayed by `lsattr'.
  8. follow: # This flag indicates that filesystem links, if they exist, should be followed.
  9. force: # force the creation of the symlinks in two cases: the source file does not exist (but will appear later); the
  10. destination exists and is a file (so, we need to unlink the "path" file and
  11. create symlink to the "src" file in place of it).
  12. group: # Name of the group that should own the file/directory, as would be fed to `chown'.
  13. mode: # Mode the file or directory should be. For those used to `/usr/bin/chmod' remember that modes are actually
  14. octal numbers (like 0644). Leaving off the leading zero will likely have
  15. unexpected results. As of version 1.8, the mode may be specified as a symbolic
  16. mode (for example, `u+rwx' or `u=rw,g=r,o=r').
  17. owner: # Name of the user that should own the file/directory, as would be fed to `chown'.
  18. path: # (required) path to the file being managed. Aliases: `dest', `name'
  19. recurse: # recursively set the specified file attributes (applies only to state=directory)
  20. selevel: # Level part of the SELinux file context. This is the MLS/MCS attribute, sometimes known as the `range'.
  21. `_default' feature works as for `seuser'.
  22. serole: # Role part of SELinux file context, `_default' feature works as for `seuser'.
  23. setype: # Type part of SELinux file context, `_default' feature works as for `seuser'.
  24. seuser: # User part of SELinux file context. Will default to system policy, if applicable. If set to `_default', it
  25. will use the `user' portion of the policy if available.
  26. src: # path of the file to link to (applies only to `state=link'). Will accept absolute, relative and nonexisting
  27. paths. Relative paths are not expanded.
  28. state: # If `directory', all immediate subdirectories will be created if they do not exist, since 1.7 they will be
  29. created with the supplied permissions. If `file', the file will NOT be created
  30. if it does not exist, see the [copy] or [template] module if you want that
  31. behavior. If `link', the symbolic link will be created or changed. Use `hard'
  32. for hardlinks. If `absent', directories will be recursively deleted, and files
  33. or symlinks will be unlinked. Note that `absent' will not cause `file' to fail
  34. if the `path' does not exist as the state did not change. If `touch' (new in
  35. 1.4), an empty file will be created if the `path' does not exist, while an
  36. existing file or directory will receive updated file access and modification
  37. times (similar to the way `touch` works from the command line).
  38. unsafe_writes: # Normally this module uses atomic operations to prevent data corruption or inconsistent reads from the target
  39. files, sometimes systems are configured or just broken in ways that prevent
  40. this. One example are docker mounted files, they cannot be updated atomically
  41. and can only be done in an unsafe manner. This boolean option allows ansible
  42. to fall back to unsafe methods of updating files for those cases in which you
  43. do not have any other choice. Be aware that this is subject to race conditions
  44. and can lead to data corruption.
  45.  
  46. 几个重要的属性
  47. group 设置属组
  48. mode 设置权限
  49. owner 社会资所有人
  50. path 设置文件路径
  51. recurse 设置是否使用递归,只有在目标是目录时才可以使用
  52. src 创建软连接时使用
  53. state 状态 directory file link hard absent touch path 这几种,用法参考模块文档
  54.  
  55. [root@ansible /]# ansible '*' -m file -a 'state=file path=/app/ip_up.log mode=0600 '
  56. 172.18.30.4 ' SUCCESS => {
  57. "changed": true,
  58. "gid": 0,
  59. "group": "root",
  60. "mode": "0600",
  61. "owner": "root",
  62. "path": "/app/ip_up.log",
  63. "size": 112,
  64. "state": "file",
  65. "uid": 0
  66. }
  67. 172.18.30.1 ' SUCCESS => {
  68. "changed": false,
  69. "gid": 0,
  70. "group": "root",
  71. "mode": "0600",
  72. "owner": "root",
  73. "path": "/app/ip_up.log",
  74. "size": 112,
  75. "state": "file",
  76. "uid": 0
  77. }
  78. 172.18.30.2 ' SUCCESS => {
  79. "changed": false,
  80. "gid": 0,
  81. "group": "root",
  82. "mode": "0600",
  83. "owner": "root",
  84. "path": "/app/ip_up.log",
  85. "size": 112,
  86. "state": "file",
  87. "uid": 0
  88. }
  89. 172.18.30.3 ' SUCCESS => {
  90. "changed": true,
  91. "gid": 0,
  92. "group": "root",
  93. "mode": "0600",
  94. "owner": "root",
  95. "path": "/app/ip_up.log",
  96. "size": 112,
  97. "state": "file",
  98. "uid": 0
  99. }
  100.  
  101. 下面设置软连接
  102. [root@ansible /]# ansible 'mysql' -m file -a 'state=link src=/app/ip_up.log dest=/app/log.log'
  103. 172.18.30.3 ' SUCCESS => {
  104. "changed": true,
  105. "dest": "/app/log.log",
  106. "gid": 0,
  107. "group": "root",
  108. "mode": "0777",
  109. "owner": "root",
  110. "size": 14,
  111. "src": "/app/ip_up.log",
  112. "state": "link",
  113. "uid": 0
  114. }
  115. @172.18.30.3
  116.  
  117. [root@localhost ~]# cd /app
  118. [root@localhost app]# ll
  119. 总用量 12
  120. -rw-r--r-- 1 root root 5356 1月 13 21:10 ip_down.log
  121. -rw------- 1 root root 112 1月 13 21:10 ip_up.log
  122. lrwxrwxrwx 1 root root 14 1月 13 23:54 log.log -> /app/ip_up.log
  123.  
  124. 下面递归设置目录权限
  125.  
  126. [root@ansible /]# ansible 'mysql' -m file -a 'state=directory mode=440 recurse=yes path=/app'
  127. 172.18.30.3 ' SUCCESS => {
  128. "changed": true,
  129. "gid": 0,
  130. "group": "root",
  131. "mode": "0440",
  132. "owner": "root",
  133. "path": "/app",
  134. "size": 57,
  135. "state": "directory",
  136. "uid": 0
  137. }
  138.  
  139. @172.18.30.3
  140. [root@localhost app]# ll
  141. 总用量 12
  142. -r--r----- 1 root root 5356 1 13 21:10 ip_down.log
  143. -r--r----- 1 root root 112 1 13 21:10 ip_up.log
  144. lrwxrwxrwx 1 root root 14 1 13 23:54 log.log -> /app/ip_up.log
  145. [root@localhost app]# cd ..
  146. [root@localhost /]# ll ' grep app
  147. dr--r-----. 2 root root 57 1月 13 23:54 app
  148.  
  149. 可以看出/app 的目录权限已经更改了,但是默认软连接的权限不会更改

9)hostname

  1. [root@ansible /]# ansible 'mysql' -m hostname -a 'name=mysql.joker.com'
  2. 172.18.30.3 ' SUCCESS => {
  3. "ansible_facts": {
  4. "ansible_domain": "joker.com",
  5. "ansible_fqdn": "mysql.joker.com",
  6. "ansible_hostname": "mysql",
  7. "ansible_nodename": "mysql.joker.com"
  8. },
  9. "changed": false,
  10. "name": "mysql.joker.com"
  11. }
  12.  
  13. 修改主机名

10)Yum包管理

  1. 安装httpd
  2. [root@ansible /]# ansible 'webserver' -m yum -a 'state=present name=httpd'
  3. 172.18.30.2 ' SUCCESS => {
  4. "changed": false,
  5. "msg": "",
  6. "rc": 0,
  7. "results": [
  8. "httpd-2.4.6-67.el7.centos.6.x86_64 providing httpd is already installed"
  9. ]
  10. }
  11. 172.18.30.1 ' SUCCESS => {
  12. "changed": false,
  13. "msg": "",
  14. "rc": 0,
  15. "results": [
  16. "httpd-2.4.6-67.el7.centos.6.x86_64 providing httpd is already installed"
  17. ]
  18. }
  19.  
  20. 卸载httpd
  21. [root@ansible /]# ansible 'webserver' -m yum -a 'state=absent name=httpd'
  22. 172.18.30.1 ' SUCCESS => {
  23. "changed": true,
  24. "msg": "",
  25. "rc": 0,
  26. "results": [
  27. "已加载插件:fastestmirror\n正在解决依赖关系\n--> 正在检查事务\n---> 软件包 httpd.x86_64.0.2.4.6-67.el7.centos.6 将被 删除\n--> 解决依赖关系完成\n\n依赖关系解决\n\n================================================================================\n Package 架构 版本 源 大小\n================================================================================\n正在删除:\n httpd x86_64 2.4.6-67.el7.centos.6 @updates 9.4 M\n\n事务概要\n================================================================================\n移除 1 软件包\n\n安装大小:9.4 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n 正在删除 : httpd-2.4.6-67.el7.centos.6.x86_64 1/1 \n 验证中 : httpd-2.4.6-67.el7.centos.6.x86_64 1/1 \n\n删除:\n httpd.x86_64 0:2.4.6-67.el7.centos.6 \n\n完毕!\n"
  28. ]
  29. }
  30. 172.18.30.2 ' SUCCESS => {
  31. "changed": true,
  32. "msg": "",
  33. "rc": 0,
  34. "results": [
  35. "已加载插件:fastestmirror\n正在解决依赖关系\n--> 正在检查事务\n---> 软件包 httpd.x86_64.0.2.4.6-67.el7.centos.6 将被 删除\n--> 解决依赖关系完成\n\n依赖关系解决\n\n================================================================================\n Package 架构 版本 源 大小\n================================================================================\n正在删除:\n httpd x86_64 2.4.6-67.el7.centos.6 @updates 9.4 M\n\n事务概要\n================================================================================\n移除 1 软件包\n\n安装大小:9.4 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n 正在删除 : httpd-2.4.6-67.el7.centos.6.x86_64 1/1 \n 验证中 : httpd-2.4.6-67.el7.centos.6.x86_64 1/1 \n\n删除:\n httpd.x86_64 0:2.4.6-67.el7.centos.6 \n\n完毕!\n"
  36. ]
  37. }
  38.  
  39. 以上结果的乱码有yum导致的,可以通过sed命令来做操作

11)Service管理服务

  1. 停止服务
  2. [root@ansible /]# ansible 'webserver' -m service -a 'state=stopped name=crond'
  3. 172.18.30.2 ' SUCCESS => {
  4. "changed": true,
  5. "name": "crond",
  6. "state": "stopped",
  7. "status": {
  8. "ActiveEnterTimestamp": "六 2018-01-13 20:36:07 CST",
  9. "ActiveEnterTimestampMonotonic": "4116254",
  10. "ActiveExitTimestampMonotonic": "0",
  11. "ActiveState": "active",
  12. "After": "system.slice systemd-journald.socket basic.target auditd.service systemd-user-sessions.service time-sync.target",
  13. "AllowIsolate": "no",
  14. "AmbientCapabilities": "0",
  15. "AssertResult": "yes",
  16. "AssertTimestamp": "六 2018-01-13 20:36:07 CST",
  17. "AssertTimestampMonotonic": "4109479",
  18. "Before": "multi-user.target shutdown.target",
  19. "BlockIOAccounting": "no",
  20. "BlockIOWeight": "18446744073709551615",
  21. "CPUAccounting": "no",
  22. "CPUQuotaPerSecUSec": "infinity",
  23. "CPUSchedulingPolicy": "0",
  24. "CPUSchedulingPriority": "0",
  25. "CPUSchedulingResetOnFork": "no",
  26. "CPUShares": "18446744073709551615",
  27. "CanIsolate": "no",
  28. "CanReload": "yes",
  29. "CanStart": "yes",
  30. "CanStop": "yes",
  31. "CapabilityBoundingSet": "18446744073709551615",
  32. "ConditionResult": "yes",
  33. "ConditionTimestamp": "六 2018-01-13 20:36:07 CST",
  34. "ConditionTimestampMonotonic": "4109479",
  35. "Conflicts": "shutdown.target",
  36. "ControlGroup": "/system.slice/crond.service",
  37. "ControlPID": "0",
  38. "DefaultDependencies": "yes",
  39. "Delegate": "no",
  40. "Description": "Command Scheduler",
  41. "DevicePolicy": "auto",
  42. "EnvironmentFile": "/etc/sysconfig/crond (ignore_errors=no)",
  43. "ExecMainCode": "0",
  44. "ExecMainExitTimestampMonotonic": "0",
  45. "ExecMainPID": "698",
  46. "ExecMainStartTimestamp": "六 2018-01-13 20:36:07 CST",
  47. "ExecMainStartTimestampMonotonic": "4116176",
  48. "ExecMainStatus": "0",
  49. "ExecReload": "{ path=/bin/kill ; argv[]=/bin/kill -HUP $MAINPID ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }",
  50. "ExecStart": "{ path=/usr/sbin/crond ; argv[]=/usr/sbin/crond -n $CRONDARGS ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }",
  51. "FailureAction": "none",
  52. "FileDescriptorStoreMax": "0",
  53. "FragmentPath": "/usr/lib/systemd/system/crond.service",
  54. "GuessMainPID": "yes",
  55. "IOScheduling": "0",
  56. "Id": "crond.service",
  57. "IgnoreOnIsolate": "no",
  58. "IgnoreOnSnapshot": "no",
  59. "IgnoreSIGPIPE": "yes",
  60. "InactiveEnterTimestampMonotonic": "0",
  61. "InactiveExitTimestamp": "六 2018-01-13 20:36:07 CST",
  62. "InactiveExitTimestampMonotonic": "4116254",
  63. "JobTimeoutAction": "none",
  64. "JobTimeoutUSec": "0",
  65. "KillMode": "process",
  66. "KillSignal": "15",
  67. "LimitAS": "18446744073709551615",
  68. "LimitCORE": "18446744073709551615",
  69. "LimitCPU": "18446744073709551615",
  70. "LimitDATA": "18446744073709551615",
  71. "LimitFSIZE": "18446744073709551615",
  72. "LimitLOCKS": "18446744073709551615",
  73. "LimitMEMLOCK": "65536",
  74. "LimitMSGQUEUE": "819200",
  75. "LimitNICE": "0",
  76. "LimitNOFILE": "4096",
  77. "LimitNPROC": "1802",
  78. "LimitRSS": "18446744073709551615",
  79. "LimitRTPRIO": "0",
  80. "LimitRTTIME": "18446744073709551615",
  81. "LimitSIGPENDING": "1802",
  82. "LimitSTACK": "18446744073709551615",
  83. "LoadState": "loaded",
  84. "MainPID": "698",
  85. "MemoryAccounting": "no",
  86. "MemoryCurrent": "18446744073709551615",
  87. "MemoryLimit": "18446744073709551615",
  88. "MountFlags": "0",
  89. "Names": "crond.service",
  90. "NeedDaemonReload": "no",
  91. "Nice": "0",
  92. "NoNewPrivileges": "no",
  93. "NonBlocking": "no",
  94. "NotifyAccess": "none",
  95. "OOMScoreAdjust": "0",
  96. "OnFailureJobMode": "replace",
  97. "PermissionsStartOnly": "no",
  98. "PrivateDevices": "no",
  99. "PrivateNetwork": "no",
  100. "PrivateTmp": "no",
  101. "ProtectHome": "no",
  102. "ProtectSystem": "no",
  103. "RefuseManualStart": "no",
  104. "RefuseManualStop": "no",
  105. "RemainAfterExit": "no",
  106. "Requires": "basic.target",
  107. "Restart": "no",
  108. "RestartUSec": "100ms",
  109. "Result": "success",
  110. "RootDirectoryStartOnly": "no",
  111. "RuntimeDirectoryMode": "0755",
  112. "SameProcessGroup": "no",
  113. "SecureBits": "0",
  114. "SendSIGHUP": "no",
  115. "SendSIGKILL": "yes",
  116. "Slice": "system.slice",
  117. "StandardError": "inherit",
  118. "StandardInput": "null",
  119. "StandardOutput": "journal",
  120. "StartLimitAction": "none",
  121. "StartLimitBurst": "5",
  122. "StartLimitInterval": "10000000",
  123. "StartupBlockIOWeight": "18446744073709551615",
  124. "StartupCPUShares": "18446744073709551615",
  125. "StatusErrno": "0",
  126. "StopWhenUnneeded": "no",
  127. "SubState": "running",
  128. "SyslogLevelPrefix": "yes",
  129. "SyslogPriority": "30",
  130. "SystemCallErrorNumber": "0",
  131. "TTYReset": "no",
  132. "TTYVHangup": "no",
  133. "TTYVTDisallocate": "no",
  134. "TasksAccounting": "no",
  135. "TasksCurrent": "18446744073709551615",
  136. "TasksMax": "18446744073709551615",
  137. "TimeoutStartUSec": "1min 30s",
  138. "TimeoutStopUSec": "1min 30s",
  139. "TimerSlackNSec": "50000",
  140. "Transient": "no",
  141. "Type": "simple",
  142. "UMask": "0022",
  143. "UnitFilePreset": "enabled",
  144. "UnitFileState": "enabled",
  145. "WantedBy": "multi-user.target",
  146. "Wants": "system.slice",
  147. "WatchdogTimestamp": "六 2018-01-13 20:36:07 CST",
  148. "WatchdogTimestampMonotonic": "4116237",
  149. "WatchdogUSec": "0"
  150. }
  151. }
  152. 172.18.30.1 ' SUCCESS => {
  153. "changed": true,
  154. "name": "crond",
  155. "state": "stopped",
  156. "status": {
  157. "ActiveEnterTimestamp": "五 2018-01-12 09:18:48 CST",
  158. "ActiveEnterTimestampMonotonic": "3696327",
  159. "ActiveExitTimestampMonotonic": "0",
  160. "ActiveState": "active",
  161. "After": "auditd.service systemd-user-sessions.service system.slice time-sync.target systemd-journald.socket basic.target",
  162. "AllowIsolate": "no",
  163. "AmbientCapabilities": "0",
  164. "AssertResult": "yes",
  165. "AssertTimestamp": "五 2018-01-12 09:18:48 CST",
  166. "AssertTimestampMonotonic": "3696000",
  167. "Before": "shutdown.target multi-user.target",
  168. "BlockIOAccounting": "no",
  169. "BlockIOWeight": "18446744073709551615",
  170. "CPUAccounting": "no",
  171. "CPUQuotaPerSecUSec": "infinity",
  172. "CPUSchedulingPolicy": "0",
  173. "CPUSchedulingPriority": "0",
  174. "CPUSchedulingResetOnFork": "no",
  175. "CPUShares": "18446744073709551615",
  176. "CanIsolate": "no",
  177. "CanReload": "yes",
  178. "CanStart": "yes",
  179. "CanStop": "yes",
  180. "CapabilityBoundingSet": "18446744073709551615",
  181. "ConditionResult": "yes",
  182. "ConditionTimestamp": "五 2018-01-12 09:18:48 CST",
  183. "ConditionTimestampMonotonic": "3696000",
  184. "Conflicts": "shutdown.target",
  185. "ControlGroup": "/system.slice/crond.service",
  186. "ControlPID": "0",
  187. "DefaultDependencies": "yes",
  188. "Delegate": "no",
  189. "Description": "Command Scheduler",
  190. "DevicePolicy": "auto",
  191. "EnvironmentFile": "/etc/sysconfig/crond (ignore_errors=no)",
  192. "ExecMainCode": "0",
  193. "ExecMainExitTimestampMonotonic": "0",
  194. "ExecMainPID": "700",
  195. "ExecMainStartTimestamp": "五 2018-01-12 09:18:48 CST",
  196. "ExecMainStartTimestampMonotonic": "3696282",
  197. "ExecMainStatus": "0",
  198. "ExecReload": "{ path=/bin/kill ; argv[]=/bin/kill -HUP $MAINPID ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }",
  199. "ExecStart": "{ path=/usr/sbin/crond ; argv[]=/usr/sbin/crond -n $CRONDARGS ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }",
  200. "FailureAction": "none",
  201. "FileDescriptorStoreMax": "0",
  202. "FragmentPath": "/usr/lib/systemd/system/crond.service",
  203. "GuessMainPID": "yes",
  204. "IOScheduling": "0",
  205. "Id": "crond.service",
  206. "IgnoreOnIsolate": "no",
  207. "IgnoreOnSnapshot": "no",
  208. "IgnoreSIGPIPE": "yes",
  209. "InactiveEnterTimestampMonotonic": "0",
  210. "InactiveExitTimestamp": "五 2018-01-12 09:18:48 CST",
  211. "InactiveExitTimestampMonotonic": "3696327",
  212. "JobTimeoutAction": "none",
  213. "JobTimeoutUSec": "0",
  214. "KillMode": "process",
  215. "KillSignal": "15",
  216. "LimitAS": "18446744073709551615",
  217. "LimitCORE": "18446744073709551615",
  218. "LimitCPU": "18446744073709551615",
  219. "LimitDATA": "18446744073709551615",
  220. "LimitFSIZE": "18446744073709551615",
  221. "LimitLOCKS": "18446744073709551615",
  222. "LimitMEMLOCK": "65536",
  223. "LimitMSGQUEUE": "819200",
  224. "LimitNICE": "0",
  225. "LimitNOFILE": "4096",
  226. "LimitNPROC": "1802",
  227. "LimitRSS": "18446744073709551615",
  228. "LimitRTPRIO": "0",
  229. "LimitRTTIME": "18446744073709551615",
  230. "LimitSIGPENDING": "1802",
  231. "LimitSTACK": "18446744073709551615",
  232. "LoadState": "loaded",
  233. "MainPID": "700",
  234. "MemoryAccounting": "no",
  235. "MemoryCurrent": "18446744073709551615",
  236. "MemoryLimit": "18446744073709551615",
  237. "MountFlags": "0",
  238. "Names": "crond.service",
  239. "NeedDaemonReload": "no",
  240. "Nice": "0",
  241. "NoNewPrivileges": "no",
  242. "NonBlocking": "no",
  243. "NotifyAccess": "none",
  244. "OOMScoreAdjust": "0",
  245. "OnFailureJobMode": "replace",
  246. "PermissionsStartOnly": "no",
  247. "PrivateDevices": "no",
  248. "PrivateNetwork": "no",
  249. "PrivateTmp": "no",
  250. "ProtectHome": "no",
  251. "ProtectSystem": "no",
  252. "RefuseManualStart": "no",
  253. "RefuseManualStop": "no",
  254. "RemainAfterExit": "no",
  255. "Requires": "basic.target",
  256. "Restart": "no",
  257. "RestartUSec": "100ms",
  258. "Result": "success",
  259. "RootDirectoryStartOnly": "no",
  260. "RuntimeDirectoryMode": "0755",
  261. "SameProcessGroup": "no",
  262. "SecureBits": "0",
  263. "SendSIGHUP": "no",
  264. "SendSIGKILL": "yes",
  265. "Slice": "system.slice",
  266. "StandardError": "inherit",
  267. "StandardInput": "null",
  268. "StandardOutput": "journal",
  269. "StartLimitAction": "none",
  270. "StartLimitBurst": "5",
  271. "StartLimitInterval": "10000000",
  272. "StartupBlockIOWeight": "18446744073709551615",
  273. "StartupCPUShares": "18446744073709551615",
  274. "StatusErrno": "0",
  275. "StopWhenUnneeded": "no",
  276. "SubState": "running",
  277. "SyslogLevelPrefix": "yes",
  278. "SyslogPriority": "30",
  279. "SystemCallErrorNumber": "0",
  280. "TTYReset": "no",
  281. "TTYVHangup": "no",
  282. "TTYVTDisallocate": "no",
  283. "TasksAccounting": "no",
  284. "TasksCurrent": "18446744073709551615",
  285. "TasksMax": "18446744073709551615",
  286. "TimeoutStartUSec": "1min 30s",
  287. "TimeoutStopUSec": "1min 30s",
  288. "TimerSlackNSec": "50000",
  289. "Transient": "no",
  290. "Type": "simple",
  291. "UMask": "0022",
  292. "UnitFilePreset": "enabled",
  293. "UnitFileState": "enabled",
  294. "WantedBy": "multi-user.target",
  295. "Wants": "system.slice",
  296. "WatchdogTimestamp": "五 2018-01-12 09:18:48 CST",
  297. "WatchdogTimestampMonotonic": "3696305",
  298. "WatchdogUSec": "0"
  299. }
  300. }
  301.  
  302. 启动服务
  303. [root@ansible /]# ansible 'webserver' -m service -a 'state=started name=crond'
  304. 172.18.30.2 ' SUCCESS => {
  305. "changed": true,
  306. "name": "crond",
  307. "state": "started",
  308. "status": {
  309. "ActiveEnterTimestamp": "六 2018-01-13 20:36:07 CST",
  310. "ActiveEnterTimestampMonotonic": "4116254",
  311. "ActiveExitTimestamp": "日 2018-01-14 03:00:17 CST",
  312. "ActiveExitTimestampMonotonic": "23054283113",
  313. "ActiveState": "inactive",
  314. "After": "system.slice systemd-journald.socket basic.target auditd.service systemd-user-sessions.service time-sync.target",
  315. "AllowIsolate": "no",
  316. "AmbientCapabilities": "0",
  317. "AssertResult": "yes",
  318. "AssertTimestamp": "六 2018-01-13 20:36:07 CST",
  319. "AssertTimestampMonotonic": "4109479",
  320. "Before": "multi-user.target shutdown.target",
  321. "BlockIOAccounting": "no",
  322. "BlockIOWeight": "18446744073709551615",
  323. "CPUAccounting": "no",
  324. "CPUQuotaPerSecUSec": "infinity",
  325. "CPUSchedulingPolicy": "0",
  326. "CPUSchedulingPriority": "0",
  327. "CPUSchedulingResetOnFork": "no",
  328. "CPUShares": "18446744073709551615",
  329. "CanIsolate": "no",
  330. "CanReload": "yes",
  331. "CanStart": "yes",
  332. "CanStop": "yes",
  333. "CapabilityBoundingSet": "18446744073709551615",
  334. "ConditionResult": "yes",
  335. "ConditionTimestamp": "六 2018-01-13 20:36:07 CST",
  336. "ConditionTimestampMonotonic": "4109479",
  337. "Conflicts": "shutdown.target",
  338. "ControlPID": "0",
  339. "DefaultDependencies": "yes",
  340. "Delegate": "no",
  341. "Description": "Command Scheduler",
  342. "DevicePolicy": "auto",
  343. "EnvironmentFile": "/etc/sysconfig/crond (ignore_errors=no)",
  344. "ExecMainCode": "1",
  345. "ExecMainExitTimestamp": "日 2018-01-14 03:00:17 CST",
  346. "ExecMainExitTimestampMonotonic": "23054285827",
  347. "ExecMainPID": "698",
  348. "ExecMainStartTimestamp": "六 2018-01-13 20:36:07 CST",
  349. "ExecMainStartTimestampMonotonic": "4116176",
  350. "ExecMainStatus": "0",
  351. "ExecReload": "{ path=/bin/kill ; argv[]=/bin/kill -HUP $MAINPID ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }",
  352. "ExecStart": "{ path=/usr/sbin/crond ; argv[]=/usr/sbin/crond -n $CRONDARGS ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }",
  353. "FailureAction": "none",
  354. "FileDescriptorStoreMax": "0",
  355. "FragmentPath": "/usr/lib/systemd/system/crond.service",
  356. "GuessMainPID": "yes",
  357. "IOScheduling": "0",
  358. "Id": "crond.service",
  359. "IgnoreOnIsolate": "no",
  360. "IgnoreOnSnapshot": "no",
  361. "IgnoreSIGPIPE": "yes",
  362. "InactiveEnterTimestamp": "日 2018-01-14 03:00:17 CST",
  363. "InactiveEnterTimestampMonotonic": "23054285887",
  364. "InactiveExitTimestamp": "六 2018-01-13 20:36:07 CST",
  365. "InactiveExitTimestampMonotonic": "4116254",
  366. "JobTimeoutAction": "none",
  367. "JobTimeoutUSec": "0",
  368. "KillMode": "process",
  369. "KillSignal": "15",
  370. "LimitAS": "18446744073709551615",
  371. "LimitCORE": "18446744073709551615",
  372. "LimitCPU": "18446744073709551615",
  373. "LimitDATA": "18446744073709551615",
  374. "LimitFSIZE": "18446744073709551615",
  375. "LimitLOCKS": "18446744073709551615",
  376. "LimitMEMLOCK": "65536",
  377. "LimitMSGQUEUE": "819200",
  378. "LimitNICE": "0",
  379. "LimitNOFILE": "4096",
  380. "LimitNPROC": "1802",
  381. "LimitRSS": "18446744073709551615",
  382. "LimitRTPRIO": "0",
  383. "LimitRTTIME": "18446744073709551615",
  384. "LimitSIGPENDING": "1802",
  385. "LimitSTACK": "18446744073709551615",
  386. "LoadState": "loaded",
  387. "MainPID": "0",
  388. "MemoryAccounting": "no",
  389. "MemoryCurrent": "18446744073709551615",
  390. "MemoryLimit": "18446744073709551615",
  391. "MountFlags": "0",
  392. "Names": "crond.service",
  393. "NeedDaemonReload": "no",
  394. "Nice": "0",
  395. "NoNewPrivileges": "no",
  396. "NonBlocking": "no",
  397. "NotifyAccess": "none",
  398. "OOMScoreAdjust": "0",
  399. "OnFailureJobMode": "replace",
  400. "PermissionsStartOnly": "no",
  401. "PrivateDevices": "no",
  402. "PrivateNetwork": "no",
  403. "PrivateTmp": "no",
  404. "ProtectHome": "no",
  405. "ProtectSystem": "no",
  406. "RefuseManualStart": "no",
  407. "RefuseManualStop": "no",
  408. "RemainAfterExit": "no",
  409. "Requires": "basic.target",
  410. "Restart": "no",
  411. "RestartUSec": "100ms",
  412. "Result": "success",
  413. "RootDirectoryStartOnly": "no",
  414. "RuntimeDirectoryMode": "0755",
  415. "SameProcessGroup": "no",
  416. "SecureBits": "0",
  417. "SendSIGHUP": "no",
  418. "SendSIGKILL": "yes",
  419. "Slice": "system.slice",
  420. "StandardError": "inherit",
  421. "StandardInput": "null",
  422. "StandardOutput": "journal",
  423. "StartLimitAction": "none",
  424. "StartLimitBurst": "5",
  425. "StartLimitInterval": "10000000",
  426. "StartupBlockIOWeight": "18446744073709551615",
  427. "StartupCPUShares": "18446744073709551615",
  428. "StatusErrno": "0",
  429. "StopWhenUnneeded": "no",
  430. "SubState": "dead",
  431. "SyslogLevelPrefix": "yes",
  432. "SyslogPriority": "30",
  433. "SystemCallErrorNumber": "0",
  434. "TTYReset": "no",
  435. "TTYVHangup": "no",
  436. "TTYVTDisallocate": "no",
  437. "TasksAccounting": "no",
  438. "TasksCurrent": "18446744073709551615",
  439. "TasksMax": "18446744073709551615",
  440. "TimeoutStartUSec": "1min 30s",
  441. "TimeoutStopUSec": "1min 30s",
  442. "TimerSlackNSec": "50000",
  443. "Transient": "no",
  444. "Type": "simple",
  445. "UMask": "0022",
  446. "UnitFilePreset": "enabled",
  447. "UnitFileState": "enabled",
  448. "WantedBy": "multi-user.target",
  449. "Wants": "system.slice",
  450. "WatchdogTimestampMonotonic": "0",
  451. "WatchdogUSec": "0"
  452. }
  453. }
  454. 172.18.30.1 ' SUCCESS => {
  455. "changed": true,
  456. "name": "crond",
  457. "state": "started",
  458. "status": {
  459. "ActiveEnterTimestamp": "五 2018-01-12 09:18:48 CST",
  460. "ActiveEnterTimestampMonotonic": "3696327",
  461. "ActiveExitTimestamp": "六 2018-01-13 20:53:29 CST",
  462. "ActiveExitTimestampMonotonic": "126112891498",
  463. "ActiveState": "inactive",
  464. "After": "auditd.service systemd-user-sessions.service system.slice time-sync.target systemd-journald.socket basic.target",
  465. "AllowIsolate": "no",
  466. "AmbientCapabilities": "0",
  467. "AssertResult": "yes",
  468. "AssertTimestamp": "五 2018-01-12 09:18:48 CST",
  469. "AssertTimestampMonotonic": "3696000",
  470. "Before": "shutdown.target multi-user.target",
  471. "BlockIOAccounting": "no",
  472. "BlockIOWeight": "18446744073709551615",
  473. "CPUAccounting": "no",
  474. "CPUQuotaPerSecUSec": "infinity",
  475. "CPUSchedulingPolicy": "0",
  476. "CPUSchedulingPriority": "0",
  477. "CPUSchedulingResetOnFork": "no",
  478. "CPUShares": "18446744073709551615",
  479. "CanIsolate": "no",
  480. "CanReload": "yes",
  481. "CanStart": "yes",
  482. "CanStop": "yes",
  483. "CapabilityBoundingSet": "18446744073709551615",
  484. "ConditionResult": "yes",
  485. "ConditionTimestamp": "五 2018-01-12 09:18:48 CST",
  486. "ConditionTimestampMonotonic": "3696000",
  487. "Conflicts": "shutdown.target",
  488. "ControlPID": "0",
  489. "DefaultDependencies": "yes",
  490. "Delegate": "no",
  491. "Description": "Command Scheduler",
  492. "DevicePolicy": "auto",
  493. "EnvironmentFile": "/etc/sysconfig/crond (ignore_errors=no)",
  494. "ExecMainCode": "1",
  495. "ExecMainExitTimestamp": "六 2018-01-13 20:53:29 CST",
  496. "ExecMainExitTimestampMonotonic": "126112902263",
  497. "ExecMainPID": "700",
  498. "ExecMainStartTimestamp": "五 2018-01-12 09:18:48 CST",
  499. "ExecMainStartTimestampMonotonic": "3696282",
  500. "ExecMainStatus": "0",
  501. "ExecReload": "{ path=/bin/kill ; argv[]=/bin/kill -HUP $MAINPID ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }",
  502. "ExecStart": "{ path=/usr/sbin/crond ; argv[]=/usr/sbin/crond -n $CRONDARGS ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }",
  503. "FailureAction": "none",
  504. "FileDescriptorStoreMax": "0",
  505. "FragmentPath": "/usr/lib/systemd/system/crond.service",
  506. "GuessMainPID": "yes",
  507. "IOScheduling": "0",
  508. "Id": "crond.service",
  509. "IgnoreOnIsolate": "no",
  510. "IgnoreOnSnapshot": "no",
  511. "IgnoreSIGPIPE": "yes",
  512. "InactiveEnterTimestamp": "六 2018-01-13 20:53:29 CST",
  513. "InactiveEnterTimestampMonotonic": "126112902377",
  514. "InactiveExitTimestamp": "五 2018-01-12 09:18:48 CST",
  515. "InactiveExitTimestampMonotonic": "3696327",
  516. "JobTimeoutAction": "none",
  517. "JobTimeoutUSec": "0",
  518. "KillMode": "process",
  519. "KillSignal": "15",
  520. "LimitAS": "18446744073709551615",
  521. "LimitCORE": "18446744073709551615",
  522. "LimitCPU": "18446744073709551615",
  523. "LimitDATA": "18446744073709551615",
  524. "LimitFSIZE": "18446744073709551615",
  525. "LimitLOCKS": "18446744073709551615",
  526. "LimitMEMLOCK": "65536",
  527. "LimitMSGQUEUE": "819200",
  528. "LimitNICE": "0",
  529. "LimitNOFILE": "4096",
  530. "LimitNPROC": "1802",
  531. "LimitRSS": "18446744073709551615",
  532. "LimitRTPRIO": "0",
  533. "LimitRTTIME": "18446744073709551615",
  534. "LimitSIGPENDING": "1802",
  535. "LimitSTACK": "18446744073709551615",
  536. "LoadState": "loaded",
  537. "MainPID": "0",
  538. "MemoryAccounting": "no",
  539. "MemoryCurrent": "18446744073709551615",
  540. "MemoryLimit": "18446744073709551615",
  541. "MountFlags": "0",
  542. "Names": "crond.service",
  543. "NeedDaemonReload": "no",
  544. "Nice": "0",
  545. "NoNewPrivileges": "no",
  546. "NonBlocking": "no",
  547. "NotifyAccess": "none",
  548. "OOMScoreAdjust": "0",
  549. "OnFailureJobMode": "replace",
  550. "PermissionsStartOnly": "no",
  551. "PrivateDevices": "no",
  552. "PrivateNetwork": "no",
  553. "PrivateTmp": "no",
  554. "ProtectHome": "no",
  555. "ProtectSystem": "no",
  556. "RefuseManualStart": "no",
  557. "RefuseManualStop": "no",
  558. "RemainAfterExit": "no",
  559. "Requires": "basic.target",
  560. "Restart": "no",
  561. "RestartUSec": "100ms",
  562. "Result": "success",
  563. "RootDirectoryStartOnly": "no",
  564. "RuntimeDirectoryMode": "0755",
  565. "SameProcessGroup": "no",
  566. "SecureBits": "0",
  567. "SendSIGHUP": "no",
  568. "SendSIGKILL": "yes",
  569. "Slice": "system.slice",
  570. "StandardError": "inherit",
  571. "StandardInput": "null",
  572. "StandardOutput": "journal",
  573. "StartLimitAction": "none",
  574. "StartLimitBurst": "5",
  575. "StartLimitInterval": "10000000",
  576. "StartupBlockIOWeight": "18446744073709551615",
  577. "StartupCPUShares": "18446744073709551615",
  578. "StatusErrno": "0",
  579. "StopWhenUnneeded": "no",
  580. "SubState": "dead",
  581. "SyslogLevelPrefix": "yes",
  582. "SyslogPriority": "30",
  583. "SystemCallErrorNumber": "0",
  584. "TTYReset": "no",
  585. "TTYVHangup": "no",
  586. "TTYVTDisallocate": "no",
  587. "TasksAccounting": "no",
  588. "TasksCurrent": "18446744073709551615",
  589. "TasksMax": "18446744073709551615",
  590. "TimeoutStartUSec": "1min 30s",
  591. "TimeoutStopUSec": "1min 30s",
  592. "TimerSlackNSec": "50000",
  593. "Transient": "no",
  594. "Type": "simple",
  595. "UMask": "0022",
  596. "UnitFilePreset": "enabled",
  597. "UnitFileState": "enabled",
  598. "WantedBy": "multi-user.target",
  599. "Wants": "system.slice",
  600. "WatchdogTimestampMonotonic": "0",
  601. "WatchdogUSec": "0"
  602. }
  603. }

12)user管理用户

  1. 创建用户
  2. [root@ansible /]# ansible 'webserver' -m user -a 'name=nfs system=yes state=present'
  3. 172.18.30.2 ' SUCCESS => {
  4. "changed": true,
  5. "comment": "",
  6. "createhome": true,
  7. "group": 996,
  8. "home": "/home/nfs",
  9. "name": "nfs",
  10. "shell": "/bin/bash",
  11. "state": "present",
  12. "stderr": "useradd:警告:此主目录已经存在。\n不从 skel 目录里向其中复制任何文件。\n",
  13. "stderr_lines": [
  14. "useradd:警告:此主目录已经存在。",
  15. "不从 skel 目录里向其中复制任何文件。"
  16. ],
  17. "system": true,
  18. "uid": 998
  19. }
  20. 172.18.30.1 ' SUCCESS => {
  21. "changed": true,
  22. "comment": "",
  23. "createhome": true,
  24. "group": 996,
  25. "home": "/home/nfs",
  26. "name": "nfs",
  27. "shell": "/bin/bash",
  28. "state": "present",
  29. "stderr": "useradd:警告:此主目录已经存在。\n不从 skel 目录里向其中复制任何文件。\n",
  30. "stderr_lines": [
  31. "useradd:警告:此主目录已经存在。",
  32. "不从 skel 目录里向其中复制任何文件。"
  33. ],
  34. "system": true,
  35. "uid": 998
  36. }
  37. 删除用户
  38. [root@ansible /]# ansible 'webserver' -m user -a 'name=nfs system=yes state=absent'
  39. 172.18.30.1 ' SUCCESS => {
  40. "changed": true,
  41. "force": false,
  42. "name": "nfs",
  43. "remove": false,
  44. "state": "absent"
  45. }
  46. 172.18.30.2 ' SUCCESS => {
  47. "changed": true,
  48. "force": false,
  49. "name": "nfs",
  50. "remove": false,
  51. "state": "absent"
  52. }
  53. 注意,不会移除家目录
  54. 更多的参数请参考ansible-doc -s user

13)group管理组

  1. 添加组
  2. [root@ansible /]# ansible 'webserver' -m group -a 'name=nfs system=yes'
  3. 172.18.30.2 ' SUCCESS => {
  4. "changed": true,
  5. "gid": 996,
  6. "name": "nfs",
  7. "state": "present",
  8. "system": true
  9. }
  10. 172.18.30.1 ' SUCCESS => {
  11. "changed": true,
  12. "gid": 996,
  13. "name": "nfs",
  14. "state": "present",
  15. "system": true
  16. }
  17. 删除组
  18. [root@ansible /]# ansible 'webserver' -m group -a 'name=nfs state=absent'
  19. 172.18.30.2 ' SUCCESS => {
  20. "changed": true,
  21. "name": "nfs",
  22. "state": "absent"
  23. }
  24. 172.18.30.1 ' SUCCESS => {
  25. "changed": true,
  26. "name": "nfs",
  27. "state": "absent"
  28. }
文档更新时间: 2018-12-20 15:38   作者:张尚