Difference between revisions of "Ansible Setup"

From rbachwiki
Jump to navigation Jump to search
(Created page with "* Download and install Miniconda: curl -OL https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh bash Miniconda3-latest-Linux-x86_64.sh **You will be prompted several times during the installation process. Review the terms and conditions and select “yes” for each prompt. *Restart your shell session for the changes to your PATH to take effect. exec bash -l *Create a new virtual environment for Ansible: conda create -n ansible-dev python=3 *Acti...")
 
(14 intermediate revisions by the same user not shown)
Line 1: Line 1:
* Download and install Miniconda:
<html>
  curl -OL https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh
<style>
  bash Miniconda3-latest-Linux-x86_64.sh
.optional{ background: rgb(182, 209, 252);padding: 1rem;}
</style>
<div class="optional">
<h3> Optional if you want to install a virtual environment, but not required</h3>
<p> **Download and install Miniconda:</p>
  <code>curl -OL https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh
  bash Miniconda3-latest-Linux-x86_64.sh</code>


**You will be prompted several times during the installation process. Review the terms and conditions and select “yes” for each prompt.
<p>**You will be prompted several times during the installation process. Review the terms and conditions and select “yes” for each prompt.</p>
*Restart your shell session for the changes to your PATH to take effect.
<p>*Restart your shell session for the changes to your PATH to take effect.</p>


  exec bash -l
  <code>exec bash -l</code>


*Create a new virtual environment for Ansible:
<p>*Create a new virtual environment for Ansible:</p>


  conda create -n ansible-dev python=3
  <code>conda create -n ansible-dev python=3</code>
*Activate the new environment:
<p>*Activate the new environment:</p>


  conda activate ansible-dev
  <code>conda activate ansible-dev</code>


*Check your Python version:
<p>*Check your Python version:</p>


  python --version
  <code>python --version</code>
</div>
</html>


'''Ubuntu 18.04'''
'''Ubuntu 18.04'''
Line 25: Line 33:
  sudo apt-add-repository --yes --update ppa:ansible/ansible
  sudo apt-add-repository --yes --update ppa:ansible/ansible
  sudo apt install ansible
  sudo apt install ansible
ansible-galaxy collection install community.general


*Verify that Ansible is installed:
*Verify that Ansible is installed:
Line 42: Line 51:
*Add your nodes to the default inventory file.
*Add your nodes to the default inventory file.


<code>File: /etc/ansible/hosts</code>
File: /etc/ansible/hosts
  [nginx]
  203.0.113.0
'''host file with alias for server addresses'''
* so you can use 'server1' in your ansible file to refer to 192.168.1.1
[webserver]
server1 ansible_ssh_host=192.168.1.1
[fileserver]
server2 ansible_ssh_host=192.168.2.2
 
'''Host file with just ip addresses'''
  [webserver]
  192.168.20.222
   
   
  [wordpress]
  [fileservers]
  203.0.113.1
  192.168.1.1


'''Each bracketed label denotes an Ansible group . Grouping your nodes by function will make it easier to run commands against the correct set of nodes.'''
'''Each bracketed label denotes an Ansible group . Grouping your nodes by function will make it easier to run commands against the correct set of nodes.'''
Line 79: Line 98:
         name: nginx
         name: nginx
         state: latest
         state: latest
=[[Ansible| Ansible Menu]]=
[[Category:Ansible]]

Revision as of 15:23, 24 June 2022

Optional if you want to install a virtual environment, but not required

**Download and install Miniconda:

curl -OL https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh bash Miniconda3-latest-Linux-x86_64.sh

**You will be prompted several times during the installation process. Review the terms and conditions and select “yes” for each prompt.

*Restart your shell session for the changes to your PATH to take effect.

exec bash -l

*Create a new virtual environment for Ansible:

conda create -n ansible-dev python=3

*Activate the new environment:

conda activate ansible-dev

*Check your Python version:

python --version

Ubuntu 18.04

sudo apt update
sudo apt install software-properties-common
sudo apt-add-repository --yes --update ppa:ansible/ansible
sudo apt install ansible
ansible-galaxy collection install community.general
  • Verify that Ansible is installed:
ansible --version

Configure Ansible

By default, Ansible’s configuration file location is /etc/ansible/ansible.cfg. In most cases, the default configurations are enough to get you started using Ansible. In this example, you will use Ansible’s default configurations.

  • To view a list of all current configs available to your control node, use the ansible-config command line utility.
ansible-config list

Create an Ansible Inventory

Ansible keeps track of its managed nodes using an inventory file located in /etc/ansible/hosts. In the inventory file, you can group your managed nodes and use these groups to target specific hosts that make up your infrastructure

  • Add your nodes to the default inventory file.
File: /etc/ansible/hosts

host file with alias for server addresses

  • so you can use 'server1' in your ansible file to refer to 192.168.1.1
[webserver]
server1 ansible_ssh_host=192.168.1.1

[fileserver]
server2 ansible_ssh_host=192.168.2.2

Host file with just ip addresses

[webserver]
192.168.20.222

[fileservers]
192.168.1.1

Each bracketed label denotes an Ansible group . Grouping your nodes by function will make it easier to run commands against the correct set of nodes.

Note

The /etc/ansible directory will not exist by default in some environments. If you find that this is the case, create it manually with the following command:

mkdir /etc/ansible/

If you are using a non-standard SSH port on your nodes, include the port after a colon on the same line within your hosts file (203.0.113.1:2222).

Test connection to server

ansible all -u root -m ping --private-key ~/.ssh/ansible

Create a playbook

nano server.yml
---                                                                                 ---
- hosts: webserver
 tasks:
   - name: install apache
     package:
       name: apache2
       state: latest
- hosts: fileserver
 tasks:
   - name: install nginx
     package:
       name: nginx
       state: latest


Ansible Menu