Ansible will be the latest version supporting current Python in OS. So to get latest Ansible , Python must be updated as well!
Warning
Do not update the default Python in OS - it is used by system services, which may break!
Build Python:
sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libsqlite3-dev libreadline-dev libffi-dev curl libbz2-dev
wget https://www.python.org/ftp/python/3.9.15/Python-3.9.15.tgz
tar -xf Python-3.9.15.tgz
cd Python-3.9.15
./configure --enable-optimizations
make -j 8
Installing Python:
Best way is to do checkinstall, create a .deb file to share with the team.
Use altinstall parameter to install Python in an alternative path. This is simpler & better than using Python virtualenv.
sudo make altinstall # now Python 3.9x is installed on separate path, while Python 3.7 in OS is unchangedsudo python3.9 -m pip install --upgrade pip
sudo python3.9 -m pip install ansible
ansible --version # now ansible is the latest version
---- name:front-end playhosts:allgather_facts:yesbecome:yestasks:- name:include role apacheinclude_role:name:apache- name:include role phpinclude_role:name:php...
apache / tasks / main.yml - tasks example:
---- name:install apacheapt:name:apache2- name:Enable service apache2ansible.builtin.systemd:name:apache2enabled:yesmasked:no- name:Make sure apache2 is runningansible.builtin.systemd:state:startedname:apache2...
Role structure
Directories inside a role:
- defaults - variable values by default
-- main.yml
- vars - variables defined by role (for other roles)
-- main.yml
- tasks - jobs to be completed
-- main.yml
- handlers - actions to be taken after checks
-- main.yml
- files - static files to be copied into client machine
- templates
ansible-doc -l # all installed modules listansible-doc <module-name> # module manansible-doc -s <module-name> # playbook snippets code examples on module
Module examples
Copy module
Copies a file from the local box to a remote system - useful for copying config files to remote system
can do backups
can do remote validation
Fetch module
Copy a file from remote system to local box
validate file using md5 checksum
Setup module
Gather info and facts on remote system
Inventory analysis of the system
ansible -i inventory web1 -m setup # gather all available system infoansible -i inventory web1 -m setup -a "filter=ansible_eth*"# gather info on NICsansible -i inventory all -m setup --tree ./setup # form an inventory of files in /setup/ folder with info on targeted systems
ansible webservers -i inventory -m yum -a "name=httpd state=present" -u vagrant --sudo
# name - name of package (Apache)# present - if package is not there, install. If it is there - do nothing and report "changed: false" (idempotence test)
Service module
Start/stop/restart services
Set autostart option for services
ansible webservers -i inventory -m service -a "name=httpd state=started enabled=yes" -u vagrant --sudo
# name - name of service (Apache)# state = started/stopped - make idempotence test and change if necessary# enabled = yes/no - autostart on system boot
# Tasks in a playbook are executed top down. Tasks use modules.tasks:- name:Name the task for readabilitymodule:parameters=go_here# Example:- name:Deploy Apache Configuration Filecopy:src=../../ansible/files/configuration/httpd.confdest=/etc/httpd/conf/
---# -------- Global play declaration- hosts:webservers ## ----- Variables per playvars:git_repo:https://github.com/repo.githttp_port:8081db_name:wordpress## ------------------------### ---- Declare user to run taskssudo:yessudo_user:wordpress_user### ------------------------------gather_facts:no# dont't gather facts with SETUP module (default gathers facts - expensive in time)remote_user:roottasks:# --------------------------------- name:Install Apacheyum:name=httpd state=present- name:Start Apacheservice:name=httpd state=started
Including files
Use “- include” and “- include_vars” directives to include playbook files:
Check output of previous task as condition to run next task:
tasks:- name:Stop iptables nowservice:name=iptables state=stoppedregister:resultignore_errors:yes# supress default stop on error- debug:msg="Failure!"when:result|failed # Debug message will only be shown if task has failed# other conditions are "result|success", "result|skipped"
Checking variables with WHEN condition
Bring variable check to BOOL check:
- name:"test"hosts:localvars_prompt:- name:"os_type"prompt:"What OS? (centos or ubuntu)"default:"centos"private:novars:- is_ubuntu:"{{os_type == 'ubuntu'}}"- is_debian:"{{os_type == 'debian'}}"tasks:- debug:msg="this shows the conditional if variable equals ubuntu"when:is_ubuntu|bool- debug:msg="this shows the conditional if variable equals centos"when:is_centos|bool