14.12.2020

Ansible Playbook Generate Ssh Key

30
  • This module allows one to (re)generate OpenSSH private and public keys. It uses ssh-keygen to generate keys. One can generate rsa, dsa, rsa1, ed25519 or ecdsa private keys.

Ansible-playbook -inventory-file=hosts.ini setup.yml. Once complete you can now use the 'ansibleremote' user to run other Ansible playbooks and complete actions on hosts using the secure ssh keys. There are a few ways of using the ssh key connection, but one way is by referencing it. Again, the password is set using a variable at the top of the playbook. Use the authorizedkey Ansible module to copy the public ssh key (kept in the same folder as the Ansible project) and place it on the server in the.ssh/authorizedkeys file. After this step it is possible to connect to the server using the ssh keys alone.

The below requirements are needed on the host that executes this module.

  • ssh-keygen
ParameterChoices/DefaultsComments
attributes
string
The attributes the resulting file or directory should have.
To get supported flags look at the man page for chattr on the target system.
This string should contain the attributes in the same order as the one displayed by lsattr.
The = operator is assumed as default, otherwise + or - operators need to be included in the string.
comment
added in 2.9
Provides a new comment to the public key. When checking if the key is in the correct state this will be ignored.
force
boolean
    Choices:
  • yes
Should the key be regenerated even if it already exists
group
string
Name of the group that should own the file/directory, as would be fed to chown.
mode
string
The permissions the resulting file or directory should have.
For those used to /usr/bin/chmod remember that modes are actually octal numbers. You must either add a leading zero so that Ansible's YAML parser knows it is an octal number (like 0644 or 01777) or quote it (like '644' or '1777') so Ansible receives a string and can do its own conversion from string into number.
Giving Ansible a number without following one of these rules will end up with a decimal number which will have unexpected results.
As of Ansible 1.8, the mode may be specified as a symbolic mode (for example, u+rwx or u=rw,g=r,o=r).
As of Ansible 2.6, the mode may also be the special string preserve.
When set to preserve the file will be given the same permissions as the source file.
owner
string
Name of the user that should own the file/directory, as would be fed to chown.
path
path / required
Name of the files containing the public and private key. The file containing the public key will have the extension .pub.
selevel
string
Default:
The level part of the SELinux file context.
This is the MLS/MCS attribute, sometimes known as the range.
When set to _default, it will use the level portion of the policy if available.
serole
string
When set to _default, it will use the role portion of the policy if available.
setype
string
When set to _default, it will use the type portion of the policy if available.
seuser
string
By default it uses the system policy, where applicable.
When set to _default, it will use the user portion of the policy if available.
size
integer
Specifies the number of bits in the private key to create. For RSA keys, the minimum size is 1024 bits and the default is 4096 bits. Generally, 2048 bits is considered sufficient. DSA keys must be exactly 1024 bits as specified by FIPS 186-2. For ECDSA keys, size determines the key length by selecting from one of three elliptic curve sizes: 256, 384 or 521 bits. Attempting to use bit lengths other than these three values for ECDSA keys will cause this module to fail. Ed25519 keys have a fixed length and the size will be ignored.
state
string
    Choices:
  • absent
Whether the private and public keys should exist or not, taking action if the state is different from what is stated.
type
string
    Choices:
  • dsa
  • rsa1
  • ecdsa
  • ed25519
The algorithm used to generate the SSH private key. rsa1 is for protocol version 1. rsa1 is deprecated and may not be supported by every version of ssh-keygen.
unsafe_writes
boolean
    Choices:
  • yes
Influence when to use atomic operation to prevent data corruption or inconsistent reads from the target file.
By default this module uses atomic operations to prevent data corruption or inconsistent reads from the target files, but sometimes systems are configured or just broken in ways that prevent this. One example is docker mounted files, which cannot be updated atomically from inside the container and can only be written in an unsafe manner.
This option allows Ansible to fall back to unsafe methods of updating files when atomic operations fail (however, it doesn't force Ansible to perform unsafe writes).
IMPORTANT! Unsafe writes are subject to race conditions and can lead to data corruption.

Common return values are documented here, the following are the fields unique to this module:

KeyReturnedDescription
comment
string
changed or success
Sample:
filenamechanged or success
Path to the generated SSH private key file

/tmp/id_ssh_rsa
fingerprint
string
changed or success
Sample:
SHA256:r4YCZxihVjedH2OlfjVGI6Y5xAYtdCwk8VxKyzVyYfM
public_key
string
changed or success
Sample:
ssh-rsa AAAAB3Nza(..omitted..)veL4E3Xcw test_key
size
integer
changed or success
Sample:
typechanged or success
Algorithm used to generate the SSH private key

rsa

  • This module is not guaranteed to have a backwards compatible interface. [preview]
  • This module is maintained by the Ansible Community. [community]

Authors¶

  • David Kainz (@lolcube)

Hint

If you notice any issues in this documentation, you can edit this document to improve it.

Iset up SSH keys on a Linux or Unix as per my project needs and cloud hosting providers. I also new to Ansible IT automation and DevOps tool. My management node has keys for Linode, AWS/EC2 and Google cloud. How do I set up and tell Ansible to use different ssh keys? How do I configure SSH credentials per cloud hosting service provider?
Ansible is a free and open source IT software that automates software provisioning, configuration management, and application deployment. One can use Ansible to create cloud hosts in EC2, patch servers, add users, configure routers/firewall and more. Ansible uses SSH which allows users and ansbile too; to log in to remote servers and perform management tasks. This page shows how to already setup SSH keys to log in into remote server using Ansible IT automation tool.
Advertisements

Ansible define ssh key per host using ansible_ssh_private_key_file

You need to use ansible_ssh_private_key_file in inventory file. The syntax is pretty simple:

ansible_ssh_private_key_file example

Medieval ii total war cd key generator. Let us open a file named hosts in ~/projects/ansible/hosts using a text editor such as vim command:
$ vim ~/projects/ansible/hosts
Sample file:

Ansible Playbook Generate Ssh Key Linux

Another example:

It is possible to group it as follows:

How to dry run and test your inventory or playbooks

Ansible Playbook Generate Ssh Key Password

You can ask Ansible not to make any changes; instead, try to predict some differences in those files:
$ ansible-playbook -i hosts my-book.yml --check
$ ansible freebsd -i hosts --list-hosts
$ ansible aws -i hosts --list-hosts
$ ansible google_cloud -i hosts --list-hosts

A list of all ansible SSH connection config options

Ansible Playbook Generate Ssh Key Login

  1. ansible_port=2020 : The ssh port number, if not 22
  2. ansible_user=vivek : The default ssh user name to use.
  3. ansible_ssh_private_key_file=/path/to/ssh.pem : Private key file used by ssh. Useful if using multiple keys and you don’t want to use SSH agent.
  4. ansible_python_interpreter=/usr/local/bin/python : The target host python path

Conclusion

You learned how to use different or multiple SSH private keys for the servers you manage using Ansible tool. For more info see “List of Behavioral Inventory Parameters” here.

ADVERTISEMENTS