Ansible

Courses

Links

Syntax

Static Inventory and configure ansible machine

hosts file in /etc/ansible/hosts or with option -i hosts

[mygroup]
jenkinsec2.mycompany.com
jumpserver.mycompany.com

other possible configuration for hosts file
/etc/ansible/hosts

#this is the inventory for machine
shortname ansible_host=name.mydoamin.com

and run with

ansible shortname -m ping -u cloud_user

Inventory alias

you need to have in the inventory localhost

sudo vim /etc/ansible/hosts
ec2 ansible_host=myipaddress or dns

setup of ansible user

sudo useradd ansible
mkdir ~/.ssh
sudo su - ansible
echo "ssh-rsa my ansible public key ansible@ip" >> ~/.ssh/authorized_keys
chmod go-w ~/
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

Dynamic Inventory

https://docs.ansible.com/ansible/latest/user_guide/intro_dynamic_inventory.html#inventory-script-example-aws-ec2

you need to have these 2 files, the .py must be executable

ll ec2.*
-rw-rw-r-- 1 ansible ansible   471 Dec  4 09:40 ec2.ini
-rwxrwxr-x 1 ansible ansible 73130 Dec  3 14:23 ec2.py*

ls.yml

--- #only for testing
- hosts: all
  tasks:
  - shell: ls -l /home
    register: ls_contents
  - debug:
      msg: "LS output: {{ ls_contents }}"

how to apply

ansible-playbook -i ec2.py ls.yml
ansible all -i ec2.py -m shell -a "echo a"

Consul

Maybe I'll check in the future
https://www.consul.io/
https://pypi.org/project/ansible-dynamic-inventory/
https://docs.ansible.com/ansible/latest/modules/consul_module.html

Some commands and playbook

#print some ansible facts
ansible machine -m setup 

#start an httpd service
ansible machine -b -m service -a "name=httpd state=started"

#use not default playbook
ansible-playbook -i not-default-inventory myplaybook.yml 

#filter the ansible facts
ansible machine -m setup -a filter-*ipv4*

vim /home/ansible/git-setup.yml

--- # install git on target host
- hosts: workstation
  become: yes
  tasks:
  - name: install git
    yum:
      name: git
      state: latest

Save and exit the file (:wq in vim).

Run the playbook:

ansible-playbook -i /home/ansible/inventory /home/ansible/git-setup.yml

do a module ping of my machine

ansible all -i dynamic.py -m ping

AWX

my installation script

apt install software-properties-common
apt-add-repository ppa:ansible/ansible
apt update -y
apt upgrade -y 
apt install ansible -y
ansible --version
sudo adduser ansible
passwd -d ansible

apt install python-pip -y

pip install docker
apt install nodejs -y
apt install npm -y
service docker status

#apt install docker-compose #this will crash the system. 
pip install docker-compose

git clone https://github.com/ansible/awx.git
ansible-playbook -i inventory -e docker_registry_password=password install.yml

Tower Dynamic Inventory and multiple ssh keys

Custom Environment

  • Until you haven't active you don't see it in the web interface the option.

https://github.com/ansible/awx/blob/devel/docs/custom_virtualenvs.md

they said to use PATCH method to load but I'm not sure it is necessary probably it is enough enabled from the installation playbook.

A guy declare if you create the environments in the /var/lib/awx/venv/ansible/ you don't need to modify the playbook installation.

Run everything from the webconsole so you can see the error and fix it.

grep custom_venv_dir inventory
custom_venv_dir=/opt

ansible-playbook -i inventory install.yml --extra-vars "@venv_vars.yaml"

at this point with the http://localhost:8080/api/v2/config/
and at the end you will have the 

"custom_virtualenvs": [
       "/var/lib/awx/venv/ansible/",
       "/opt/custom-venvs/"
   ]
}

*

Salvo diversa indicazione, il contenuto di questa pagina è sotto licenza Creative Commons Attribution-ShareAlike 3.0 License