Ansible playbook can be used to automate remote devices. Every remote device needs to have one ansible user to apply the automation.
Follow this script to have complete configuration of ansible user, ssh keys, sudo permission on remote Nodegrid device.
Create these four files in one folder and later give them executable permission.
1. ansible_setup.sh
- #!/bin/sh
- read -p " Enter Your Choice 1(yes)/2(no): " choice
- while [ $choice -eq 1 ]
- do
- read -p "enter the ip address : " IPADDRESS
- echo " The ip address is $IPADDRESS "
- echo "Copy all config files to Nodegrid, provide root password"
- scp ansible_setup* root@$IPADDRESS:/tmp/
- echo "Create Ansible User and Group, provide root password"
- ssh root@$IPADDRESS 'su admin -l -c "cli -f /tmp/ansible_setup.cli"'
- echo "Create Ansible sudoers file, provide root password"
- ssh root@$IPADDRESS '/bin/bash /tmp/ansible_setup_sudoers.sh'
- echo "Copy ssh-key to Ansible user, provide ansible password"
- ssh-copy-id -i .ssh/id_rsa.pub ansible@$IPADDRESS
- echo "Applying the Sudo Permission"
- ssh root@$IPADDRESS 'su admin -l -c "cli -f /tmp/ansible_setup_sudop.cli"'
- echo "Delete setup files from Nodegrid, provide root password"
- ssh root@$IPADDRESS 'rm /tmp/ansible_setup*'
- read -p "Enter Your Choice 1(yes)/2(no): " choice
- done
2. ansible_setup.cli
- cd /settings/authorization/
- add
- set name=ansible
- commit
- cd ansible/profile/
- set track_system_information=yes
- set terminate_sessions=yes
- set software_upgrade_and_reboot_system=yes
- set configure_system=yes
- set configure_user_accounts=yes
- set apply_&_save_settings=yes
- set shell_access=yes
- set restrict_configure_system_permission_to_read_only=no
- set menu-driven_access_to_devices=no
- set custom_session_timeout=no
- set startup_application=shell
- commit
- cd /settings/local_accounts/
- add
- set username=ansible
- set password=1234
- set hash_format_password=no
- set user_group=ansible
- commit
3. ansible_setup_sudoers.sh
- #!/bin/bash
- echo "ansible ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/ansible
- chmod 600 /etc/sudoers.d/ansible
4. ansible_setup_sudop.cli
- set /settings/authorization/ansible/profile/ sudo_permission=yes
- commit
Now, run the script : sudo ./ansible_setup.sh
Enter root password for remote nodegrid when asked and ansible password is what one has given in ansible_setup.cli file