{ "ansible_facts": { "discovered_interpret" />

IT袋

当前位置:主页 > 经验教程 > 系统教程 >

Ansible安装配置和基本使用

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

时间:2023-09-21 18:51:35 来源:IT袋 作者:苏晓敏
导读:Ansible安装配置和基本使用,使用逻辑与 在websrvs组并且在dbsrvs组中的主机。 [root@ansible-srv1 ansible-example]# ansible "websrvs:dbsrvs" -m ping10.10.108.30 | SUCCESS => { "ansible_facts": { "discovered_interpret

Ansible安装配置和基本使用


使用逻辑与
在websrvs组并且在dbsrvs组中的主机。

[root@ansible-srv1 ansible-example]# ansible "websrvs:&dbsrvs" -m ping
10.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 ping
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"
}


使用正则表达式

[root@ansible-srv1 ansible-example]# ansible "websrvs:&dbsrvs" -m ping
10.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 ping
10.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.log

3.2.3 ansible执行后颜色描述

默认情况下是以下三种颜色:

相关阅读