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

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
Salvo diversa indicazione, il contenuto di questa pagina è sotto licenza Creative Commons Attribution-ShareAlike 3.0 License