Ansible安装配置和基本使用( 三 )

Ansible安装配置和基本使用 。小编来告诉你更多相关信息 。
Ansible安装配置和基本使用使用逻辑与
在websrvs组并且在dbsrvs组中的主机 。
[root@ansible-srv1 ansible-example]# ansible \"websrvs:&dbsrvs\" -m ping10.10.108.30 | SUCCESS => {\"ansible_facts\": {\"discovered_interpreter_python\": \"/usr/bin/python\"},\"changed\": false,\"ping\": \"pong\"}使用逻辑非
在websrvs组,但不在dbsrvs组中的主机.注意:此处只能是单引号 。
[root@ansible-srv1 ansible-example]# ansible \'websrvs:!dbsrvs\' -m ping10.10.108.33 | SUCCESS => {\"ansible_facts\": {\"discovered_interpreter_python\": \"/usr/bin/python\"},\"changed\": false,\"ping\": \"pong\"}10.10.108.31 | SUCCESS => {\"ansible_facts\": {\"discovered_interpreter_python\": \"/usr/bin/python\"},\"changed\": false,\"ping\": \"pong\"}10.10.108.32 | SUCCESS => {\"ansible_facts\": {\"discovered_interpreter_python\": \"/usr/bin/python\"},\"changed\": false,\"ping\": \"pong\"}使用正则表达式
[root@ansible-srv1 ansible-example]# ansible \"websrvs:&dbsrvs\" -m ping10.10.108.30 | SUCCESS => {\"ansible_facts\": {\"discovered_interpreter_python\": \"/usr/bin/python\"},\"changed\": false,\"ping\": \"pong\"}[root@ansible-srv1 ansible-example]# ansible \"~(web|db)srvs\" -m ping10.10.108.30 | SUCCESS => {\"ansible_facts\": {\"discovered_interpreter_python\": \"/usr/bin/python\"},\"changed\": false,\"ping\": \"pong\"}10.10.108.33 | SUCCESS => {\"ansible_facts\": {\"discovered_interpreter_python\": \"/usr/bin/python\"},\"changed\": false,\"ping\": \"pong\"}10.10.108.31 | SUCCESS => {\"ansible_facts\": {\"discovered_interpreter_python\": \"/usr/bin/python\"},\"changed\": false,\"ping\": \"pong\"}10.10.108.32 | SUCCESS => {\"ansible_facts\": {\"discovered_interpreter_python\": \"/usr/bin/python\"},\"changed\": false,\"ping\": \"pong\"}3.2.2 ansible执行命令过程

  • 加载自己的配置文件默认/etc/ansible/ansible.cfg,如果指定了你自定义的清单文件,则从自己的清单文件中查找被管理主机
  • 加载自己对应的模块文件,如:command
  • 通过ansible将模块或命令生成对应的临时py文件,并将该文件传输至远程服务器的对应执行用户$HOME/.ansible/tmp/ansible-tmp-数字/XXX.PY文件
  • 给文件+x执行
  • 执行并返回结果
  • 删除临时py文件,退出
可以通过加参数-v或者-vvv列出详细的执行过程(可以多加几个v参数) 。
[root@ansible-srv1 ansible-example]# ansible \"~(web|db)servers\" -vvv -m ping > ansible.log[root@ansible-srv1 ansible-example]# grep \"chmod\" ansible.log3.2.3 ansible执行后颜色描述默认情况下是以下三种颜色:
Ansible安装配置和基本使用 。小编来告诉你更多相关信息 。
Ansible安装配置和基本使用
  • 绿色:表示成功
  • 黄色:修改了远程文件后并执行成功
  • 红色:表示执行失败
但是在ansible配置文件中可以定义颜色,如下:
[root@ansible-srv1 ansible-example]# vim /etc/ansible/ansible.cfg[colors]#highlight = white#verbose = blue#warn = bright purple#error = red#debug = dark gray#deprecate = purple#skip = cyan#unreachable = red#ok = green#changed = yellow#diff_add = green#diff_remove = red#diff_lines = cyan3.2.4 Ansible-playbook示例[root@ansible-srv1 ansible-example]# cat echo-demo.yml---- hosts: dbsrvsremote_user: roottasks:- name: echo demoshell: echo \"第一个 ansible-playbook 示例\" [root@ansible-srv1 ansible-example]# ansible-playbook echo-demo.yml解释:这里指定主机组为:dbsrvs,指定远程用户为root,使用shell模块执行了一个echo命令 。
3.2.5 ansible-vault该工具用于对yaml文件进行加解密,格式如下:
ansible-vault [create|decrypt|edit|encrypt|rekey|view]如:ansible-vault encrypt echo-demo.yml# 加密ansible-vault view echo-demo.yml# 查看ansible-vault decrypt echo-demo.yml# 解密ansible-vault edit echo-demo.yml# 编辑加密文件ansible-vault create echo-demo.yml# 创建新文件Ansible是一个简单高效且很强大的工具,它的功能远不止于此 。而那些不常用的命令工具,本文也暂不介绍,学习了不用也是在浪费自己的时间 。如果有兴趣的话可以去官网或者其他站点查阅资料自行了解 。
【Ansible安装配置和基本使用】以上就是带来的Ansible安装配置和基本使用的全部内容,您了解了吗?