Ansible常用模块

作者: 潘晓华Michael | 来源:发表于2019-07-22 14:41 被阅读5次
Ansible

git模块

简介

此模块用于checkout远程git仓库中的文件

使用要求(在执行模块的主机上)

git>=1.7.1 (命令行工具)

模块参数

名称 必选 默认值 备注
accept_hostkey no no 如果yes,请确保“-o StrictHostKeyChecking = no”作为ssh选项存在。
archive no 使用扩展名指定存档文件路径。 如果指定,则创建包含源树树结构的指定格式的存档文件。 允许的存档格式[“zip”,“tar.gz”,“tar”,“tgz”]
这将从本地目录克隆并执行git archive
bare no no 如果yes,则将创建存储库作为裸存储库,否则它将是具有工作空间的标准存储库。
clone no yes 如果no,即使它本地不存在,也不要克隆存储库
depth no clone的深度,最小值为1, git>=1.9.1才支持
dest yes 应该检出存储库的路径。 除非将clone设置为no,否则此参数是必需的。
executable no 要使用的git可执行文件的路径
force no no 如果yes,则将丢弃工作存储库中的任何已修改文件。
key_file no 私钥存放地址
recursive no yes 如果no,将使用--recursive选项克隆存储库,跳过子模块。
reference no 参考git clone --reference
refspec no no 添加要获取的其他refspec。 如果将版本设置为无法从任何分支或标记访问的SHA-1,则可能需要此选项来指定包含SHA-1的ref。 使用与'git fetch'命令相同的语法。 示例值可以是“refs / meta / config”。
remote no origin 远程仓库名
repo yes git仓库地址
separate_git_dir no 设置git仓库目录的存储
ssh_opts no ssh命令参数,覆盖默认的ssh参数
track_submodules no no 如果 yes,子模块将跟踪其主分支(或.gitmodules中指定的其他分支)上的最新提交。 如果no,则子模块将保留在主项目指定的修订版本中。 这相当于为git子模块更新指定了--remote标志。
umask no 在执行任何检出或任何其他存储库维护之前设置的umask。
update no yes 如果no,请不要从源存储库中检索新修订
verify_commit no no 如果yes,则在克隆或签出版本时验证GPG签名提交的签名。git>2.1.0
version no HEAD clone代码的版本号

示例

# Example git checkout from Ansible Playbooks
- git:
    repo: 'https://foosball.example.org/path/to/repo.git'
    dest: /srv/checkout
    version: release-0.22

# Example read-write git checkout from github
- git:
    repo: git@github.com:mylogin/hello.git
    dest: /home/mylogin/hello

# Example just ensuring the repo checkout exists
- git:
    repo: 'https://foosball.example.org/path/to/repo.git'
    dest: /srv/checkout
    update: no

# Example just get information about the repository whether or not it has
# already been cloned locally.
- git:
    repo: 'https://foosball.example.org/path/to/repo.git'
    dest: /srv/checkout
    clone: no
    update: no

# Example checkout a github repo and use refspec to fetch all pull requests
- git:
    repo: https://github.com/ansible/ansible-examples.git
    dest: /src/ansible-examples
    refspec: '+refs/pull/*:refs/heads/*'

# Example Create git archive from repo
- git:
    repo: https://github.com/ansible/ansible-examples.git
    dest: /src/ansible-examples
    archive: /tmp/ansible-examples.zip

# Example clone a repo with separate git directory
- git:
    repo: https://github.com/ansible/ansible-examples.git
    dest: /src/ansible-examples

expect模块

简介

  • expect模块用于在给的的节点上执行一个命令并响应提示。
  • 它不会通过shell处理命令,因此不支持像$HOME这样的变量和,以及<, >, |, ;&等都是无效的。也就是在command模块中无法使用管道符。

使用要求(在执行模块的主机上)

python >= 2.6
pexpect >= 3.3

模块参数

名称 必选 默认值 备注
chdir no 运行command命令前先cd到这个目录
command yes 命令模块执行命令运行
echo no no 是否回显你的回应字符串
responses yes 期望的字符串/正则表达式和字符串的映射来响应。 如果响应是一个列表,则连续的匹配将返回连续的响应。 列表功能是2.1中的新功能。
creates no 如果这个参数对应的文件存在,就不运行command
removes no 如果这个参数对应的文件不存在,就不运行command,与creates参数的作用相反
timeout no 30 以秒为单位等待预期时间

示例

  • 在远程主机上执行脚本
- name: Case insensitve password string match
  expect:
    command: passwd username
    responses:
      (?i)password: "MySekretPa$$word"

- name: Generic question with multiple different responses
  expect:
    command: /path/to/custom/command
    responses:
      Question:
        - response1
        - response2
        - response3

注意事项

  • 如果你想通过shell运行一个命令(比如你正在使用<,>,|等),你必须在命令中指定一个shell,比如/bin/bash -c "/path/to/something | grep else"
  • responses下关键是一个python正则表达式匹配,不区分大小写的搜索用前缀?i。
  • 默认情况下,如果多次遇到问题,则会重复其字符串响应。 如果连续问题匹配需要不同的响应,而不是字符串响应,请使用字符串列表作为响应。
  • expect模块设计用于简单场景,对于更复杂的需求,应该考虑在shellscript模块中使用expect代码

相关文章

网友评论

    本文标题:Ansible常用模块

    本文链接:https://www.haomeiwen.com/subject/fnmdzqtx.html