/etc/ansible/ansible.cfg
ansible all -m ping
ansible all -m ping -s -k -u vagrant
-m for module -s for sudo -k for ask password -u use vagrant as a username in remote machines
--- - hosts: local tasks: - name: Install Nginx apt: pkg=nginx state=installed
--- - hosts: local tasks: - name: Install nginx apt: pkg=nginx state=installed notify: - Start Nginx handlers: - name: Start Nginx service: name=nginx state =installed
--- - hosts: local vars: - docroot: /var/www/serversforhackers.com/public tasks: - name: Add Nginx repository apt_repository: repo='ppa:nginx/stable' state=present register: ppastable - name: Install Nginx apt: pkg=installed state=installed update_cache=true when: ppastable|success register: nginxinstalled notify: - Start Nginx
In above playbook, there is a task to add nginx repo. If it succeeds, then it registers it to ppastable. When the next task is taken to install nginx, the success event of task with regitery ppastable
is checked.
Note:: With multiple tasks inside a playbook, a playbook can become difficult to manage and there are no scope connects. Based on the learning above, we have following important sections in an ansible action –
Each of the above can beocme a sirectory haing a main.yml file in them. the complete structure then is referred to as a role. A role will have similiars tasks under its hood and is managed by seperating all the components used in the ansible playbook.
Put all your handlers for nginx ansible in handlers/main.yml
--- - name: Start Nginx service: name=nginx state=started - name: Reload Nginx service: name=nginx state=reloaded
The above can be referenced from a central main file when required.
See a sample templated file which is generally based on Python Jinja templates/nginx-config.conf
server { listen 80 default_server; server_name *.{{ '{{' }} domain {{ '}}' }}; return 301 https://{{ '{{' }} domain '}}' }}$request_uri; } server { listen 443 default_server ssl; root /var/www/{{ '{{' }} domain {{ '}}'/public; index index.html index.htm index.php
vars/main.yml
--- domain: something.com ssl_key: /etc/ssl/sfh/sfh.key server_name: someone.something.com
roles_path
variable in ansible.cfg file say it to /home/user/ansible/roles--- - hosts: all roles: - nginx
where nginx is the name of your role and is a directory in roles_path
with_items
, one can mention a list of pakacges to be installed.- name: install default packages apt: pkg={{ item }} state=installed with_items: - aptitude - vim - supervisor
--- - name: create directroy under /home/aashay file: path=/home/aashay/test-dir state=directory mode=755 - name: Git checkout from github git: repo=https://github.com/ashay-maheshwari/Linux-stuff.git dest=/home/aashay/.test-dir - name: Drop config fro supervisord into conf.d directory copy: src=supervisor-test-.conf dest=/etc/supervisor/conf.d/supervisor-test.conf