ansible命令的基本用法和Inventory文件的写法:

一、 创建一个hosts文件,用来防止所有的主机名。
(ansible) [root@Master initNodes]# cat /opt/node.list
n1.plus7s.com
n2.plus7s.com
n3.plus7s.com
二、 使用ansible命令,-i 指定inventory(hosts)文件位置和主机组,中间用空格间隔,-m指定需要的模块:(当前master01节点还没生效,不影响演示)
(ansible) [root@Master initNodes]# ansible -i /opt/ansible/hosts all -m ping
master1.plus7s.com | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: ssh: Could not resolve hostname master1.plus7s.com: Name or service not known",
    "unreachable": true
}
n3.plus7s.com | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
n2.plus7s.com | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
n1.plus7s.com | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}


(ansible) [root@Master initNodes]# ansible -i /opt/ansible/hosts node -m ping
n2.plus7s.com | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
n1.plus7s.com | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
n3.plus7s.com | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}


(ansible) [root@Master initNodes]# ansible -i /opt/ansible/hosts master -m ping
master1.plus7s.com | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: ssh: Could not resolve hostname master1.plus7s.com: Name or service not known",
    "unreachable": true
}
(ansible) [root@Master initNodes]#

三、给ansible命令所调用的模块传参数:
(ansible) [root@Master ansible]# ansible -i /opt/ansible/hosts.yaml nodes -m fetch -a "src=/etc/fstab dest=/tmp/"
n2.plus7s.com | CHANGED => {
    "changed": true,
    "checksum": "9d6bcc2d91dcf1baf77fe909b7c86433676b0aa6",
    "dest": "/tmp/n2.plus7s.com/etc/fstab",
    "md5sum": "e999ceae2760ffc6ce806bc66458ee93",
    "remote_checksum": "9d6bcc2d91dcf1baf77fe909b7c86433676b0aa6",
    "remote_md5sum": null
}
n1.plus7s.com | CHANGED => {
    "changed": true,
    "checksum": "9d6bcc2d91dcf1baf77fe909b7c86433676b0aa6",
    "dest": "/tmp/n1.plus7s.com/etc/fstab",
    "md5sum": "e999ceae2760ffc6ce806bc66458ee93",
    "remote_checksum": "9d6bcc2d91dcf1baf77fe909b7c86433676b0aa6",
    "remote_md5sum": null
}
n3.plus7s.com | CHANGED => {
    "changed": true,
    "checksum": "9d6bcc2d91dcf1baf77fe909b7c86433676b0aa6",
    "dest": "/tmp/n3.plus7s.com/etc/fstab",
    "md5sum": "e999ceae2760ffc6ce806bc66458ee93",
    "remote_checksum": "9d6bcc2d91dcf1baf77fe909b7c86433676b0aa6",
    "remote_md5sum": null
}
(ansible) [root@Master ansible]# cd /tmp/
(ansible) [root@Master tmp]# ls
hosts                  n2.plus7s.com  systemd-private-02479212a1bf45df99cfc9571315d5d0-chronyd.service-ayKUNW
n1.plus7s.com  n3.plus7s.com  systemd-private-02479212a1bf45df99cfc9571315d5d0-do-agent.service-DEKdS5

从结果可以看出,ansible会在目标主机为多台主机拉去数据时自动创建于主机名相同的目录,并创建相应的目录到该主机名目录下。
This entry was posted in Ansible, AutomaticOPS and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *