Skip to content

Latest commit

 

History

History
101 lines (78 loc) · 1.92 KB

20171108.md

File metadata and controls

101 lines (78 loc) · 1.92 KB

Example

Setup: Master: Linux archlinux 4.9.48-1-lts Node: Linux ubuntu 4.4.0-62-generic

#/etc/ansible/hosts
localhost

[myservers]
192.168.1.122
#~/sample_playbook.yml
- hosts: 192.168.1.122
  tasks:
    - name: Create a file
      file:
        path: /tmp/shurelous
        state: touch
      tags:
        - test
    - name: Save network data
      script: /sbin/ifconfig > /tmp/shurelous2
      args:
        creates: /tmp/shurelous2
      tags:
        - test
    - name: Stop apache service
      become: true
      become_user: root
      systemd:
        name: apache2
        state: stopped
      tags:
        - stop
    - name: Start apache service
      become: true
      become_user: root
      systemd:
        name: apache2
        state: started
      tags:
        - deploy

How to play

  • Run all tasks:
ansible-playbook sample_playbook.yml
  • Ask for the remote host sudo password and run all tasks:
ansible-playbook sample_playbook.yml -K
  • Run only the tasks with a given tag:
ansible-playbook sample_playbook.yml --tags test
ansible-playbook sample_playbook.yml --tags test,stop
  • List all modules in the system:
ansible-doc -l

IMPORTANT:

  • You should authorize your local ssh key in all your nodes to avoid complex settings:
// copy your ssh key
cat ~/.ssh/id_rsa.pub

// access your node
ssh [email protected]

// authorize your key to access the node
vim ~/.ssh/authorized_keys // paste

Reference