user_01_02
user_01_02

Reputation: 763

How to pass a groups hosts in ansible?

I am working on a ansible role to automate the installation of vertica. The role is running on a 3 node cluster . I am stuck in 2 places.

  1. I am trying to run the below command, I want to pass the hosts from my inventory of a group, I have passed run_once which will run only on 1st host ( which is what i want) but how to pass all the 3 hosts , like --hosts xx.xxx.xx.xx,xx.xxx.xx.xx,xx.xxx.xx.xx how can i achieve that?

  2. I want ansible to use the pem key i pass for installing vertica. Where should the key be stored.

    - name: Install vertica
      command: /opt/vertica/sbin/install_vertica --hosts x.xx.xx.xx,xx.xxx.xx.xx,xx.xxx.xx.xx --rpm /opt/vertica-{{ vertica_version }}.x86_64.RHEL6.rpm --dba-user-password-disabled --point-to-point --data-dir /vertica/data --ssh-identity x.pem
      when: vertica_already_installed|failed
      run_once: True
      become: yes
    

Looking for suggestions!

Thanks in advance!

Upvotes: 0

Views: 147

Answers (1)

larsks
larsks

Reputation: 312038

I am trying to run the below command, I want to pass the hosts from my inventory of a group...

Ansible provides you with the groups variable. This is a dictionary the keys of which are group names are the values are lists of hosts in each group. So for example if you have a group named vertica_servers, you could write something like this:

- name: Install vertica
    command: /opt/vertica/sbin/install_vertica --hosts ','.join(groups.vertica_servers) ...

I want ansible to use the pem key i pass for installing vertica. Where should the key be stored?

It sounds like you have an ssh private key, and this will be used by the install_vertica command to access the hosts in your cluster. You'll need to start by making that private key available on the remote host on which you're running the install_vertica command. You could do that with a copy task:

- name: install private key
  copy:
    src: "{{ private_key_file }}"
    dest: "/root/.ssh/vertica_key.pem"
    mode: "0400"
    owner: root
    group: root

And then pass that path to your install_vertica command:

- name: Install vertica
    command: /opt/vertica/sbin/install_vertica --hosts ','.join(groups.vertica_servers) --ssh-identity /root/.ssh/vertica_key.pem

This assumes that you have set private_key_file to the path to the key on your local system.

Upvotes: 1

Related Questions