ansible 学习笔记

ansible 学习笔记

大家好,又见面了,我是全栈君。

安装

安装epel源:

rpm -Uvh http://mirrors.kernel.org/fedora-epel/6/i386/epel-release-6-8.noarch.rpm
yum install -y ansible

ansible命令参数

  -a 'Arguments', --args='Arguments' 命令行参数
  -m NAME, --module-name=NAME 执行模块的名字,默认使用 command 模块,所以如果是只执行单一命令可以不用 -m参数
  -i PATH, --inventory=PATH 指定库存主机文件的路径,默认为/etc/ansible/hosts.
  -u Username, --user=Username 执行用户,使用这个远程用户名而不是当前用户
  -U --sud-user=SUDO_User  sudo到哪个用户,默认为 root
  -k --ask-pass  登录密码,提示输入SSH密码而不是假设基于密钥的验证
  -K --ask-sudo-pass 提示密码使用sudo
  -s --sudo sudo运行
  -S --su 用 su 命令
  -l  --list 显示所支持的所有模块
  -s --snippet 指定模块显示剧本片段
  -f  --forks=NUM 并行任务数。NUM被指定为一个整数,默认是5。 #ansible testhosts -a "/sbin/reboot" -f 10 重启testhosts组的所有机器,每次重启10台
  --private-key=PRIVATE_KEY_FILE 私钥路径,使用这个文件来验证连接
  -v --verbose 详细信息
  all  针对hosts 定义的所有主机执行
  -M MODULE_PATH, --module-path=MODULE_PATH 要执行的模块的路径,默认为/usr/share/ansible/
  --list-hosts 只打印有哪些主机会执行这个 playbook 文件,不是实际执行该 playbook 文件
  -o --one-line 压缩输出,摘要输出.尝试一切都在一行上输出。
  -t Directory, --tree=Directory 将内容保存在该输出目录,结果保存在一个文件中在每台主机上。
  -B 后台运行超时时间
  -P 调查后台程序时间
  -T Seconds, --timeout=Seconds 时间,单位秒s
  -P NUM, --poll=NUM 调查背景工作每隔数秒。需要- b
  -c Connection, --connection=Connection  连接类型使用。可能的选项是paramiko(SSH),SSH和地方。当地主要是用于crontab或启动。
  --tags=TAGS 只执行指定标签的任务    例子:ansible-playbook test.yml --tags=copy  只执行标签为copy的那个任务
  --list-hosts 只打印有哪些主机会执行这个 playbook 文件,不是实际执行该 playbook 文件
  --list-tasks 列出所有将被执行的任务
  -C, --check 只是测试一下会改变什么内容,不会真正去执行;相反,试图预测一些可能发生的变化
  --syntax-check 执行语法检查的剧本,但不执行它
  -l SUBSET, --limit=SUBSET 进一步限制所选主机/组模式  --limit=192.168.0.15 只对这个ip执行
  --skip-tags=SKIP_TAGS 只运行戏剧和任务不匹配这些值的标签  --skip-tags=copy_start
  -e EXTRA_VARS, --extra-vars=EXTRA_VARS  额外的变量设置为键=值或YAML / JSON
        #cat update.yml
        ---
        - hosts: {
   
   { hosts }}
          remote_user: {
   
   { user }}
        ..............
        #ansible-playbook update.yml --extra-vars "hosts=vipers user=admin"   传递{
   
   {hosts}}、{
   
   {user}}变量,hosts可以是 ip或组名
  -l,--limit 对指定的 主机/组 执行任务  --limit=192.168.0.10,192.168.0.11 或 -l 192.168.0.10,192.168.0.11 只对这个2个ip执行任务

例如:

[root@localhost ansible]# ansible php -m ping -k
SSH password: 
192.168.0.51 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
[root@localhost ansible]# ansible php -a 'pwd' -k
SSH password: 
192.168.0.51 | SUCCESS | rc=0 >>
/root

ansible-doc命令

参数详解:

[root@localhost ~]# ansible-doc  -h
Usage: ansible-doc [options] [module...]

Show Ansible module documentation     显示Ansible模块文档

Options:
  --version             show program's version number and exit    显示ansible-doc的版本号
  -h, --help            show this help message and exit  显示命令参数API文档
  -M MODULE_PATH, --module-path=MODULE_PATH   查询模块,--module-path=MODULE_PATH  指定模块的路径
                        Ansible modules/ directory
  -l, --list            List available modules   显示已存在的所有模块列表
  -s, --snippet         Show playbook snippet for specified module(s)   显示playbook制定模块的用法
  -v                    Show version number and exit   显示ansible-doc的版本号

例如:

[root@localhost ansible]# ansible-doc -s ping
- name: Try to connect to host, verify a usable python and return `pong' on success.
  action: ping

ansible-galaxy

参数详解:

[root@localhost ~]# ansible-galaxy -h
Usage: ansible-galaxy [init|info|install|list|remove] [--help] [options] ...

Options:
  -h, --help  show this help message and exit

See 'ansible-galaxy <command> --help' for more information on a specific command.

ansible-galaxy 指令用于方便的从https://galaxy.ansible.com/ 站点下载第三方扩展模块,我们可以形象的理解其类似于centos下的yum、python下的pipeasy_install.

例子:

[root@localhost ~]# ansible-galaxy install aeriscloud.docker - downloading role 'docker', owned by aeriscloud - downloading role from https://github.com/AerisCloud/ansible-docker/archive/v1.0.0.tar.gz - extracting aeriscloud.docker to /etc/ansible/roles/aeriscloud.docker - aeriscloud.docker was installed successfully

ansible-playbook

参数:

[root@localhost ~]# ansible-playbook -h
Usage: ansible-playbook playbook.yml

Options:
  -k, --ask-pass        ask for SSH password
  --ask-su-pass         ask for su password
  -K, --ask-sudo-pass   ask for sudo password
  --ask-vault-pass      ask for vault password
  -C, --check           don't make any changes; instead, try to predict some
                        of the changes that may occur
  -c CONNECTION, --connection=CONNECTION
                        connection type to use (default=smart)
  -D, --diff            when changing (small) files and templates, show the
                        differences in those files; works great with --check
  -e EXTRA_VARS, --extra-vars=EXTRA_VARS
                        set additional variables as key=value or YAML/JSON
  --force-handlers      run handlers even if a task fails
  -f FORKS, --forks=FORKS
                        specify number of parallel processes to use
                        (default=5)
  -h, --help            show this help message and exit
  -i INVENTORY, --inventory-file=INVENTORY
                        specify inventory host file
                        (default=/etc/ansible/hosts)
  -l SUBSET, --limit=SUBSET
                        further limit selected hosts to an additional pattern
  --list-hosts          outputs a list of matching hosts; does not execute
                        anything else
  --list-tasks          list all tasks that would be executed
  -M MODULE_PATH, --module-path=MODULE_PATH
                        specify path(s) to module library
                        (default=/usr/share/ansible/)
  --private-key=PRIVATE_KEY_FILE
                        use this file to authenticate the connection
  --skip-tags=SKIP_TAGS
                        only run plays and tasks whose tags do not match these
                        values
  --start-at-task=START_AT
                        start the playbook at the task matching this name
  --step                one-step-at-a-time: confirm each task before running
  -S, --su              run operations with su
  -R SU_USER, --su-user=SU_USER
                        run operations with su as this user (default=root)
  -s, --sudo            run operations with sudo (nopasswd)
  -U SUDO_USER, --sudo-user=SUDO_USER
                        desired sudo user (default=root)
  --syntax-check        perform a syntax check on the playbook, but do not
                        execute it
  -t TAGS, --tags=TAGS  only run plays and tasks tagged with these values
  -T TIMEOUT, --timeout=TIMEOUT
                        override the SSH timeout in seconds (default=10)
  -u REMOTE_USER, --user=REMOTE_USER
                        connect as this user (default=root)
  --vault-password-file=VAULT_PASSWORD_FILE
                        vault password file
  -v, --verbose         verbose mode (-vvv for more, -vvvv to enable
                        connection debugging)
  --version             show program's version number and exit

该指令是使用最多的指令,其通过读取playbook 文件后,执行相应的动作

ansible-pull

参数:

[root@localhost ~]# ansible-pull -h
Usage: ansible-pull [options] [playbook.yml]

Options:
  -K, --ask-sudo-pass   ask for sudo password    
  -C CHECKOUT, --checkout=CHECKOUT
                        branch/tag/commit to checkout.  Defaults to behavior
                        of repository module.
  -d DEST, --directory=DEST    
                        directory to checkout repository to
  -e EXTRA_VARS, --extra-vars=EXTRA_VARS
                        set additional variables as key=value or YAML/JSON
  -f, --force           run the playbook even if the repository could not be
                        updated
  -h, --help            show this help message and exit
  -i INVENTORY, --inventory-file=INVENTORY    指定库存主机文件的路径,默认为/etc/ansible/hosts
                        location of the inventory host file
  -m MODULE_NAME, --module-name=MODULE_NAME   从仓库检查的模块名,默认是git
                        Module name used to check out repository.  Default is
                        git.
  -o, --only-if-changed   
                        only run the playbook if the repository has been
                        updated
  --purge               purge checkout after playbook run   
  -s SLEEP, --sleep=SLEEP
                        sleep for random interval (between 0 and n number of
                        seconds) before starting. this is a useful way to
                        disperse git requests
  -U URL, --url=URL     URL of the playbook repository
  --vault-password-file=VAULT_PASSWORD_FILE
                        vault password file
  -v, --verbose         Pass -vvvv to ansible-playbook

该指令使用需要谈到ansible的另一种模式---pull 模式,这和我们平常经常用的push模式刚好相反,其适用于以下场景:你有数量巨大的机器需要配置,即使使用非常高的线程还是要花费很多时间;你要在一个没有网络连接的机器上运行Anisble,比如在启动之后安装。

ansible-vault

参数:

[root@localhost ~]# ansible-vault 
Usage: ansible-vault [create|decrypt|edit|encrypt|rekey] [--help] [options] file_name

Options:
  -h, --help  show this help message and exit

See 'ansible-vault <command> --help' for more information on a specific command.

ansible-vault主要应用于配置文件中含有敏感信息,又不希望他能被人看到,vault可以帮你加密/解密这个配置文件,属高级用法。主要对于playbooks里比如涉及到配置密码或其他变量时,可以通过该指令加密,这样我们通过cat看到的会是一个密码串类的文件,编辑的时候需要输入事先设定的密码才能打开。这种playbook文件在执行时,需要加上 –ask-vault-pass参数,同样需要输入密码后才能正常执行。

ansible-lint

ansible-lint是对playbook的语法进行检查的一个工具。用法是ansible-lint playbook.yml

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/108636.html原文链接:https://javaforall.net

(0)
上一篇 2022年3月1日 下午7:00
下一篇 2022年3月1日 下午7:00


相关推荐

  • 抗渗等级p6是什么意思_混凝土防水等级S6,P6分别是什么意思

    抗渗等级p6是什么意思_混凝土防水等级S6,P6分别是什么意思展开全部混凝土防水等级 P6 指的是防水混凝土的抗渗等级 意思是能抵 e4b893e5b19e 抗 0 6MPa 静水压力而不渗水 S6 指的也是是防水混凝土的抗渗等级 是之前的说法 抗渗等级大于或等于 P6 级别的混凝土用途防水混凝土主要用于工业 民用建筑地下工程 取水构筑物以及干湿交替作用或冻融作用的工程 扩展资料防水混

    2026年3月18日
    2
  • SpringBoot数据库访问异常HikariPool-1 – Exception during pool initialization.[通俗易懂]

    SpringBoot数据库访问异常HikariPool-1 – Exception during pool initialization.[通俗易懂]权限问题

    2022年6月23日
    230
  • pytest测试框架和unittest_python列表生成式

    pytest测试框架和unittest_python列表生成式一、核心要素(1)TestCase测试用例一个.py脚本就是一个测试用例测试用例类必须继承于unittest.TestCase类中的方法名必须以test开头,否则不会被执行类的外部写main方法(自测代码)(2)TestSuite测试套件测试套件就是测试用例的集合把多个测试用例集合到一起就构成了测试套件测试套件不能直接执行(3)TextTestR…

    2022年10月14日
    4
  • 数据库常见的三种join方式 inner join ,left outter join,right outter join

    数据库常见的三种join方式 inner join ,left outter join,right outter join数据库常见的三种join方式 inner join ,left outter join,right outter join

    2022年4月23日
    52
  • 可视化篇:效果图_可视化建模

    可视化篇:效果图_可视化建模写在最前在做可视化的时候,理解自己做的每个图形展示的意义,是多么的至关重要每做一张图的时候,我都在想,该如何阐述图形背后的故事下面是一些效果图,每张图,都只为更好地反应数据背后的那段故事。由于图片最大只能2M,所以调小后有的看起来不是很舒服,多多见谅所使用的工具主要是:R语言,Echart,D3.js如何实现在别的文章有详细说明。2016年的广州春运广州南站载客

    2022年8月31日
    5
  • MapStruct 详解

    MapStruct 详解GitHub 访问地址 nbsp https github com mapstruct mapstruct 使用例子 nbsp https github com mapstruct mapstruct examples nbsp MapStrcut 与其它工具对比以及使用说明 http www tuicool com articles uiIRjai nbsp BeanUtils copyPro

    2026年3月17日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号