0

I have to execute some shell commands remotely (via SSH) from Ansible, but without external hosts file.

Host information is actually current "item" from the loop, so I have to execute some commands remotely while in this loop (for each item). I already have a loop, but I'm not sure about the code for ssh.

Username and password are variables from Ansible Tower that I can call in this task i.e. "{{username}}" and "{{password}}"


- name: Connect to remote servers via SSH
  hosts: "{{item}}"
  gather_facts: no
  connection: ssh
  tasks:
    - name: Run command on remote server
      command: uptime
      register: uptime_output

Is this code correct and where do I put credentials in it?

Thanks.

LJS
  • 317
  • 1
  • 9
  • "_but without external hosts file_", is there a specific reason for? "_Host information is actually current "item" from the loop, so I have to execute some commands remotely while in this loop (for each item)._", where is that information, the list content (`item`) coming from? However, according your current description you could just use [`add_host`](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/add_host_module.html) and add the hosts to a dynamic group and connection as usual with Ansible to it. – U880D Mar 07 '23 at 15:56
  • The thing is that hosts (these are actually VM's) are dynamically gathered / determined whenever playbook is executed. So, I get a list of hosts (VM's) and for each of them I am doing some operations in their vCenter. However, along with these VMware operations, I have to also establish SSH connection to and remotely execute some code (for each item/host in this same loop) – LJS Mar 07 '23 at 16:08
  • That's exactly the right use case for the `add_host` module and then you can just use Ansible usual as [Ansible is connecting to Remote Hosts via SSH](https://docs.ansible.com/ansible/latest/inventory_guide/connection_details.html). – U880D Mar 07 '23 at 16:11
  • Thanks, I haven't use add_host so far. How will this dynamic group look after let's say 10 cycles? Will it have 10 hosts, or just the last one? I don't mind if there are all hosts, just what I care is that I connect to the right host, which is I guess the last one added to the group. Maybe it would be best that group is overwritten with each cycle, so it contain only the current host. – LJS Mar 07 '23 at 16:15
  • If this group is dynamic, how do I use it with external credentials from Ansible Tower? Where do I put them, how do I call them? – LJS Mar 07 '23 at 16:21
  • Please take note that "_**How do I use external credentials from Ansible Tower**_" is an other new question, see in example [Ansible Tower: Custom Credential Type](https://stackoverflow.com/a/73422397/6771046) or [Ansible Tower - How to pass Machine credentials ...](https://stackoverflow.com/a/71030474/6771046). – U880D Mar 07 '23 at 16:40

1 Answers1

1

I understand your question that you are looking for

  • How to connect to a host not in inventory file?
  • How to connect to Linux VMs using variables?

As already commented this is a use case for add_host module – Add a host (and alternatively a group) to the ansible-playbook in-memory inventory. The following minimal example shows a solution approach.

---
- hosts: localhost
  become: false
  gather_facts: false

  tasks:

  - add_host:
      hostname: "{{ item }}"
      group: vcenter_vms
    loop: "{{ LIST_OF_VMS }}"

- hosts: vcenter_vms
  become: false
  gather_facts: false
  remote_user: "{{ username }}"
  ansible_ssh_pass: "{{ password }}"

  tasks:

  - name: Show hostname
    shell:
      cmd: "hostname && who am i"
    register: result

  - name: Show result
    debug:
      var: result

You need only take care of the difference between your initial playbook running ansible_user and the later used remote_user (annot.: which is only an alias for better understanding, see Ansible ansible_user vs remote_user), as well the ansible_ssh_pass, the remote_password.

Similar Q&A

I like also to recommend other Stack sites with more advanced examples.

You can also just search with tag [ansible] and keyword add_host for more examples.

U880D
  • 8,601
  • 6
  • 24
  • 40