AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 628989
Accepted
Robert
Robert
Asked: 2014-09-17 11:11:21 +0800 CST2014-09-17 11:11:21 +0800 CST 2014-09-17 11:11:21 +0800 CST

如何为 SSH 连接设置默认 Ansible 用户名/密码?

  • 772

我正在使用 Ansible,并且我的库存/全部中有此配置:

[master]
192.168.1.10 ansible_connection=ssh ansible_ssh_user=vagrant ansible_ssh_pass=vagrant

[slave]
192.168.1.11 ansible_connection=ssh ansible_ssh_user=vagrant ansible_ssh_pass=vagrant
192.168.1.12 ansible_connection=ssh ansible_ssh_user=vagrant ansible_ssh_pass=vagrant

[app]
192.168.1.13 ansible_connection=ssh ansible_ssh_user=vagrant ansible_ssh_pass=vagrant

[all:children]
master
slave

我不想为每个新实例重复所有参数。我怎样才能在一个地方配置它们?有没有这些参数的文件?

ansible
  • 7 7 个回答
  • 408570 Views

7 个回答

  • Voted
  1. Best Answer
    Alexey Labzov
    2015-12-12T02:12:44+08:002015-12-12T02:12:44+08:00

    您可以将以下部分添加到库存文件中:

    [all:vars]
    ansible_connection=ssh
    ansible_user=vagrant
    ansible_ssh_pass=vagrant
    

    注意:在 Ansible 2.0 之前ansible_user是ansible_ssh_user.

    • 148
  2. Philip Wilson
    2014-09-17T13:31:28+08:002014-09-17T13:31:28+08:00

    组变量

    您可以使用Ansible 的最佳实践文档中指定的playbook 布局并创建定义它们的文件来设置适用于所有主机的变量。group_vars/all

    ---
    # file: group_vars/all
    ansible_connection: ssh 
    ansible_ssh_user: vagrant 
    ansible_ssh_pass: vagrant
    

    [编辑] 我对你试图做的事情感到困惑。您不需要在清单中指定 Ansible 用户或密码。如果你使用 Vagrant,你肯定不会,如果你从命令行调用 Ansible,你可以指定用户--user=vagrant并让它要求输入密码--ask-pass。

    • 27
  3. podarok
    2014-09-18T23:33:08+08:002014-09-18T23:33:08+08:00

    我认为最好在所有服务器上使用 ssh 密钥安装。您应该仅在每个节点上运行 ssh-copy-id 并在任何地方安装您的 ssh 密钥,以便 ansible 能够使用您的 ssh 密钥登录。不将密码保存到剧本/库存中会更安全。

    为此,您应该生成 ssh 密钥对 ,然后为所有服务器运行 ssh-copy-id。

    • 8
  4. Mithril
    2017-11-03T23:23:01+08:002017-11-03T23:23:01+08:00

    将以下添加到清单主机。

    对于 Ansible <2.0:

    [all:vars]
    ansible_connection=ssh
    ansible_ssh_user=vagrant 
    ansible_ssh_pass=vagrant
    

    对于 Ansible >=2.0:

    [all:vars]
    ansible_connection=ssh # actually default mode smart is OK
    ansible_user=vagrant
    ansible_pass=vagrant # or ansible_ssh_pass=vagrant
    
    • 8
  5. kentr
    2016-12-15T12:52:09+08:002016-12-15T12:52:09+08:00

    免责声明:我只在 OSX 上测试过。根据各种文档,我希望它可以在其他平台上运行。

    “项目目录”是指 Vagrant 项目的基本目录——包含Vagrantfile.

    Vagrant 自动生成的 Ansible Inventory 文件:

    Vagrant使用默认的 Ansible 连接变量创建清单文件。在 中寻找它<project directory>/.vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory。

    该文件将由 Vagrant 根据需要重新生成,因此手动编辑将被覆盖。但是,根据 Vagrant 文档,您可以在其中指定多台机器、组变量等,Vagrantfile它们将被添加到此清单文件中。

    将 Ansible 配置为默认使用此清单文件:

    ansible当您在项目目录中(在主机上)时,要使该文件成为命令使用的默认文件,请在项目目录中添加一个ansible.cfg包含这些内容的文件,并根据需要更改路径:

    [defaults]
    inventory = ./path/to/inventory
    

    要确认此清单文件正在被使用,请将其查找为 ansible 报告的默认值:

    (从项目目录中)

    $ ansible | grep inventory ERROR! Missing target hosts -i INVENTORY, --inventory-file=INVENTORY specify inventory host path (default=./.vagrant/provis ioners/ansible/inventory/vagrant_ansible_inventory) or

    要确认您的主机:

    $ ansible all --list-hosts hosts (2): master slave

    在这些主机上使用 Ansible:

    在项目目录中,您应该能够ansible正常使用您在Vagrantfile.

    例如:

    ansible slave -a 'hostname'
    
    • 2
  6. activedecay
    2020-06-09T13:01:36+08:002020-06-09T13:01:36+08:00

    您必须设置正确的变量。你需要使用:

    # yml syntax
    ansible_user: myusername
    ansible_password: mypassword
    
    # inventory syntax
    host ansible_user=myusername ansible_password=mypassword
    

    您需要仔细阅读,因为ansible_ssh_password并且ansible_ssh_user在版本中不起作用:ansible 2.9.6

    • 1
  7. Namasivayam Chinnapillai
    2020-08-06T19:22:50+08:002020-08-06T19:22:50+08:00

    以下是如何为 SSH 连接设置默认 Ansible 用户名/密码的步骤

    ansible.cfs 配置将是

    $ 猫 ansible.cfg
    [默认值]
    库存 = ./库存
    
    [特权升级]
    变成=真
    成为_method = sudo
    成为_用户 = 根
    
    

    库存文件将是

    $猫库存
    [应用程序1]
    10.163.128.21
    
    [应用程序1:变量]
    ansible_password=*************
    ansible_ssh_user=Appuser1
    host_key_checking=False
    

    ansible执行成功结果

    $ ansible App1 -m ping
    10.163.128.21 | 成功 => {
        “ansible_facts”:{
            “discovered_interpreter_python”:“/usr/bin/python”
        },
        “改变”:错误,
        “乒乓”
    }
    
    • 1

相关问题

  • Ansible 剧本,使用选项 --disabled-login 创建用户

  • ansible playbook,操作系统不同的用户

  • ansible:为什么文件模块会跳过?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve