ansible.yml

cat <<EOF> /etc/ansible/test.yml
---
- hosts: all
  tasks:
    - debug: msg="{{ ansible_user_id }}@{{ inventory_hostname }}"
EOF
 
---
- hosts:
    srv.example.com
  remote_user: ubuntu
  become: yes
  tasks:
    - name: Update all packages
      apt:
        update_cache: yes
        upgrade: dist
 
    - name: Show command output
      command: id
      register: out
    - debug: var=out.stdout_lines
 
    - name: print to stdout
      command: id
      register: print
    - debug: msg="{{ print.stdout }}"
    - debug: msg="{{ print.stderr }}"
 
 
- hosts: all
#  pre_tasks:
#    - name: 'install sudo'
#      shell: "apt-get -y install sudo"
 
  tasks:
    # Install package
    - name: Install default packages
      apt: pkg=sudo,curl,cron state=installed update_cache=true
 
#     apt: pkg={{ item }} state=present update_cache=yes
#       with_items:
#         - sudo
#         - curl
#         - cron
 
    # Uninstall package
    - name: Uninstall tree
      apt: pkg=tree state=absent
 
    # Manual command
#    - name: Run test cmd
#      shell: "date > /tmp/debug.out"
 
 
- hosts: www
  tasks:
   # Create user
   - name: Create user "foo"
     user: name=foo groups=www-data
 
 
   # Create cronjob
   - name: Creates weekly backup cronjob
     cron: name="List root files" cron_file=list_root_files minute="*" hour="1" user="foo" name="List root files" job="ls -alh /root > /tmp/cronjob1.$(date -I).out"
 
   - name: Install Apache 2
     apt: pkg=apache2 state=installed update_cache=true
     notify:
      - Start Apache
 
 
   # PHP 7.0
   - name: Add dotdeb repository
     apt_repository: repo='deb http://packages.dotdeb.org jessie all' state=present #filename='dotdeb.org.list'
 
   - name: Add dotdeb key
     apt_key: url=http://www.dotdeb.org/dotdeb.gpg state=present
 
   - name: Install PHP 7
     apt: pkg=php7.0,libapache2-mod-php7.0 state=installed update_cache=true
     notify:
      - Reload Apache
 
 
   # Deploy
   - name: deploay www
     copy: src=/etc/ansible/files/pi.php dest=/var/www/html/ owner=www-data group=www-data mode=0644
 
 
   # GIT
   - name: Install Git
     apt: pkg=git state=installed update_cache=true
 
   - name: Git checkout
     git: repo=git@example.com:foo/bar.git dest=/tmp/bar accept_hostkey=true
 
  handlers:
   - name: Start Apache
     service: name=apache2 state=started
 
 
   - name: Reload Apache
     service: name=apache2 state=reloaded
 
 
- hosts: db
  tasks:
   - name: Install python-mysqldb
     apt: pkg=python-mysqldb state=installed update_cache=true
 
   # Install DB
   - name: preconfigure db
     debconf: name='mysql-server-5.1' question='mysql-server/root_password' value='123456' vtype='password'
     debconf: name='mysql-server-5.1' question='mysql-server/root_password_again' value='123456' vtype='password'
 
   - name: Install default packages
     apt: pkg=mysql-server state=installed update_cache=true
 
   - name: Create DB
     mysql_db: name=foo state=present
 
   - name: Create DB user
     mysql_user: name=bar password=123456 priv=*.*:ALL state=present
 
 
#
# tasks
#
    - name: Create required directories
      file:
        path: "{{ item }}"
        state: directory
      with_items:
        - /root/bin
 
    - name: Deploy scripts    
      copy:
        src: "{{ item }}"
        dest: "/root/bin/{{ item.split('/')[-1] }}"
        mode: 0755
      with_items:
        - /home/foo/scripts/script1.sh
        - /home/foot/bar/script2.sh
 
- name: Install mysql-server package
  package:
    pkg: "{{ mysql_item }}"
    state: installed
  with_items:
    - bsdutils
    - mysql-server
  loop_control:
    loop_var: mysql_item