Skip to main content

Command Palette

Search for a command to run...

Ansible setup with root user

Published
2 min read

you might know the required installation packages to use ansible.

After installation is done follow below steps:

  • Login as a root user

      sudo -i
    
  • create a password for root.

      passwd root
    
  • root user have all permissions by-default so we dont need to edit visudo.

  • edit the sshd_config under /etc/ssh/sshd_config

  • Navigate to the 63rd line of the file and modify 'PasswordAuthentication yes'.

  • Navigate to the 38th line of the file and uncomment "PermitRootLogin yes" to login root.

      vim /etc/ssh/sshd_config
    
      63. PasswordAuthentication yes
      38. PermitRootLogin yes
    
  • Restart sshd

      [root@ip-172-31-80-244 ~] systemctl restart sshd
    

    I've created two more instances as slaves here.

    Slave-1 - 54.86.33.245

  • Slave-2 - 54.87.131.102

  • Perform the same steps on all the slaves, but omit the installation step.

To establish an SSH connection between the master and slaves, we need a PEM file. However, I do not have one.

Instead, we can generate an SSH key pair on the master server and copy the public key to the slaves.

Visit this link . --> do required operation in root user.

  • by-default inventory is commented so the host's file is not get executed so we have to uncomment it.

  • Uncomment the inventory in the Ansible configuration file located at /etc/ansible.

      vim /etc/ansible/ansible.cfg
      #inventory = /etc/ansible/hosts
    
  • Add slaves in the hosts file located at /etc/ansible

      vim /etc/ansible/hosts
    
      [slave-1]
      54.86.33.245
    
      [slave-2]
      54.87.131.102
    

    Playbooks

  • we are creating a playbook file one.yaml.

  • but we are not using "user" and "become" under the target's section, because we using ansible with the root user so we don't need to add them.

  • action can be performed in various ways:

    • action: yum name=<<package_Name>> state=present/absent/latest

    • yum: name=<<package_Name>> state=present/absent/latest

    • command: --> we can directly execute command

        ---
        - hosts: slave-1
          connection: ssh
      
          tasks:
            - name: installing tree in slave-1
              action: yum name=tree state=present
        ...
        ~
      
        ---
        - hosts: slave-1
          connection: ssh
      
          tasks:
            - name: installing tree in slave-1
              yum: name=tree state=present
        ...
      
        ---
        - hosts: slave-1
          connection: ssh
      
          tasks:
            - name: installing tree in slave-1
              yum:
                name: tree
                state: present
        ...
      
        ---
        - hosts: all
          connection: ssh
      
          tasks:
            - name: installing tree in slave-1
              command: yum remove tree -y
        ...
      

More from this blog

Untitled Publication

12 posts