Ansible 常用的模块

接下来介绍一些后面章节中会用到的模块 , 也是很常用的模块。
@ 调试和测试类的模块。
。 ping: ping 一下你的远程主机 , 如果可以通过 Ansible 连接成功,那么返回 pong 。
© debug : 用于调试的模块,只是简单打 印
些消息 , 有点像 Linux 的 echo 命令。
@ 文件类的模块。
© copy :从本地复制文件到远程节点。
。 template: 从本地复制文件到远程节点 , 并进行变量的替换。
© file :设置文件属性。
C) Linux 上的常用操作。
© user :管理用户账户。
。 yum : RedHat 系 Linux 上的包管理。
© service :管理服务。
© firewalld : 管理防火墙中的服务和端口。
@ 执行 shell 命令。
。 shell: 在节点上执行 shell 命令,支持$HOME 、~\ “ 〉 ”、“| ” 、 “ ;”和 “&” 。
。 command :在远程节点上面执行命令 , 不支持$HOME 、 “ < ”、 “>'|',';','&'.

  1. ping 模块
    这个是测试远程节点的 SSH 连接是否就绪的常用模块。但它并不像 Linux 命令那样简单地
    ping 一下远程节点 , 而是先检查能否通过 SSH 登录远程节点 , 再检查其 Python 版本能否满足要求 , 如果都满足则会返回 ping, 表示成功.ping 无须任何参数。因为 ping 是测试节点连接的可用性,所以通常在命令行中使用得较为频繁。下面是 ping 在命令行中的用法。
    ansible all -m ping

  2. debug 模块
    打印输出信息,类似 Linux 上的 echo 命令。
    。 通过参数 msg 定义打印的字符串。
    msg 中可以嵌入变量 ,下面的例子中注入了系统变量, Ansible 在执行 Playbook 之前会收集
    一些比较常用的系统变量 ,这样在 Playbook 中不需要定义直接使用就可以。
    [root@server3 test]# cat debug.yml
    ---
    - hosts: nginx
    remote_user: root
    tasks:
    - debug: 
     msg: "systemc {{ inventory_hostname }} has gateway {{ ansible_default_ipv4.gateway }}"
    - name: display all variables/facts known for a host
    debug:
     var: hostvars[inventory_hostname]["ansible_default_ipv4"]["gateway"]
    - shell: /usr/bin/uptime
    register: result
    - debug:
     var: result

[root@server3 test]# ansible-playbook debug.yml

PLAY [nginx] *****

TASK [Gathering Facts] ***
ok: [192.168.1.4]

TASK [debug] *****
ok: [192.168.1.4] => {
"msg": "systemc 192.168.1.4 has gateway 192.168.1.254"
}

TASK [display all variables/facts known for a host] **
ok: [192.168.1.4] => {
"hostvars[inventory_hostname][\"ansible_default_ipv4\"][\"gateway\"]": "192.168.1.254"
}

TASK [shell] *****
changed: [192.168.1.4]

TASK [debug] *****
ok: [192.168.1.4] => {
"result": {
"changed": true,
"cmd": "/usr/bin/uptime",
"delta": "0:00:00.006414",
"end": "2020-11-27 09:43:52.697253",
"failed": false,
"rc": 0,
"start": "2020-11-27 09:43:52.690839",
"stderr": "",
"stderr_lines": [],
"stdout": " 09:43:52 up 13 days, 23:27, 2 users, load average: 0.00, 0.01, 0.05",
"stdout_lines": [
" 09:43:52 up 13 days, 23:27, 2 users, load average: 0.00, 0.01, 0.05"
]
}
}

猜你喜欢

转载自blog.51cto.com/13810716/2555442