In a playbook , I gathered the user key into the variable "user_key"  and 
using a add_host module to login to the server and execute the required  
tasks .

- name: Writing the key to a file 
      ansible.builtin.copy:
        content: "{{ user_key }}"
        dest: /tmp/new_inst.pem
        mode: '0600'
        follow: yes
      register: keyfile

    
    - name:  create a temp inventory
      ansible.builtin.add_host:
        hostname: '{{ servera }}'
        groups: mygroup
        ansible_ssh_private_key_file: "{{ keyfile.dest }}"
        ansible_ssh_user: "root"
        ansible_ssh_extra_args: '-o StrictHostKeyChecking=no'

- name: validate the httpd in new hosts
  hosts: mygroup
  gather_facts: true
  become: yes
  environment:
    ANSIBLE_HOST_KEY_CHECKING: "False"
  tasks:
    - name: Start service httpd, if not started
      service:
        name: httpd
       state: started
   
All works fine  using the above.
However is there an option to read the  content of the key directly , read 
the contents of the keyfile to the  add_host  module  with something like 
"ansible_ssh_private_key" ??
I want to avoid writing the key to a file, chmod 600 and then  remove it 
after execution.

Also ,  is there a  way to  read the variable "user_key"   when manually 
feed during the  ansible-playbook command , like  for the same above codes 
usage .

# ansible-playbook playbook -e "user_key={{ssh_content}}"
where  ssh_content    is a python variable which  have the  exact key_value 
details from another  program output .

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/bf4f3205-cc71-4584-a607-ceece3ae2d5dn%40googlegroups.com.

Reply via email to