Ansible - yszheda/wiki GitHub Wiki
localhost
sudo
config
with_items
APT
- http://docs.ansible.com/ansible/apt_module.html
- http://docs.ansible.com/ansible/apt_repository_module.html
- Introduction to Ansible APT Package and Repository
- How to install a list of packages with Ansible
- https://stackoverflow.com/questions/41535838/how-to-run-apt-update-and-upgrade-via-ansible-shell
Multiple commands
- command: "{{ item }} chdir=/src/package/"
with_items:
- ./configure
- /usr/bin/make
- /usr/bin/make install
key: |
This text
has multiple
lines
- https://stackoverflow.com/questions/24851575/ansible-how-to-pass-multiple-commands
- https://stackoverflow.com/questions/40230184/how-to-do-multiline-shell-script-in-ansible
copy
- copy:
src: "{{ role_path }}/files/foo.conf"
dest: /etc/foo.conf
- name: Your copy task
copy: src={{ item.src }} dest={{ item.dest }}
with_items:
- { src: 'containerizers', dest: '/etc/mesos/containerizers' }
- { src: 'another_file', dest: '/etc/somewhere' }
- { src: 'dynamic', dest: '{{ var_path }}' }
# more files here
- http://docs.ansible.com/ansible/copy_module.html
- How to Copy Files and Directories in Ansible Using Copy and Fetch Modules
- How to copy files with ansible relatively to the role?
- Copy multiple files with Ansible
run_once
- http://docs.ansible.com/ansible/playbooks_delegation.html#run-once
- https://stackoverflow.com/questions/22070232/how-to-get-an-ansible-check-to-run-only-once-in-a-playbook
Condition
Check file/dir stat
tasks:
- name: Check that the somefile.conf exists
stat:
path: /etc/file.txt
register: stat_result
- name: Create the file, if it doesnt exist already
file:
path: /etc/file.txt
state: touch
when: stat_result.stat.exists == False
- http://docs.ansible.com/ansible/stat_module.html
- https://stackoverflow.com/questions/35021384/how-can-i-run-a-ansible-task-only-if-a-file-or-directory-does-not-exist
- https://stackoverflow.com/questions/35654286/how-check-a-file-exists-in-ansible
- Ansible - Only if a file exists or does not exist
Multiple conditions
Check process
- name: Check if Apache is running
shell: ps aux | grep apache2 | grep -v grep
ignore_errors: yes
changed_when: false
register: service_apache_status
- name: Report status of Apache
fail:
msg: |
Service apache2 is not running.
Return code from `grep`:
{{ service_apache_status.rc }}
when: service_apache_status.rc != 0
- https://superuser.com/questions/1084129/ansible-task-to-confirm-if-a-process-is-running
- https://serverfault.com/questions/736820/ansible-playbook-ensure-a-process-is-running-on-any-one-node-of-a-cluster
user
Examples
- https://github.com/ansible/ansible-examples
- https://github.com/geerlingguy/ansible-vagrant-examples
- https://github.com/yukimemi/ansible-playbook
docker
Trouble-shooting
Failed to connect to the host via ssh
# wrong
# 172.17.0.2 ansible_connection=ssh ansible_port=32768 ansible_ssh_pass=pass ansible_ssh_user=root
# correct
localhost:32768 ansible_connection=ssh ansible_ssh_pass=pass ansible_user=root