Friday, April 16, 2021

Useful Ansible Ad-Hoc Commands

FILE/DIRECTORY


# copies a file to multiple severs in parallel
ansible all -m copy -a "src=/tmp/file dest=/tmp/file"

# changes file permission
ansible lab -m file -a "dest=/tmp/file mode=600 owner=bob group=wheel"

# creates a directory
ansible webservers -m file -a "dest=/path/to/c mode=755 owner=root group=mdehaan state=directory"

# deletes a directory
ansible lab -m file -a "dest=/tmp/dir state=absent"

Managing Packages:

# ensures a package is installed (don't update)
ansible lab -m yum -a "name=openssh state=present"

# ensure a package is installed to a specific version
ansible webservers -m yum -a "name=acme-1.5 state=present"

# ensure a package is on the latest version (update if lower version was found)
ansible lab -m yum -a "name=openssh state=latest"

# ensure a package is not installed (uninstalls it if needed)
ansible lab -m yum -a "name=bind state=absent"


Users and Groups


# ensures a this particular user exist
ansible webservers -m user -a "name=apache state=present"

# ensures a this particular user doesn't exist
ansible webservers -m user -a "name=hacker state=absent"


DEPLOYING FROM SOURCE CONTROL


# gets configuration from git
ansible dbservers -m git -a "repo=git://my.db.uk/repo.git dest=/var/lib/db version=HEAD"

Managing Services:

# ensures service is started
ansible webservers -m service -a "name=httpd state=started"

# restarts a service
ansible webservers -m service -a "name=httpd state=restarted"

# ensures a service is stopped
ansible webservers -m service -a "name=httpd state=stopped"


TIME LIMITED OPERATIONS


# executes a long running operation
ansible all -B 3600 -P 0 -a "/usr/bin/long_running_operation --do-stuff"
  * -B 3600 = timeout of 3600 seconds
  * -P 0 = no polling

# checks the status of the long running job
ansible web1.example.com -m async_status -a "jid=488359678239.2844"
  * jid = job id returned when command was ran in the background

# similar to the above but with polling
ansible all -B 1800 -P 60 -a "/usr/bin/long_running_operation --do-stuff"
  * -P 60 = polls every 60 seconds


GATHERING FACTS


# returns the settings of all target hosts
ansible all -m setup


SOURCES


ansible modules:
http://docs.ansible.com/ansible/list_of_all_modules.html

No comments:

Post a Comment