Skip to content

Instantly share code, notes, and snippets.

@positiveque
Forked from pareddy113/ansible commands
Created August 28, 2020 10:13
Show Gist options
  • Select an option

  • Save positiveque/b201390f8220afc1519748c23ac64cdf to your computer and use it in GitHub Desktop.

Select an option

Save positiveque/b201390f8220afc1519748c23ac64cdf to your computer and use it in GitHub Desktop.

Revisions

  1. @pareddy113 pareddy113 created this gist Nov 2, 2017.
    78 changes: 78 additions & 0 deletions ansible commands
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,78 @@

    -i :inventory file
    -u list_of_server :username for remote login for the list_of_servers
    -m :runs the moudle name and gives the output
    -a :shell arguments to run on the remote machine ex: ls, docker ps, etc
    -f : specify the number of parallel process to run\
    -B : run asynchronously, failing after X


    Modules:
    ping
    shell
    copy


    Ansible:
    ansible atlanta -a "/usr/bin/foo" -u username
    ansible -u ec2-user us-east-1 -m ping
    ansible -i ec2.py -u ec2-user us-east-1 -m ping
    ansible -i ec2.py -u ec2-user tag_Zone_YELLOW -m ping

    ansible <pattern_goes_here> -m <module_name> -a <arguments>
    ansible webservers -m service -a "name=httpd state=restarted"
    <pattern_goes_here>: all, *, webservers:dbservers(: specifies 'or'), webservers:&staging (& specifies and), webservers:!phoenix (exclude phoenix), or combine or, and and not
    *.example.com, *.com, webservers[0] ,webservers[-1] , ~(web|db).*\.example\.com

    Run in parallel:
    ansible atlanta -a "/sbin/reboot" -f 10

    Copy files:
    ansible atlanta -m copy -a "src=/etc/hosts dest=/tmp/hosts"
    ansible raleigh -m shell -a 'echo $TERM'

    Change permissions or groups of files:
    ansible webservers -m file -a "dest=/srv/foo/a.txt mode=600"
    ansible webservers -m file -a "dest=/srv/foo/b.txt mode=600 owner=mdehaan group=mdehaan"
    ansible webservers -m file -a "dest=/path/to/c mode=755 owner=mdehaan group=mdehaan state=directory"

    Install packages:
    ansible webservers -m yum -a "name=acme state=present"
    ansible webservers -m yum -a "name=acme-1.5 state=present"
    ansible webservers -m yum -a "name=acme state=latest"
    ansible webservers -m yum -a "name=acme state=absent"

    Users and Groups:
    The ‘user’ module allows easy creation and manipulation of existing user accounts, as well as removal of user accounts that may exist:
    ansible all -m user -a "name=foo password=<crypted password here>"
    ansible all -m user -a "name=foo state=absent"

    Deploying from Source Control:
    ansible webservers -m git -a "repo=https://foo.example.org/repo.git dest=/srv/myapp version=HEAD"

    Managing services:
    ansible webservers -m service -a "name=httpd state=started"
    ansible webservers -m service -a "name=httpd state=restarted"
    ansible webservers -m service -a "name=httpd state=stopped"

    Time limited background operations:
    ansible all -B 3600 -P 0 -a "/usr/bin/long_running_operation --do-stuff"
    ansible web1.example.com -m async_status -a "jid=488359678239.2844"
    ansible all -B 1800 -P 60 -a "/usr/bin/long_running_operation --do-stuff"

    Configuration(ansible.cfg):
    * ANSIBLE_CONFIG (an environment variable)
    * ansible.cfg (in the current directory)
    * .ansible.cfg (in the home directory)
    * /etc/ansible/ansible.cfg



    ./ec2.py --list
    ./ec2.py --host 10.10.1.149


    Playbook:
    ansible-playbook -i ec2.py -u ec2-user dynamicInventoryFilter.yml
    ansible-playbook site.yml --limit datacenter2
    ible-playbook site.yml --limit @retry_hosts.txt