Hi, Unfortunately, ansible_ssh_private_key_file requires a file and I don't see a proper way to assemble a file from a string in an inline manner. You could probably do some jinja magic to that end, though it won't be very elegant. I'll try to wrap my head about it a bit more and come back to you if I find something.
As for your second question, are you trying to read ssh_content from a file or stdin ? Here is an example for both scenarios using lookup plugin <https://docs.ansible.com/ansible/latest/plugins/lookup.html> : 19:02|ptn@BENDER:~/conf (main // U:1 M(u):1) (default)$ cat ~/TEMP/truc truc 19:02|ptn@BENDER:~/conf (main // U:1 M(u):1) (default)$ ansible -c local localhost -m debug -a 'var=foo' -e foo="{{ lookup('file', '~/TEMP/truc') }}" localhost | SUCCESS => { "foo": "truc" } 19:03|ptn@BENDER:~/conf (main // U:1 M(u):1) (default)$ echo "truc" | ansible -c local localhost -m debug -a 'var=foo' -e foo="{{ lookup('file', '/dev/stdin') }}" localhost | SUCCESS => { "foo": "truc" } Le lundi 7 août 2023 à 13:01:41 UTC+2, Veera a écrit : > 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/88a61c75-1d43-4b39-8db2-5a3dbd101722n%40googlegroups.com.
