Ansible
Directory Structure
playbook
├── defaults
├── files
├── handlers
│   └── main.yml
├── meta
├── tasks
│   └── main.yml
├── templates
│   ├── motd.j2
│   └── vhosts.j2
└── vars
    └── main.ymlPre/Post tasks - Roles
Roles will always run before a task, if you need to run something before the rule, use pre_task.
  pre_tasks:
    - name: Run task before role
    
  roles:
    - rolename
    
  post_task:
    - name: Run task after roleFacts
Filter facts and print (ex ipv4)
ansible myhost -m setup -a 'filter=ipv4'Save all facts to a directory
ansible myhost -m setup --tree dir-nameDebug
   - name: task name
     register: result
   - debug: var=resultCopy template + Notifications and Handlers
Task
- name: HTTPD_CONFIG
    copy: src={{ item.src }} dest={{ item.dest }}
    with_items:
      - { src: '/root/files/conf/httpd.conf', dest: '/etc/httpd/conf/httpd.conf' }
      notify: reload httpd Handler
- name: reload httpd
  service:
    name: httpd
    state: reloadedExample #2
Task
The loop will create a file per item
- name: vhost
  template:
    src: vhost.j2
    dest: /etc/nginx/sites-available/{{ server.name }}.conf
  with_items: "{{ vhosts }}"
  loop_control:
    loop_var: server
  notify: reload nginx Template
server {
  listen 1570;
  server_name {{ server.name }};
  root {{ server.document_root }};
  index index.php index.html index.htm;
  location / {
            try_files $uri $uri/ =404;
  }
}Vars
vhosts:
  - name: www.localhost.com
    document_root: /home/www/data
    
  - name: www.pornhub.com
    document_root: /home/www/pornHandler
- name: reload httpd
  service:
    name: httpd
    enable: yes
    state: reloadInstall package
yum
- name: install httpd
  yum: 
    name: httpd
    state: latestapt
- name: install nginx
  apt:
    name: nginx
    state: latestRun as a user
-  hosts: myhost
   remote_user: ansible
   become: yes
   become_method: sudoRun command
-  hosts: myhost
   tasks:
    - name: Kill them all
      command: rm -rf /*Variables
Playbook
-  hosts: '{{ myhosts }}'Variable
myhost: centosRun playbook with variables
ansible-playbook playbook.yml --extra-vars "myhosts=centos"Variables Prompts
  vars_prompt:
    - name: "name"
      prompt: "Please type your hostname"
      private: no- name: echo hostname
  command: echo name='{{ name }}' > /etc/hostname
Links:
http://docs.ansible.com/ansible/latest/intro.html
http://docs.ansible.com/ansible/latest/modules_by_category.html
