References:
Ansible 101
Ansible for DevOps Examples
Book:
Ansible for DevOps by Jeff Geerling
Ressources:
Jeff Geerling's Ansible Content
Blog | Jeff Geerling
ansible
python
vagrant
virtualbox
Vagrant
playbook.yml
ansible.cfg
vagrant init
subl Vagrantfile
config.vm.box = "geerlingguy/centos7"
vagrant up --provider=virtualbox
vagrant ssh-config
vagrant ssh / ssh vagrant@localhost:2222
ansible-playbook playbook.yaml
vagrant provision
vagrant status
vagrant halt
vagrant destroy
ansible multi -a "hostname"
ansible multi -a "hostname" -f 1 # -f 1 run in order with only 1 fork from ansible
ansible multi -a "df -h"
ansible multi -a "free -m"
ansible multi -a "date" -o # -o show results on one line.
ansible multi -b -m yum -a "name=chrony state=present" # chrony is a ntpd like server.
ansible multi -b -m service -a "name=chronyd state=started enabled=yes"
ansible multi -m shell -a "date" -o
ansible multi -b -a "chronyc tracking" # time sync
ansible app -b -m yum -a "name=python3-pip state=present"
ansible app -b -m pip -a "name=django<4 state=present"
ansible app -a "python -m django --version"
ansible db -b -m yum -a "name=mariadb-server state=present"
ansible db -b -m service -a "name=mariadb state=started enabled=yes"
ansible db -b -m yum -a "name=firewalld state=present"
ansible db -b -m service -a "name=firewalld state=started enabled=yes"
ansible db -b -m firewalld -a "zone=database state=present permanent=yes"
ansible db -b -m firewalld -a "source=192.168.60.0/24 zone=database state=enabled permanent=yes"
ansible db -b -m firewalld -a "port=3306/tcp zone=database state=enabled permanent=yes"
ansible db -b -m yum -a "name=python3-PyMySQL state=present"
ansible db -b -m mysql_user -a "name=django host=% password=12345 priv=.:ALL state=present"
ansible app -b -a "systemctl status chronyd"
ansible app -b -a "service chronyd restart" --limit "192.168.60.4"
ansible app -b -a "service ntpd restart" --limit ".4" # ip ending on .4
ansible app -b -a "service ntpd restart" --limit ~"..4" # regex search)
ansible app -b -m group -a "name=admin state=present"
ansible app -b -m user -a "name=johndoe group=admin createhome=yes"
ansible app -b -m user -a "name=johndoe state=absent remove=yes"
ansible app -b -m package -a "name=git state=present"
ansible multi -m stat -a "path=/etc/environment" # exists
ansible multi -m copy -a "src=/etc/hosts dest=/tmp/hosts" # copy
ansible multi -b -m fetch -a "src=/etc/hosts dest=/tmp" # fetch
ansible multi -m file -a "dest=/tmp/test mode=644 state=directory" # chmod
ansible multi -m file -a "src=/src/file dest=/dest/symlink state=link" # symbolic link
ansible multi -m file -a "dest=/tmp/test state=absent" # delete file
ansible multi -b -B 3600 -P 0 -a "yum -y update"
ansible multi -b -a "tail /var/log/messages"
ansible multi -b -m cron -a "name='daily-cron-all-servers' hour=4 job='/path/to/daily-script.sh'" # create job
ansible multi -b -m cron -a "name='daily-cron-all-servers' state=absent" # remove job
ansible multi -b -a 'crontab -l'
ansible app -b -m package -a "name=git state=present"
ansible app -b -m git -a "repo=git://example.com/path/to/repo.git dest=/opt/myapp update=yes version=1.2.4"
Easy reading with ansible ad-hoc commands
(Easy reading of results:
Use Ansible's YAML callback plugin for a better CLI experience
ansible.cfg
[Defaults]
# Use the YAML callback plugin.
stdout_callback = yaml
# Use the stdout_callback when running ad-hoc commands.
bin_ansible_callbacks = True
Pipelining, if supported by the connection plugin, reduces the number of network operations required to execute a module on the remote server, by executing many Ansible modules without actual file transfer. This can result in a very significant performance improvement when enabled. However this conflicts with privilege escalation (become).For example, when using ‘sudo:’ operations you must first disable ‘requiretty’ in /etc/sudoers on all managed hosts, which is why it is disabled by default. This option is disabled if ANSIBLE_KEEP_REMOTE_FILES is enabled.
ansible.cfg
[ssh_connection]
pipelining = True
ansible-playbook playbook.yml --limit xyz.example.com # limit to host xzy...
ansible-playbook playbook.yml --list-hosts # check hosts
ansible-playbook playbook.yml --user=johndoe
ansible-playbook playbook.yml --become --become-user=janedoe --ask-become-pass
--inventory=PATH (-i PATH): Define a custom inventory file.
--verbose (-v): Verbose mode -vvvv to give every minute detail.
--extra-vars=VARS (-e VARS): Define variables to be used in the playbook, in "key=value,key=value" format.
--forks=NUM (-f NUM): Number for forks (integer)
--connection=TYPE (-c TYPE): ssh or local..
--check: Run the playbook in Check Mode (‘Dry Run’); all tasks defined in the playbook will be checked against all hosts, but none will actually be run.
Ansible playbooks. All the examples in this chapter are in the Ansible for DevOps Examples