Ansible--常用模块使用

ansible命令解释

通过ansible命令执⾏的任务称为ad-hoc命令(任务),其实它是相对playbook⽽⾔的。通常,命令⾏⽤来实现ansible的批量管理功能,playbook⽤来实现批量⾃动化功能。
执行:ansible --help 查看具体的使用帮助
帮助参数详细说明:

----------------------------------------------------------------------------------------------------------------
[普通选项]
-a MODULE_ARGS
--args=MODULE_ARGS
传递参数给模块

--ask-vault-pass
询问vault的密码

-B SECONDS
--background=SECONDS
异步后台⽅式执⾏任务,并在指定的秒数后超时,超时会杀掉任务的进程。默认是同步,即保持长连接,它会等待
所有执⾏完毕(即阻塞模式)。但有时候是没必要这样的,⽐如某些命令的执⾏时间⽐ssh的超时时间还长。如果
不指定超时秒数,将以同步⽅式运⾏任务

-P POLL_INTERVAL
--poll=POLL_INTERVAL
异步模式下轮询任务的时间间隔,默认60秒

-C
--check
不对远程主机做一些改变,而是预测某些可能发生的改变(检查功能)

-e EXTRA_VARS
--extra-vars=EXTRA_VARS
配置额外的配置变量(key=value或者YAML/JSON格式)

-f FORKS
--forks=FORKS
指定并行处理的进程数量,默认5个

-h
--help
显示帮助信息

-i INVENTORY
--inventory-file=INVENTORY
指定inventory⽂件,多个⽂件使⽤逗号分隔。默认为/etc/ansible/hosts

-l SUBSET
--limit=SECONDS
使⽤额外的匹配模式来筛选⽬标主机列表。
此处的匹配模式是在已有匹配模式下进⾏的,所以是更严格的筛选。例如指定了主机组的情况下,使⽤-l选项从中只选⼀台主机进⾏控制

--list-hosts
不会执行任何操作,而是列出匹配到的主机列表

-m MODULE_NAME
--module-name=MODULE_NAME
指定要执行的模块名,默认的模块为"command"

--new-vault-password-file=NEW_VAULT_PASSWORD_FILE
new vault password f ile f or rekey

-o
--one-line
简化输出(一行输出模式)

--output-OUTPUT_FILE
output f ile name f or encrypt or decrypt; use - f or stdout

--syntax-check
检查playbook的语法,不会执行

-t TREE
--tree=TREE
记录输出到此⽬录中(测试时以每个host名如IP地址为⽂件名记录,结果记录到对应的⽂件中)。
此选项在ansible巨慢的时候(如瞬间应该返回的命令还需要10多秒才完成)有奇⽤,或者将ansible的结果重
定向到某个⽂件中也能解决,为什么如此,我也不明⽩(表⾯看来和输出⽅式有关系),多次亲测有效。

--vault-password-file=VAULT_PASSWORD_FILE
指定vault密码文件

-v
--verbose
输出详细信息,-vvv和-vvvv输出更详细的信息

--version
显示ansbile的版本

----------------------------------------------------------------------------------------------------------------
[以下是连接选项,⽤于控制谁以及如何连接主机]
-k
--ask-pass
询问连接时的密码

--private-key=KEY_FILE
--key-file=KEY_FILE
使用文件来认证SSH连接的过程

-u REMOTE_USER
--user=REMOTE_USER
使用指定的用户名进行连接

-c CINNECTION
--connection=CINNECTION
连接类型,默认为ssh。paramiko (SSH), ssh, winrm and local. local is mostly usef ul f or crontab or kickstarts.

-T TIMEOUT
--time=TIMEOUT
连接的超时时间,单位为秒,默认10秒

--ssh-common-args=SSH_COMMON_ARGS
指定传递给sftp/scp/ssh等⼯具的通⽤额外参数

--sftp-extra-args=SFTP_EXTRA_ARGS
指定只传递给sftp的额外参数,如-f

--scp-extra-args=SCP_EXTRA_ARGS
指定只传递给scp的额外参数,如-l

--ssh-extra-args=SSH_EXTRA_ARGS
指定只传递给ssh的额外参数,如-R

----------------------------------------------------------------------------------------------------------------
[以下是权限控制选项:控制在⽬标主机上以什么⾝份和权限运⾏任务]
-s
--sudo
为运行ansible命令的用户提升权限为sudo_user的权限,此命令已经报废,使用become代替

-u SUDO_USER
--sudo-user=SUDO_USER
期望的sudo_user,默认为root,已报废,使用become代替

-S
--su
使⽤su的⽅式执⾏操作,已废弃,使⽤become替代

-R SU_USER
--su-user=SU_USER
使⽤此user的su执⾏操作,默认为root,已废弃,使⽤become替代

-b
--become
使用become的方式升级权限

--become-method=BECOME_METHOD
指定提升权限的方式,可选以下⼏种:sudo/su/pbrun/pf exec/doas/dzdo/ksu/runas值

--become-user=BECOME_USER
要提升为哪个user的权限,默认为root

--ask-sudo-pass
询问sudo密码,已废弃,使⽤become替代

--ask-su-pass
询问su的密码,已废弃,使⽤become替代

-K
--ask-become-pass
询问become提升权限时的密码

----------------------------------------------------------------------------------------------------------------

常用模块

ping 模块

用途:尝试连接被控制端主机,成功返回pong(ping--pong是一对的)
使用方法:

[root@ansible ~]# ansible-doc -s ping
- name: Try to connect to host, verify a usable python and return `pong' on success
  ping:
      data:                  # Data to return for the `ping' return value. If this parameter is set to `crash', the module will cause an exception.

 示例:

[root@ansible ~]# ansible all -m ping
192.168.192.131 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
192.168.192.130 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
192.168.192.129 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
192.168.192.128 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
参数all代表被控制端的所有主机

command 模块

用途:默认ansible使⽤的模块是command,即可以执⾏⼀些shell命令
使用方法:

[root@ansible ~]# ansible-doc -s command
- name: Executes a command on a remote node
  command:
      argv:                  # 允许用户以列表和字符串的形式提供命令,不能同时使用,也不必须提供其中一种
      chdir:                 # 在执行命令之前,先cd到指定的目录下
      creates:               # 用于判断命令是否要执行,如果指定的文件存在(可以使用通配符)存在,则不执行
      free_form:             # 默认的选项,这里只是显示,实际上是没有的
      removes:               # 用于判断命令是否要执行,如果指定的文件存在(可以使用通配符)不存在,则不执行
      stdin:                 # 将命令的stdin直接设置为指定值
      warn:                  # 设置command的警告信息(在/etc/ansible/ansible.cfg中有配置项)

示例:

不加参数执行
[root@ansible ~]# ansible all -a "ls"
192.168.192.130 | CHANGED | rc=0 >>
anaconda-ks.cfg
haha.txt

192.168.192.129 | CHANGED | rc=0 >>
anaconda-ks.cfg
haha.txt
rc.local

chdir参数
[root@ansible ~]# ansible all -a "chdir=/etc/ssh ls"              
192.168.192.129 | CHANGED | rc=0 >>
moduli
ssh_config
sshd_config
ssh_host_ecdsa_key
ssh_host_ecdsa_key.pub
ssh_host_ed25519_key
ssh_host_ed25519_key.pub
ssh_host_rsa_key
ssh_host_rsa_key.pub

192.168.192.130 | CHANGED | rc=0 >>
moduli
ssh_config
sshd_config
ssh_host_ecdsa_key
ssh_host_ecdsa_key.pub
ssh_host_ed25519_key
ssh_host_ed25519_key.pub
ssh_host_rsa_key
ssh_host_rsa_key.pub

creates参数
[root@ansible ~]# ansible all -a "touch haha.txt creates=/root/haha.txt"         
192.168.192.129 | SUCCESS | rc=0 >>
skipped, since /root/haha.txt exists

192.168.192.130 | SUCCESS | rc=0 >>
skipped, since /root/haha.txt exists

removes参数
[root@ansible ~]# ansible all -a "touch haha1.txt removes=/root/haha1.txt"
192.168.192.130 | SUCCESS | rc=0 >>
skipped, since /root/haha1.txt does not exist

192.168.192.129 | SUCCESS | rc=0 >>
skipped, since /root/haha1.txt does not exist

温馨提示:
command不能解析变量(如$HOME)和某些操作符("<", ">", "|", ";"以及"&"),所以明确要使⽤这些不可解析的操作符时,使⽤shell模块来代替command

shell 模块

用途:shell和command的⽤法基本⼀样,实际上shell模块执⾏命令的⽅式是在远程使⽤/bin/sh来执⾏的,如/bin/sh ping
使用方法:

[root@ansible ~]# ansible-doc -s shell
- name: Execute commands in nodes.
  shell:
      chdir:                 # 在执行命令之前,先cd到指定的目录下
      creates:               # 用于判断命令是否要执行,如果指定的文件存在(可以使用通配符)存在,则不执行
      executable:            # 不再使⽤默认的/bin/sh解析并执⾏命令,⽽是使⽤此处指定的命令解析(例如使⽤expect解析expect脚本。必须为绝对路径)
      free_form:             # 默认的选项,这里只是显示,实际上是没有的
      removes:               # 用于判断命令是否要执行,如果指定的文件存在(可以使用通配符)不存在,则不执行
      stdin:                 # 将命令的stdin直接设置为指定值
      warn:                  # 设置command的警告信息(在/etc/ansible/ansible.cfg中有配置项)

示例:

不加参数执行
[root@ansible ~]# ansible all -m shell -a "ls"
192.168.192.129 | CHANGED | rc=0 >>
anaconda-ks.cfg
haha.txt
rc.local

192.168.192.130 | CHANGED | rc=0 >>
anaconda-ks.cfg
haha.txt

chdir参数
[root@ansible ~]# ansible all -m shell -a "chdir=/etc/ ls | grep ssh" 
192.168.192.129 | CHANGED | rc=0 >>
ssh

192.168.192.130 | CHANGED | rc=0 >>
ssh

creates参数
[root@ansible ~]# ansible all -a "touch haha.txt creates=/root/haha.txt"         
192.168.192.129 | SUCCESS | rc=0 >>
skipped, since /root/haha.txt exists

192.168.192.130 | SUCCESS | rc=0 >>
skipped, since /root/haha.txt exists

removes参数
[root@ansible ~]# ansible all -a "touch haha1.txt removes=/root/haha1.txt"
192.168.192.130 | SUCCESS | rc=0 >>
skipped, since /root/haha1.txt does not exist

192.168.192.129 | SUCCESS | rc=0 >>
skipped, since /root/haha1.txt does not exist

使用在command下不能用的操作符
[root@ansible ~]# ansible all -m shell -a "echo '2131' > haha.txt && cat haha.txt"
192.168.192.130 | CHANGED | rc=0 >>
2131

192.168.192.129 | CHANGED | rc=0 >>
2131

注意:
在ansible中使⽤shell或command模块⼀定要注意,它们默认不满⾜幂等性,很多操作会重复执⾏,但有些操作是不允许重复执⾏的。
例如mysql的初始化命令mysql_install_db,它只能在第⼀次配置的过程中初始化⼀次,其他任何时候如⾮需要则不允许执⾏。
解决方式:
这时候要实现幂等性,可以通过模块的creates和removes选项进⾏判断,但⽆论如何,在执⾏这两个模块的时候都需要考虑要执⾏的命令是否应该实现幂等性

script 模块

用途:script模块用于控制远程主机执行脚本,在执行脚本前,ansible会将本地脚本传输到远程主机,然后在执行,在执行脚本的时候,其采用的是远程主机上的shell环境
使用方法:

[root@ansible ~]# ansible-doc -s script
- name: Runs a local script on a remote node after transferring it
  script:
      chdir:                 # 在远程执⾏脚本前先切换到此⽬录下
      creates:               # 当此⽂件存在时,不执⾏脚本。可⽤于实现幂等性。
      decrypt:               # 此选项使用vault控制源文件的自动解密(对使用ansible-vault encrypt 文件名.yml 进行加密的文件解密)
      executable:            # 不再使⽤默认的/bin/sh解析并执⾏命令,⽽是使⽤此处指定的命令解析(例如使⽤expect解析expect脚本。必须为绝对路径)
      free_form:             # 本地待执⾏的脚本路径、选项、参数。之所以称为free_form,是因为它是脚本名+选项+参数。
      removes:               # 当此⽂件不存在时,不执⾏脚本。可⽤于实现幂等性。

示例:

新建一个脚本在ansible管理主机上
[root@ansible ~]# cat test.sh 
#!/bin/bash
echo "Test script module"

不加参数执行
[root@ansible ~]# ansible all -m script -a "test.sh"
192.168.192.130 | CHANGED => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to 192.168.192.130 closed.
", 
    "stderr_lines": [
        "Shared connection to 192.168.192.130 closed."
    ], 
    "stdout": "Test script module
", 
    "stdout_lines": [
        "Test script module"
    ]
}
192.168.192.129 | CHANGED => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to 192.168.192.129 closed.
", 
    "stderr_lines": [
        "Shared connection to 192.168.192.129 closed."
    ], 
    "stdout": "Test script module
", 
    "stdout_lines": [
        "Test script module"
    ]
}

chdir参数
[root@ansible ~]# ansible all -m script -a "chdir=/etc/ ./test.sh"
192.168.192.130 | CHANGED => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to 192.168.192.130 closed.
", 
    "stderr_lines": [
        "Shared connection to 192.168.192.130 closed."
    ], 
    "stdout": "Test script module
", 
    "stdout_lines": [
        "Test script module"
    ]
}
192.168.192.129 | CHANGED => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to 192.168.192.129 closed.
", 
    "stderr_lines": [
        "Shared connection to 192.168.192.129 closed."
    ], 
    "stdout": "Test script module
", 
    "stdout_lines": [
        "Test script module"
    ]
}

其他的参数的使用方法都和shell的参数一样,这里就不列举了!!!

copy 模块

用途:copy模块的作用就是拷贝文件,将 ansible 管理主机上的文件拷贝到远程主机中
使用方法:

[root@ansible ~]# ansible-doc -s copy
- name: Copies files to remote locations
  copy:
      backup:                # 拷贝的同时也创建⼀个包含时间戳信息的备份⽂件,默认为no,可以指定为backup=yes做文件备份
      content:               # 当用content代替src参数的时候,可以把content指定的内容直接写到一个文件
      decrypt:               # 此选项使用vault控制源文件的自动解密(对使用ansible-vault encrypt 文件名.yml 进行加密的文件解密)
      dest:                  # ⽬标路径,只能是绝对路径,如果拷贝的⽂件是⽬录,则⽬标路径必须也是⽬录
      directory_mode:        # 当对⽬录做递归拷贝时,设置了directory_mode将会使得只拷贝新建⽂件旧⽂件不会被拷贝。默认未设置.
      follow:                # 是否追踪到链接的源⽂件(follow=yes|on)
      force:                 # 设置为yes(默认)时,将覆盖远程同名⽂件。设置为no时,忽略同名⽂件的拷贝。
      group:                 # 指定文件拷贝到远程主机后的属组,但是远程主机上必须有对应的组,否则会报错
      local_follow:          # 是否遵循本地机器中的文件系统链接(local_follow=yes|on)
      mode:                  # 设置远程⽂件的权限。使⽤数值表⽰时不能省略第⼀位,如0644。也可以使⽤'u+rwx'或'u=rw,g=r,o=r'等⽅式设置
      owner:                 # 设置远程⽂件的所有者
      remote_src:            # 如果yes它会从目标机上搜索src文件(remote_src=yes|on)
      src:                   # 拷贝本地源⽂件到远程,可使⽤绝对路径或相对路径。如果路径是⽬录,且⽬录后加了斜杠"/",则只会拷贝⽬录中的内容到远程,如果⽬录后不加斜杠,则拷贝⽬录本⾝和⽬录内的内容到远程
      unsafe_writes:         # 是否以不安全的方式进行,可能导致数据损坏(unsafe_writes=yes|on)
      validate:              # 复制前是否检验需要复制目的地的路径

示例:

这里做一些常用选项的示例

1)不加参数执行(将本地主机上的/root/ansible/ansible-copy-module.sh拷贝到被控端主机/root/ansible/下)
[root@ansible ansible]# ansible all -m copy -a 'src=/root/ansible/ansible-copy-module.sh dest=/root/ansible'     
192.168.192.129 | CHANGED => {
    "changed": true, 
    "checksum": "9e4af9ed468fc3e4bcac3dc3022bb30a9cb85e01", 
    "dest": "/root/ansible", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "10b9ac730043f12b56be19b32746abff", 
    "mode": "0644", 
    "owner": "root", 
    "size": 28, 
    "src": "/root/.ansible/tmp/ansible-tmp-1545791911.48-195372218323444/source", 
    "state": "file", 
    "uid": 0
}
在管理主机上对ansible-copy-module.sh进行修改,然后在执行下面的拷贝前备份

2)backup参数:将本地主机上的/root/ansible/ansible-copy-module.sh拷贝到被控端主机/root/ansible/下(拷贝前备份,这里的备份是在被控制端进行的)
[root@ansible ansible]# ansible all -m copy -a 'src=/root/ansible/ansible-copy-module.sh dest=/root/ansible backup=yes'
192.168.192.129 | CHANGED => {
    "backup_file": "/root/ansible.4061.2018-12-26@10:40:11~", 
    "changed": true, 
    "checksum": "ac0d55a766b74d8e24a4b028fe5b162a1f026522", 
    "dest": "/root/ansible", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "f4cb7616d498638b01f81d164ebc64e4", 
    "mode": "0644", 
    "owner": "root", 
    "size": 42, 
    "src": "/root/.ansible/tmp/ansible-tmp-1545792010.33-171875446599908/source", 
    "state": "file", 
    "uid": 0
}
在上面可以看到backup_file这个参数,后面就是备份的路径(在被控制端主机上查看)

3)owner、group、mode参数
拷贝文件对文件权限和属主属组进行改变
[root@ansible ansible]# ansible all -m copy -a 'src=/root/ansible/ansible-copy-module.sh dest=/root/ansible/ owner=sshd group=sshd mode=0644'
192.168.192.129 | CHANGED => {
    "changed": true, 
    "checksum": "ac0d55a766b74d8e24a4b028fe5b162a1f026522", 
    "dest": "/root/ansible/ansible-copy-module.sh", 
    "gid": 74, 
    "group": "sshd", 
    "md5sum": "f4cb7616d498638b01f81d164ebc64e4", 
    "mode": "0644", 
    "owner": "sshd", 
    "size": 42, 
    "src": "/root/.ansible/tmp/ansible-tmp-1545792376.05-245560382140658/source", 
    "state": "file", 
    "uid": 74
}
查看权限
[root@ansible ansible]# ansible all -m shell -a "ls -l /root/ansible/ansible-copy-module.sh"
192.168.192.129 | CHANGED | rc=0 >>
-rw-r--r-- 1 sshd sshd 42 Dec 26 10:46 /root/ansible/ansible-copy-module.sh

4)content参数
将内容直接写到被控端指定的文件中
[root@ansible ansible]# ansible all -m copy -a "content='my name is zhujingzhi' dest=/root/ansible/my.txt"
192.168.192.129 | CHANGED => {
    "changed": true, 
    "checksum": "4f138d7333fe41ecc4cd8bb08a55c92b9952a65f", 
    "dest": "/root/ansible/my.txt", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "9a6e5e1b3273e1ba2dbec06cc4fd8a0c", 
    "mode": "0644", 
    "owner": "root", 
    "size": 21, 
    "src": "/root/.ansible/tmp/ansible-tmp-1545792591.83-125843029198237/source", 
    "state": "file", 
    "uid": 0
}
查看文件
[root@ansible ansible]# ansible all -m shell -a "cat /root/ansible/my.txt"
192.168.192.129 | CHANGED | rc=0 >>
my name is zhujingzhi!

5)force参数
是否忽略远程同名文件(默认yes)
[root@ansible ansible]# ansible all -m copy -a "src=/root/ansible/my.txt dest=/root/ansible/my.txt force=no"        
192.168.192.129 | SUCCESS => {
    "changed": false, 
    "dest": "/root/ansible/my.txt", 
    "src": "/root/ansible/my.txt"
}

template 模块

用途:template模块⽤法和copy模块⽤法基本⼀致,它主要⽤于复制配置⽂件
使用方法:

[root@ansible ~]# ansible-doc -s template
- name: Templates a file out to a remote server
  template:
      backup:                # 拷贝的同时也创建⼀个包含时间戳信息的备份⽂件,默认为no
      block_end_string:      # 标记块结束的字符串.
      block_start_string:    # 标记块开头的字符串.
      dest:                  # 远程节点上的绝对路径,用于放置template文件
      follow:                # 是否追踪到链接的源⽂件(follow=yes|on)
      force:                 # 设置为yes (默认)时,将覆盖远程同名⽂件。设置为no时,忽略同名⽂件的拷贝
      group:                 # 设置远程⽂件的所属组
      lstrip_blocks:         # 如果将其设置为真正的前导空格,那么制表符将从行开始剥离到块。将此选项设置为True需要Jinja2版本>=2.7
      mode:                  # 设置远程⽂件的权限。使⽤数值表⽰时不能省略第⼀位,如0644。也可以使⽤'u+rwx' or 'u=rw,g=r,o=r'等⽅式设置
      newline_sequence:      # 指定用于模板文件的换行序列.
      output_encoding:       # 改变dest指定模板文件的编码,默认为utf8.
      owner:                 # 设置远程⽂件的所有者
      src:                   # ansible控制器上Jinja2格式的模板所在位置,可以是相对或绝对路径.
      trim_blocks:           # 如果将此设置为True,则删除块后的第一个换行(块,而不是变量标记!).
      unsafe_writes:         # 是否以不安全的方式进行,可能导致数据损坏(unsafe_writes=yes|on)
      validate:              # 在复制到⽬标主机后但放到⽬标位置之前,执⾏此选项指定的命令,⼀般⽤于检查配置⽂件语法,语法正确则保存到⽬标位置,如果要引⽤⽬标⽂件名,则使⽤%s
      variable_end_string:   # 打印语句结尾的字符串.
      variable_start_string: # 打印语句开头的字符串

 示例:

[root@ansible ~]# ansible all -m template -a "src=/etc/nginx/nginx.conf dest=/etc/nginx/nginx.conf mode=0770 owner=root group=root backup=yes validate='nginx -t -c %s'"
192.168.192.129 | CHANGED => {
    "backup_file": "/etc/nginx/nginx.conf.6179.2018-12-26@11:32:00~", 
    "changed": true, 
    "checksum": "ccc2942c816b4caae696e39dd0b079f97df6cd1a", 
    "dest": "/etc/nginx/nginx.conf", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "96f714838028dea291bcd59b1447e46f", 
    "mode": "0770", 
    "owner": "root", 
    "size": 2468, 
    "src": "/root/.ansible/tmp/ansible-tmp-1545795118.58-264966235377818/source", 
    "state": "file", 
    "uid": 0
}
上的示例我测试的是nginx的配置文件,所以要提前安装nginx
[root@ansible ~]# ansible all -m shell -a "yum -y install epel-release && yum -y install nginx"

fetch 模块

用途:从被控远端机器上拉取文件(和COPY模块整好相反)
使用方法:

使用方法:
[root@ansible ~]# ansible-doc -s fetch
- name: Fetches a file from remote nodes
  fetch:
      dest:                  # 本地存储拉取⽂件的⽬录。例如dest=/data,src=/etc/fstab,远程主机名host.exp.com,则保存的路径为/data/host.exp.com/etc/fstab。
      fail_on_missing:       # 当设置为yes时,如果拉取的源⽂件不存在,则此任务失败。默认为no.
      flat:                  # 改变拉取后的路径存储⽅式。如果设置为yes,且当dest以"/"结尾时,将直接把源⽂件的basename存储在dest下。显然,应该考虑多个主机拉取时的⽂件覆盖情况。
      src:                   # 远程主机上的源⽂件。只能是⽂件,不⽀持⽬录。在未来的版本中可能会⽀持⽬录递归拉取.
      validate_checksum:     # fetch到⽂件后,检查其md5和源⽂件是否相同

 示例:

1) src、dest参数
拉取后的存储路径:/root/ansible/192.168.192.129/root/ansible/fetch.sh
[root@ansible ~]# ansible all -m fetch -a "src=/root/ansible/fetch.sh dest=/root/ansible/"     
192.168.192.129 | CHANGED => {
    "changed": true, 
    "checksum": "1d4eef5c77eacf079216ac851ebdde39c7c735ce", 
    "dest": "/root/ansible/192.168.192.129/root/ansible/fetch.sh", 
    "md5sum": "2442ddbfcb138987feedad26be9eccd8", 
    "remote_checksum": "1d4eef5c77eacf079216ac851ebdde39c7c735ce", 
    "remote_md5sum": null
}
2) flat参数
拉取后的存储路径:/root/ansible/fetch.sh
[root@ansible ~]# ansible all -m fetch -a "src=/root/ansible/fetch.sh dest=/root/ansible/ flat=yes"
192.168.192.129 | CHANGED => {
    "changed": true, 
    "checksum": "1d4eef5c77eacf079216ac851ebdde39c7c735ce", 
    "dest": "/root/ansible/fetch.sh", 
    "md5sum": "2442ddbfcb138987feedad26be9eccd8", 
    "remote_checksum": "1d4eef5c77eacf079216ac851ebdde39c7c735ce", 
    "remote_md5sum": null
}

3) 使用变量的方式按照变量值存储
拉取后的存储路径:/root/ansible/192.168.192.129/fetch.sh
[root@ansible ~]# ansible all -m fetch -a "src=/root/ansible/fetch.sh dest=/root/ansible/{{inventory_hostname}}/ flat=yes"
192.168.192.129 | CHANGED => {
    "changed": true, 
    "checksum": "1d4eef5c77eacf079216ac851ebdde39c7c735ce", 
    "dest": "/root/ansible/192.168.192.129/fetch.sh", 
    "md5sum": "2442ddbfcb138987feedad26be9eccd8", 
    "remote_checksum": "1d4eef5c77eacf079216ac851ebdde39c7c735ce", 
    "remote_md5sum": null
}
{{inventory_hostname}}表示:192.168.192.129

file 模块

用途:管理文件、目录的属性,也可以创建文件或者目录
使用方法:

在file模块中state参数是十分重要的

[root@ansible ~]# ansible-doc -s file
- name: Sets attributes of files
  file:
      follow:                       # 是否遵循目的机器中的文件系统链接(可选值为:yes|on)
      force:                        # 当state=link的时候,可配合此参数强制创建链接文件,当force=yes时,表示强制创建链接文件
                                    # 不过强制创建链接文件分为三种情况。情况一:当要创建的链接文件指向的源文件并不存在时,使用此参数,可以先强制创建出链接文件。
                                    # 情况二:当要创建链接文件的目录中已经存在与链接文件同名的文件时,将force设置为yes,会将同名文件覆盖为链接文件,相当于删除同名文件,创建链接文件。
                                    # 情况三:当要创建链接文件的目录中已经存在与链接文件同名的文件,并且链接文件指向的源文件也不存在,这时会强制替换同名文件为链接文件
      group:                        # 设置远程⽂件的所属组
      mode:                         # 设置远程⽂件的权限。使⽤数值表⽰时不能省略第⼀位,如0644。也可以使⽤
      owner:                        # 设置远程⽂件的所有者
      path:                         # 必须的参数,用于指定要操作的文件或者目录
      recurse:                      # 当要操作的文件为目录,将recurse设置为yes,可以递归的修改目录中的文件属性
      src:                          # 当state设置为link或者hard时,表示我们想要创建一个软链接或者硬链接,所以,我们必须指明软链接或硬链链接的哪个文件,通过src参数即可指定链接源
      state:                        # 此参数非常灵活,其对应的值需要根据情况设定。比如,我们想要在远程主机上创建/testdir/a/b目录,那么则需要设置path=/testdir/a/b,
                                    # 但是,我们无法从”/testdir/a/b“这个路径看出b是一个文件还是一个目录,ansible也同样无法单单从一个字符串就知道你要创建文件还是目录,所以,我们需要通过state参数进行说明
                                    # state=directory:表示创建目录,如果path指定的不存在则被创建
                                    # state=touch:创建文件
                                    # state=link:创建软链接文件
                                    # state=hard:创建硬链接文件
                                    # state=absent:删除文件(删除时不用区分目标是文件、目录、还是链接)
      unsafe_writes:                # 是否以不安全的方式进行,可能导致数据损坏(unsafe_writes=yes|on)

 示例:

1)state=directory参数(创建目录)
[root@ansible ~]# ansible all -m file -a "path=/root/file state=directory owner=root group=root mode=0777"   
192.168.192.129 | CHANGED => {
    "changed": true, 
    "gid": 0, 
    "group": "root", 
    "mode": "0777", 
    "owner": "root", 
    "path": "/root/file", 
    "size": 6, 
    "state": "directory", 
    "uid": 0
}
查看结果(很明显创建了一个file的目录)
[root@ansible ~]# ansible all -m shell -a "ls -l /root/ | grep file"
192.168.192.129 | CHANGED | rc=0 >>
drwxrwxrwx  2 root root    6 Dec 26 14:33 file

2)state=touch参数(创建文件)
[root@ansible ~]# ansible all -m file -a "path=/root/file/file.txt state=touch owner=root group=root mode=0655"
192.168.192.129 | CHANGED => {
    "changed": true, 
    "dest": "/root/file/file.txt", 
    "gid": 0, 
    "group": "root", 
    "mode": "0655", 
    "owner": "root", 
    "size": 0, 
    "state": "file", 
    "uid": 0
}
查看结果(很明显创建了一个file的文件)
[root@ansible ~]# ansible all -m shell -a "ls -l /root/file/file.txt"
192.168.192.129 | CHANGED | rc=0 >>
-rw-r-xr-x 1 root root 0 Dec 26 14:36 /root/file/file.txt

3)state=link(创建软链接文件)
[root@ansible ~]# ansible all -m file -a "path=/root/file/file.txt src=/root/ansible/my.txt state=link owner=root group=root mode=0655" 
192.168.192.129 | CHANGED => {
    "changed": true, 
    "dest": "/root/file/file.txt", 
    "gid": 0, 
    "group": "root", 
    "mode": "0777", 
    "owner": "root", 
    "size": 20, 
    "src": "/root/ansible/my.txt", 
    "state": "link", 
    "uid": 0
}
查看结果(已经发现/root/file/file.txt已经链接到/root/ansible/my.txt)
[root@ansible ~]# ansible all -m shell -a "ls -l /root/file/"    
192.168.192.129 | CHANGED | rc=0 >>
total 0
lrwxrwxrwx 1 root root 20 Dec 26 14:46 file.txt -> /root/ansible/my.txt

温馨提示:这里需要注意的是src指定的链接文件要存在,如果不存在也想先创建的话要加上force=yes参数

4)state=hard(创建硬链接文件)
[root@ansible ~]# ansible all -m file -a "path=/root/file/file1.txt src=/root/haha.txt state=hard owner=root group=root mode=0655" 
192.168.192.129 | CHANGED => {
    "changed": true, 
    "dest": "/root/file/file1.txt", 
    "gid": 0, 
    "group": "root", 
    "mode": "0655", 
    "owner": "root", 
    "size": 5, 
    "src": "/root/haha.txt", 
    "state": "hard", 
    "uid": 0
}
查看结果(查看两个文件的内容是否一样)
[root@ansible ~]# ansible all -m shell -a "cat /root/file/file1.txt && cat /root/haha.txt"
192.168.192.129 | CHANGED | rc=0 >>
2131
2131

5)state=absent(删除文件)
[root@ansible ~]# ansible all -m file -a "path=/root/file/file1.txt state=absent"
192.168.192.129 | CHANGED => {
    "changed": true, 
    "path": "/root/file/file1.txt", 
    "state": "absent"
}
查看结果(/root/file/file1.txt已经被删除)
[root@ansible ~]# ansible all -m shell -a "ls -l /root/file"
192.168.192.129 | CHANGED | rc=0 >>
total 0
lrwxrwxrwx 1 root root 20 Dec 26 14:46 file.txt -> /root/ansible/my.txt

6)force参数
在创建软链接时src指定的文件不存在,可以添加force=yes参数,先创建软链接
/root/ansible/file.txt 不存在
[root@ansible ~]# ansible all -m file -a "path=/root/file/file1.txt src=/root/ansible/file.txt state=link owner=root group=root mode=0655 force=yes"
 [WARNING]: Cannot set fs attributes on a non-existent symlink target. follow should be set to False to avoid this.
 # 这个警告可以使用follow=no参数取消掉

192.168.192.129 | CHANGED => {
    "changed": true, 
    "dest": "/root/file/file1.txt", 
    "src": "/root/ansible/file.txt", 
    "state": "absent"
}
查看结果(虽然/root/ansible/file.txt文件不存在,但是软链接也是创建成功的)
[root@ansible ~]# ansible all -m shell -a "ls -l /root/file/"         
192.168.192.129 | CHANGED | rc=0 >>
total 0
lrwxrwxrwx 1 root root 22 Dec 26 14:59 file1.txt -> /root/ansible/file.txt

rsync 模块

在ansible中rsync模块的名字叫synchronize

用途:synchronize模块⽤于实现rsync的简单版常⽤功能,它⽆法实现完整版的rsync,毕竟rsync功能太多太细致。如果要使⽤rsync,还是应该使⽤command或shell模块来调⽤rsync命令
使用方法:

使用方法:
[root@ansible ~]# ansible-doc -s synchronize
- name: A wrapper around rsync to make common tasks in your playbooks quick and easy.
  synchronize:
      archive:               # 等价于rsync的"-a"选项,即使⽤归档模式。它等价于rsync的"-rtopgDl"选项。值为yes/no.
      checksum:              # 是否对文件进行校验(在archive(归档)开启的时候checksum也是开启的).
      compress:              # 是否开启压缩,默认是开启的.
      copy_links:            # 同步的时候是否复制符号链接.
      delete:                # 删除源中没有但是目标存在的文件,使两边的数据内容一致,已推送为主需要设置参数recursive=yes结合使用.
      dest:                  # 目标文件及目录
      dest_port:             # 目标主机上的ssh的端口
      dirs:                  # 不使用递归的方式传送目录
      existing_only:         # receiver(接收端)没有的文件不同步,但仍会传输,只是临时文件重组后不重命名而已.
      group:                 # 保留所属组属性
      link_dest:             # 在rsync期间向硬链接添加目标
      links:                 # 拷贝链接文件自身
      mode:                  # 指定推(push)还是拉(pull)的传输模式
      owner:                 # 保留所有者属性
      partial:               # 等价于'--partial'选项,默认rsync在传输中断时会删除传输了一半的文件,指定该选项将保留这个部分不完整的文件,使得下次传输时可以直接从未完成的数据块开始传输.
      perms:                 # 保留权限属性.
      private_key:           # 指定基于ssh的rsync连接的私钥 (例如 `~/.ssh/id_rsa')
      recursive:             # 递归到目录中的文件.
      rsync_opts:            # 指定额外的rsync选项,使用数组的方式传递这些选项.
      rsync_path:            # 等价于'--rsync-path'选项,目的是启动远程rsync,例如可以指定[--rsync-path=rsync],甚至[--rsync-path=cd /tmp/c && rsync],当不指定rsync的路径时,默认是/usr/bin/rsync.
      rsync_timeout:         # 指定rsync在多久时间内没有数据传输就超时退出.
      set_remote_user:       # 主要用于/etc/ansible/hosts中定义或默认使用的用户与rsync使用的用户不同的情况.
      src:                   # (必选)指定待传输的源⽂件。可以是相对路径,也可以是绝对路径.
      times:                 # 保留mtime属性
      use_ssh_args:          # 使用ansible.cfg中配置的ssh_args
      verify_host:           # 对目标主机进行ssh的host key验证

 示例:

温馨提示:在使用synchronize模块的时候需要在管理机和被控制端的远程服务器上安装rsync,保证在管理机和被控制端的远程服务器上有rsync的命令
安装rsync:yum -y install rsync
1) 简简单单(指定src和dest)
[root@ansible ~]# ansible all -m synchronize -a "src=/root/rsyncfile/ dest=/root/rsyncfile/"
192.168.192.129 | CHANGED => {
    "changed": true, 
    "cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh=/usr/bin/ssh -S none -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null --out-format=<<CHANGED>>%i %n%L /root/rsyncfile/ 192.168.192.129:/root/rsyncfile/", 
    "msg": "cd+++++++++ ./
<f+++++++++ 123
<f+++++++++ 313
<f+++++++++ 31323
", 
    "rc": 0, 
    "stdout_lines": [
        "cd+++++++++ ./", 
        "<f+++++++++ 123", 
        "<f+++++++++ 313", 
        "<f+++++++++ 31323"
    ]
}
查看结果
[root@ansible ~]# ansible all -m shell -a "ls -l /root/rsyncfile"   
192.168.192.129 | CHANGED | rc=0 >>
total 0
-rw-r--r-- 1 root root 0 Dec 26 15:55 123
-rw-r--r-- 1 root root 0 Dec 26 15:55 313
-rw-r--r-- 1 root root 0 Dec 26 15:55 31323

2) 把本地的/root/rsyncfile目录以及里面除了.txt结尾的文件同步到远程服务器的/root目录里面,并且同步/root/rsyncfile目录以及文件的属性,还要删除本地没有但远程主机有的文件
/root/rsyncfile目录下的内容
[root@ansible ~]# ll rsyncfile/
total 0
-rw-r--r-- 1 sshd sshd 0 Dec 26 15:55 123
-rw-r--r-- 1 sshd sshd 0 Dec 26 16:02 1.txt
-rw-r--r-- 1 sshd sshd 0 Dec 26 16:02 2.txx
-rw-r--r-- 1 sshd sshd 0 Dec 26 15:55 313
-rw-r--r-- 1 sshd sshd 0 Dec 26 15:55 31323
-rw-r--r-- 1 sshd sshd 0 Dec 26 16:02 3.txt
-rw-r--r-- 1 sshd sshd 0 Dec 26 16:02 4.py

[root@ansible ~]# ansible all -m synchronize -a "src=/root/rsyncfile dest=/root compress=yes delete=yes archive=yes rsync_opts=--exclude=*.txt"  
192.168.192.129 | CHANGED => {
    "changed": true, 
    "cmd": "/usr/bin/rsync --delay-updates -F --compress --delete-after --archive --rsh=/usr/bin/ssh -S none -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null --exclude=*.txt --out-format=<<CHANGED>>%i %n%L /root/rsyncfile 192.168.192.129:/root", 
    "msg": "cd+++++++++ rsyncfile/
<f+++++++++ rsyncfile/123
<f+++++++++ rsyncfile/2.txx
<f+++++++++ rsyncfile/313
<f+++++++++ rsyncfile/31323
<f+++++++++ rsyncfile/4.py
", 
    "rc": 0, 
    "stdout_lines": [
        "cd+++++++++ rsyncfile/", 
        "<f+++++++++ rsyncfile/123", 
        "<f+++++++++ rsyncfile/2.txx", 
        "<f+++++++++ rsyncfile/313", 
        "<f+++++++++ rsyncfile/31323", 
        "<f+++++++++ rsyncfile/4.py"
    ]
}

查看结果(可以看到同步过去的文件属性都和源文件相同,并且没有同步源文件里面以.txt结尾的两个文件)
[root@ansible ~]# ansible all -m shell -a "ls -l /root/rsyncfile"
192.168.192.129 | CHANGED | rc=0 >>
total 0
-rw-r--r-- 1 sshd sshd 0 Dec 26 15:55 123
-rw-r--r-- 1 sshd sshd 0 Dec 26 16:02 2.txx
-rw-r--r-- 1 sshd sshd 0 Dec 26 15:55 313
-rw-r--r-- 1 sshd sshd 0 Dec 26 15:55 31323
-rw-r--r-- 1 sshd sshd 0 Dec 26 16:02 4.py

hostname 模块

用途:管理配置主机名
使用方法:

[root@ansible ~]# ansible-doc -s hostname
- name: Manage hostname
  hostname:
      name:                  # 必选,主机名称

示例:

温馨提示:使用这个模块存在一个问题就是一改主机名紧跟着应该是在这个清单里面的机器都会被改掉导致主机名完全相同
是有解决方法的,使用变量的方式进行更改

先针对一台主机进行修改主机名,了解其用法
[root@ansible ~]# ansible 192.168.192.129 -m hostname -a "name=ansible_node1"          
192.168.192.129 | CHANGED => {
    "ansible_facts": {
        "ansible_domain": "", 
        "ansible_fqdn": "ansible_node1", 
        "ansible_hostname": "ansible_node1", 
        "ansible_nodename": "ansible_node1"
    }, 
    "changed": true, 
    "name": "ansible_node1"
}
查看结果(发现主机名已经修改)
[root@ansible ~]# ansible 192.168.192.129 -m shell -a "hostname"
192.168.192.129 | CHANGED | rc=0 >>
ansible_node1

使用hostname模块修改主机名是直接生效的并且是永久生效

yum 模块

用途:使用yum包管理器管理包
使用方法:

[root@ansible ~]# ansible-doc -s yum
- name: Manages packages with the `yum' package manager
  yum:
      allow_downgrade:       # 是否允许给现有包的版本进行降级
      autoremove:            # 卸载包并且删除其所有的依赖包,如果这个依赖包被其他包所依赖则不会删除(authorremove=yes)
      bugfix:                # 如果bugfix=yes和state=latest则仅安装已标记为与bug修复相关的更新
      conf_file:             # 指定yum.repo配置文件.
      disable_excludes:      # 禁用YUM配置文件中定义的排除(yum.repo文件中的某个块). 
                             # disable_excludes=all 禁用所有排除
                             # disable_excludes=main 禁用yum.conf中的[main]中定义的排除
                             # disable_excludes=repoid 禁用未给定repo id定义的排除
      disable_gpg_check:     # 安装包时禁止gpgcheck,仅在state=present或者latest时生效.
      disable_plugin:        # 禁用yum的Loaded plugins(使用yum repolist all | less 查看所有插件)
      disablerepo:           # 禁止指定的repo源,多个repo源使用逗号分隔
      download_only:         # 只下载指定的安装包,不进行安装.
      enable_plugin:         # 开启yum的Loaded plugins(使用yum repolist all | less 查看所有插件).
      enablerepo:            # 明确使用那个repo源
      exclude:               # 排除那些包不安装,仅在state=present或者latest时生效
      installroot:           # 指定yum安装包的用户,用此用户安装的只允许root和指定用户使用
      list:                  # 类似于yum list.
      name:                  # (必选参数)指定包名,可以指定版本号,多个包可以使用逗号分隔
      releasever:            # Specifies an alternative release from which all packages will be installed.
      security:              # 如果设置为yes和state=latest,则只安装标记为与安全相关的更新
      skip_broken:           # 跳过具有损坏的依赖包
      state:                 # 用于指定软件包的状态 ,默认值为present,表示确保软件包已经安装
                             # state=present安装状态(默认值)
                             # state=installed安装状态
                             # state=latest安装状态(安装最新的版本)
                             # state=absent卸载状态
                             # state=removed卸载状态
      update_cache:          # 强制yum检查缓存是否过期,并在需要时重新下载.仅在state=present或者latest时生效.
      update_only:           # 使用最新软件包时,只更新已安装的软件包。不要安装软件包.仅在state=present或者latest时生效
      validate_certs:        # 这仅适用于使用https url作为rpm的源的情况。例如,用于localinstall。如果设置为no,则不会验证SSL证书

示例:

使用nginx和telnet为例(对应 yum 源未开启 gpg 验证,所以需要设置 disable_gpg_check=yes)
这里输出的东西太多了结果就不放到博客了(结果绿色和黄色都是执行成功了,红色为错误)
1) state=present(安装包)
[root@ansible ~]# ansible all -m yum -a "name=nginx disable_gpg_check=yes state=present"

2) state=installed(安装包)
[root@ansible ~]# ansible all -m yum -a "name=nginx disable_gpg_check=yes state=installed"

3) state=latest(安装最新的版本)
[root@ansible ~]# ansible all -m yum -a "name=nginx disable_gpg_check=yes state=latest"

4) state=absent(卸载包)
[root@ansible ~]# ansible all -m yum -a "name=nginx disable_gpg_check=yes state=absent"

5) state=removed(卸载包)
[root@ansible ~]# ansible all -m yum -a "name=nginx disable_gpg_check=yes state=removed"

6) enablerepo(明确使用那个repo源)
[root@ansible ~]# ansible all -m yum -a "name=telnet disable_gpg_check=yes enablerepo=local"

7) disablerepo(禁止指定的repo源,多个repo源使用逗号分隔)
[root@ansible ~]# ansible all -m yum -a "name=telnet disable_gpg_check=yes disablerepo=local"

更多模块

更多模块请点击:https://www.cnblogs.com/brianzhu/p/10175477.html

原文地址:https://www.cnblogs.com/brianzhu/p/10174130.html