安装和使用ansible-附带详细步骤

艺帆风顺 发布于 2025-04-03 21 次阅读


1 安装ansible

1.1使用pip安装

首先,我们需要安装一个python-pip包,安装完成以后,则直接使用pip命令来安装我们的包,具体操作过程如下:

    $ yum install python-pip$ pip install ansible.

    1.2 使用 yum 安装

    yum 安装是我们很熟悉的安装方式了。我们需要先安装一个epel-release包,然后再安装我们的 ansible 即可,具体操作过程如下:

      $ yum install epel-release -y$ yum install ansible –y

      查看是否安装成功:

      $ ansible -V

      2 配置ansible

      使用yum安装,无需创建文件夹:

      $ mkdir -vp /etc/ansible

      修改配置文件:

      $ vi ansible.cfg

      修改管理主机:

      $ vi hosts

      添加如下内容:

        [local]10.20.3.75[remote]10.20.3.7210.20.3.7310.20.3.74

        3 测试ansible是否正常运行

        执行:

          $ cd cd /etc/ansible/$ root ansible -i ./hosts --connection=local local -m ping

          输出:

            10.20.3.75 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong"

            执行:

            $ root ansible -i ./hosts remote -m ping

            输出:

              10.20.3.73 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong"}10.20.3.74 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong"}10.20.3.72 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong"}

              4 分发脚本执行

              创建脚本:

              $ vi testAnsible.sh

              内容如下:

                #!bin/shtouch ansibleTest.logecho "Test ansible file." > /software/ansibleTest.log

                发脚本:

                $ ansible remote -m copy -a "src=testAnsible.sh dest=/software/testAnsible.sh"

                执行脚本:

                $ ansible remote -m shell -a "sh /software/testAnsible.sh chdir=/software"

                执行成功后,会在/software路径下创建ansibleTest.log文件,并且内容为Test ansible file.

                  版权声明:本文内容来自51CTO:fanxinglanyu ,遵循CC 4.0 BY-SA版权协议上原文接及本声明。本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。原文链接:https://blog.51cto.com/u_10941874/5333523如有涉及到侵权,请联系,将立即予以删除处理。在此特别鸣谢原作者的创作。此篇文章的所有版权归原作者所有,与本公众号无关,商业转载建议请联系原作者,非商业转载请注明出处。