Ansible Essentials: rock with yaml
visual code extensions
sudo apt install ansible-lint
- https://marketplace.visualstudio.com/items?itemName=zbr.vscode-ansible
- https://github.com/codota/TabNine
books
Vincent Sesto - Practical Ansible_ Configuration Management from Start to Finish-Apress (2021)
2. use ansible to run commands over your server
configuration
# inventory
[App2]
127.0.0.1:2222
[App2:vars]
ansible_connection=ssh
ansible_user=test
ansible_ssh_pass=test
ansible_sudo_pass=test
# test_python_script.py
import sys
print(sys.version)
print("yingshaoxo")
# templates/my_bash_script.sh
echo "{{ word }}"
# test.yml
---
- name: Set Up Python
hosts: App2
vars:
script_path: test_python_script.py
handlers:
- name: run our script
command: "python3.9 /tmp/script.py"
tasks:
- name: install latest python
remote_user: test
become: true # become root
apt: name=python3.9 state=latest
- name: copy our test_python_script.py file to the server
copy: |
src="{{ script_path }}"
dest=/tmp/script.py
notify: # this will only be called when test_python_script.py content get changed
- run our script
- name: Use template to run bash script
hosts: App2
vars:
word: great yingshaoxo
tasks:
- name: render our template and put it into our server
remote_user: test
become: true # become root
template:
src: templates/my_bash_script.sh
dest: "/tmp/script.sh"
mode: "777"
owner: root
group: root
- name: run the bash script
remote_user: test
command: "sh /tmp/script.sh"
usage
ansible-playbook -i inventory test.yml -v
tips
- -i: inventory file; hosts
- -a: arguments; ansible module arguments
- -e: extra vars(variables); it has top priority, it will override anything else.
Example: ansible-playbook release.yml —extra-vars “@some_file.json”