Coder Social home page Coder Social logo

ansible_tutorial's Introduction

Ansible Tutorial

Ansible configuraitons

  • ansible.cfg : basic configurations
  • inventory: inventory file to define server nodes

1. Run adhoc ping module

# Use full command without configuration file
ansible all --key-file ~/.ssh/id_rsa_ansible -i inventory -m ping
# Use configuration file ansible.cfg to save typing.
ansible all -m ping --extra-vars "ansible_user=ben"
  • all means use all hosts defined in inventory file.
  • --key-file specify ssh private key file
  • -i inventory, specify what server nodes the comand applies to
  • -m means module, here we are using module ping.
  • --extra-var specify extra variables passed to ansible.

If your current user name is different from ssh user name, then you need to speficy ansible_user via --extra-vars options. If your current user name is the same with ssh user name, then you don't need to specify ansible_user.

2. Run adhoc gather_facts module

# gather all nodes facts
ansible all -m gather_facts --extra-vars "ansible_user=ben"

# gather specific node facts
ansible all -m gather_facts --limit 192.168.0.61 --extra-vars "ansible_user=ben"

3. Run sudo apt update

ansible all -m apt -a update_cache=true --become --ask-become-pass --extra-vars="ansible_user=ben"

# install vim-nox
ansible all -m apt -a name=vim-nox --become --ask-become-pass

# update snapd to latest
ansible all -m apt -a "name=snapd state=latest" --become --ask-become-pass

# update all packages
asnible all -m apt -a "upgrade=dist" --become --ask-become-pass
  • -m apt to run apt command in Ubuntu
  • --become to add sudo permission
  • --ask-become-pass prompt for sudo password

4. Use playbook

ansible-playbook --ask-become-pass --extra-vars="anisble_user=ben" install_apache.yml

# run playbook with tags
ansible-playbook --tags web --ask-become-pass install_apache.yml


# run playbook without --ask-become-pass
# We can do this because in ansible.cfg, we use `remote_user` as simone
# and user simone is a sudoer, and no password is needed due to /etc/sudoers.d/simone
ansible-playbook site.yml

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.