【问题标题】:How to install Ansible in Amazon Linux EC2?如何在 Amazon Linux EC2 中安装 Ansible?
【发布时间】:2018-04-22 01:50:30
【问题描述】:

我选择这个 Amazon Linux:Amazon Linux AMI 2017.09.1 (HVM),SSD 卷类型 - ami-1a033c7a。 我使用以下命令安装了 Ansible:

sudo pip install ansible, 

它显示安装完成。当我运行 ansible --version 时,它显示:

ansible 2.4.1.0
  config file = None
  configured module search path = [u'/home/ec2-    
user/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python2.7/site-
packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 2.7.12 (default, Nov  2 2017, 19:20:38) [GCC 4.8.5     
20150623 (Red Hat 4.8.5-11)]

为什么是config file = None?它不应该显示/etc/ansible/ansible.cfg吗?我没有看到/etc/ansible/hosts,甚至没有看到文件夹/etc/ansible。我是否安装正确,文件夹/etc/ansible 在哪里?

【问题讨论】:

    标签: amazon-web-services ansible


    【解决方案1】:

    为什么是config file = None

    因为在运行ansible --version时没有找到配置文件。

    不应该显示/etc/ansible/ansible.cfg吗?

    没有。它应该显示实际使用的ansible.cfg

    根据文档,Ansible 尝试在以下位置查找配置文件:

    • ANSIBLE_CONFIG(环境变量)
    • ansible.cfg(在当前目录中)
    • .ansible.cfg(在主目录中)
    • /etc/ansible/ansible.cfg

    ansible --version 将向您显示正在使用的路径的确切路径。

    严格来说最后一点并不总是正确的,因为包管理器和虚拟环境管理器可能会导致 /etc 目录位于其他位置。

    我是否安装正确

    您在安装过程中没有提及任何错误或警告,ansible --version 已返回正确的响应。

    没有理由不相信它已正确安装。

    文件夹/etc/ansible在哪里?

    它在您的系统上不存在。没有默认的库存文件,也没有安装包创建的配置文件。

    创建一个。

    【讨论】:

    • 感谢 techraf。很奇怪,很多教程都说我会在 /etc/ansible 中看到主机和配置文件,但实际上如果使用 pip 安装,这两个文件甚至 /etc/ansible 都不存在,我必须自己创建它们。看起来我的 ansible 至少可以 ping 通。
    【解决方案2】:

    在这里,我自己回答这个问题。

    安装 ansible 的方法有很多种,然后您会获得不同的默认设置,具体取决于操作系统。许多教程只是假设 /etc/ansible 中已经存在 ansible_hosts 和 ansible.cfg,如果您使用 pip 安装 ansible,这是不正确的。

    事实上,如果你使用 pip 安装 ansible,那么你不会在 /etc/ansible 中看到 ansible.cfg 和 ansible_hosts。甚至文件夹 /etc/ansible 也不存在。不过没关系,您可以自己创建这两个文件,如下所示:

    假设你想将 ansible_hosts 和 ansible.cfg 存储在 /home/ec2-user 中,那么你可以:

    echo <remote_host> /home/ec2-user/ansible_hosts
    export ANSIBLE_INVENTORY=/home/ec2-user/ansible_hosts
    wget https://raw.githubusercontent.com/ansible/ansible/devel/examples/ansible.cfg
    mv ansible.cfg /home/ec2-user/
    export ANSIBLE_CONFIG=/home/ec2-user/ansible.cfg
    

    那么如果 ansible --version,你会看到

    ansible 2.4.1.0
      config file = /home/ec2-user/ansible.cfg
    ....
    

    如果你测试 ansible ad-hoc 命令(我的 remote_host 是 ubuntu,所以我使用 -u ubuntu,你可以把它改成你的):

    ansible all -m ping -u ubuntu
    

    然后你会看到 ansible ping remote_host 成功。

    这表明 ansible 确实有效。

    【讨论】:

      猜你喜欢
      • 2011-06-20
      • 2014-08-05
      • 1970-01-01
      • 2018-05-06
      • 2017-09-22
      • 1970-01-01
      • 1970-01-01
      • 2014-11-06
      • 1970-01-01
      相关资源
      最近更新 更多