是否有一种方法可以忽略由Ansible做出的SSH真实性检查?例如,当我刚刚安装了一个新服务器时,我必须回答这个问题:

GATHERING FACTS ***************************************************************
The authenticity of host 'xxx.xxx.xxx.xxx (xxx.xxx.xxx.xxx)' can't be established.
RSA key fingerprint is xx:yy:zz:....
Are you sure you want to continue connecting (yes/no)?

我知道这通常是一个坏主意,但我将它合并到一个脚本中,该脚本首先在我的云提供商创建一个新的虚拟服务器,然后自动调用我的ansible playbook来配置它。我希望在脚本执行过程中避免任何人为干预。


当前回答

将所有主机的host_key_checking更改为false是一个非常糟糕的主意。

你唯一想要忽略它的时候,是在“第一次接触”的时候,这本剧本将实现:

---
- name: Bootstrap playbook
  # Don't gather facts automatically because that will trigger
  # a connection, which needs to check the remote host key
  gather_facts: false

  tasks:
    - name: Check known_hosts for {{ inventory_hostname }}
      local_action: shell ssh-keygen -F {{ inventory_hostname }}
      register: has_entry_in_known_hosts_file
      changed_when: false
      ignore_errors: true
    - name: Ignore host key for {{ inventory_hostname }} on first run
      when: has_entry_in_known_hosts_file.rc == 1
      set_fact:
        ansible_ssh_common_args: "-o StrictHostKeyChecking=no"
    # Now that we have resolved the issue with the host key
    # we can "gather facts" without issue
    - name: Delayed gathering of facts
      setup:

因此,我们只在known_hosts文件中没有主机密钥时关闭主机密钥检查。

其他回答

你可以在运行playbook时将它作为命令行参数传递:

ansible-playbook玩。yml——ssh-common-args='-o StrictHostKeyChecking=no'

将所有主机的host_key_checking更改为false是一个非常糟糕的主意。

你唯一想要忽略它的时候,是在“第一次接触”的时候,这本剧本将实现:

---
- name: Bootstrap playbook
  # Don't gather facts automatically because that will trigger
  # a connection, which needs to check the remote host key
  gather_facts: false

  tasks:
    - name: Check known_hosts for {{ inventory_hostname }}
      local_action: shell ssh-keygen -F {{ inventory_hostname }}
      register: has_entry_in_known_hosts_file
      changed_when: false
      ignore_errors: true
    - name: Ignore host key for {{ inventory_hostname }} on first run
      when: has_entry_in_known_hosts_file.rc == 1
      set_fact:
        ansible_ssh_common_args: "-o StrictHostKeyChecking=no"
    # Now that we have resolved the issue with the host key
    # we can "gather facts" without issue
    - name: Delayed gathering of facts
      setup:

因此,我们只在known_hosts文件中没有主机密钥时关闭主机密钥检查。

使用名为validate_certs的参数来忽略ssh验证

- ec2_ami:
    instance_id: i-0661fa8b45a7531a7
    wait: yes
    name: ansible
    validate_certs: false
    tags:
      Name: ansible
      Service: TestService

通过这样做,它忽略了ssh验证过程

大多数问题出现在你想添加新的主机动态库存(通过add_host模块)在剧本。我不想永久禁用指纹主机检查,所以在全局配置文件中禁用它的解决方案对我来说是不合适的。在运行playbook之前像ANSIBLE_HOST_KEY_CHECKING那样导出var是运行前需要记住的另一件事。

最好将本地配置文件添加到与playbook相同的目录中。创建名为ansible.cfg的文件,并粘贴以下文本:

[defaults]
host_key_checking = False

不需要记得在env vars或ansible-playbook选项中添加一些东西。把这个文件放到ansible git repo很容易。

尼克贝利亚

对于那些使用jenkins运行playbook的人,我刚刚在运行ansible-playbook之前添加了环境变量ANSIBLE_HOST_KEY_CHECKING = False 例如这个:

export ANSIBLE_HOST_KEY_CHECKING=False
ansible-playbook 'playbook.yml' \
--extra-vars="some vars..." \
--tags="tags_name..." -vv