Ansible:测试SSH登录是否可能没有FATAL错误?

时间:2013-10-28 18:16:19

标签: ansible

我有一个设置手册,它采用新安装的Linux实例,以默认用户身份登录(我们将调用user1),创建另一个用户(我们将调用user2),然后禁用user1。因为user1只能在执行这组任务之前访问实例,所以任务都在一个特殊的playbook中,我们必须记住在新实例上运行。之后,所有常见任务都由user2运行,因为user1不再存在。

我希望将设置和常用手册结合起来,这样我们就不必再手动运行设置手册了。我尝试创建一个任务,以查看实例上存在哪个用户,通过尝试通过SSH作为user1登录来使原始设置任务成为条件。问题是,如果我为任一用户尝试SSH登录,则ansible退出并出现FATAL错误,因为它无法登录:在新实例上尚未存在user2,或者在执行安装程序手册后已禁用user1。

我认为通过SSH测试登录是确定实例所处的条件的唯一方法。有没有办法测试SSH登录而不会出现致命错误,然后根据结果有条件地执行任务?

2 个答案:

答案 0 :(得分:9)

一种方法是通过local_action使用shell来向user1调用一个简单的ssh命令,看看它是否成功。这些方面的东西:

- name: Test for user1
  local_action: shell ssh user1@{{ inventory_hostname }} "echo success"
  register: user1_enabled

然后你可以在另一个任务中使用这样的东西来看它是否有效:

  when: user1_enabled.stdout.find("success") != -1

答案 1 :(得分:3)

使用Ansible> = 2.5时,可以使用public class Designation { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Column(name="id_designation") private Long idDesignation; private String designation; @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "id_departement", nullable = false) private Departement departement; public Designation() { super(); } public Designation(String designation, Departement departement) { super(); this.designation = designation; this.departement = departement; } public Long getIdDesignation() { return idDesignation; } public void setIdDesignation(Long idDesignation) { this.idDesignation = idDesignation; } public String getDesignation() { return designation; } public void setDesignation(String designation) { this.designation = designation; } public Departement getDepartement() { return departement; } public void setDepartement(Departement departement) { this.departement = departement; } https://docs.ansible.com/ansible/2.5/modules/wait_for_connection_module.html)。

wait_for_connection_module