由 CyanHall.com
创建于 2020-11-12,
上次更新:2021-04-30。
👉 如果有用请点赞。 Ansible 是一个自动化工具.
👉 如果有用请点赞。 Ansible 是一个自动化工具.
1. 安装 Ansible
python -m virtualenv ansible # Create a virtualenv if one does not already exist
source ansible/bin/activate # Activate the virtual environment
pip install ansible
3. Playbook
# playbook.yml
- name: Playbook Example
hosts: Prod
tasks:
- name: Pull GitHub Repo (with SSH ForwardAgent enabled)
git:
repo: '[email protected]:xxx/xxx.git'
dest: /dest/path
- name: Run shell command in specific directory
shell: npm install
args:
chdir: /git-repo/path
- name: Sync Files
synchronize:
src: source/path
dest: /source/path
- name: Mange supervisor processs
supervisorctl: name=[process-name] state=[start|stopped]
- name: Update nginx config
template: src=templates/nginx_conf.j2
dest=/etc/nginx/conf.d/example.conf
- name: Reload nginx
service: name=nginx state=reloaded
5. 运行 playbook
ansible-playbook -i path-to-my-inventory playbook.yml
2. 配置/环境
# Default location: /etc/ansible/hosts.
# Use -i <path> to specify a different inventory file
Prod ansible_ssh_user=[username] ansible_ssh_host=[ip_address] ansible_ssh_port=22 ansible_ssh_private_key_file=[ssh-key-file-path]
4. ansible.cfg
# lookup order:
#1. File specified by the ANSIBLE_CONFIG environment variable
#2. ./ansible.cfg (current directory)
#3. ~/.ansible.cfg (home directory)
#4. /etc/ansible/ansible.cfg
[defaults]
transport = ssh
log_path=ansible.log
[ssh_connection]
ssh_args = -o ForwardAgent=yes -o IdentitiesOnly=yes -o ControlMaster=auto -o ControlPersist=4h -o ControlPath=~/.ssh/%h-%p-%r
6. 开启 SSH 转发代理
# ~/.ssh/config
Host [server-address-here] [ip-address-here]
ForwardAgent yes
更多