Ansible-命令和模块

835次阅读
没有评论

ansible 系列命令

ansible ansible-doc  ansible-playbook  ansible-vault  ansible-console  ansible-galaxy ansible-pull

1、ansible-doc 显示模块帮助

    ansible-doc [options][module]

    -a  显示所有模块文档

    -l,--list 列出可用模块

    -s,--snippet 显示指定模块的 playbook 片段

实例:

    ansible-doc -l 列出所有模块

    ansible-doc ping 查看指定模块的帮助用法

    ansible-doc -s ping 查看指定模块的帮助用法

ansible 通过 ssh 实现配置管理、应用部署,任务执行等功能,建议配置 ansible 段能基于密钥认证的方式联系各被管理节点

2、ansible<host-pattern>[-m module_name] [-a args]

    --version 显示版本

    -m module 指定模块,默认为 command

    -v 详细过程 -vv -vvv 更详细

    --list-host 显示主机列表,可简写 --list

    -k,--ask-pass 提示输入 ssh 连接密码。默认 key 验证

    -K,--ask-become-pass  提示输入 sudo 时的口令

    -C,--check 检查不执行

    -T --timeout=TIMEOUT 执行命令的超时时间,默认 10s

    -u --user=REMOTE——USER 执行远程执行的用户

    -b,--become 代替旧版本的 sudo 切换

3、ansible 的 Host-pattern  匹配主机的列表

    all:表示所有 Inventory 中的所有主机

    *:通配符

        ansible“*”-m ping

        ansible 192.168.1.* -m ping

        ansible“*srvs”-m ping

    或关系

        ansible“webserver:dbserver”-m ping

        ansible "webserver:dbserver" -m ping #执行在 web 组并且在 dbserver 组中的主机(忽略重复的)

    与关系

        ansible "webserver:&dbserver" -m ping

        只执行在 web 组并且也在 dbserver 组中的主机

    逻辑非

        ansible 'webserver:!dbserver' -m ping【注意此处只能使用单引号!】

    综合逻辑

        ansible 'webserver:dbserver:&webserver:!dbserver' -m ping

    正则表达式

        ansible "webserver:&dbserver" -m ping

        ansible "~(web|db).*\.magedu.\com" -m ping

4、ansible 命令执行过程

  1. 加载自己的配置文件 默认 /etc/ansible/ansible.cfg、
  2. 加载自己对应的模块 如 command
  3. 通过 ansible 将模块或命令生成对应的临时 py 文件,并将改文件传输至远程服务器的对应执行用户 SHOME/.ansible/tmp/ansible-tmp- 数字 /XXX.py 文件
  4. 文件见 + x 执行
  5. 执行并返回结果
  6. 删除临时 py 文件,sleep 0 退出

5、执行状态

    绿色:执行成功并且不需要做改变的操作

    黄色:执行成功并且对目标主机做变更

    红色:执行失败

6、示例

ansible all -m ping -u wang -k

ansible all -m ping -u wang -b -k

ansible all -m ping -u wang -b -k  --become-user mage

ansible all -m command -u wang --becomer-user=root -a 'ls /root' -b -k -K

正文完
 
评论(没有评论)