基本概念-术语
https://docs.ansible.com/ansible/latest/user_guide/basic_concepts.html
pip 安装
- pip 方式安装无配置文件 config file,可自行根据官方给的例子创建,https://docs.ansible.com/ansible/latest/installation_guide/intro_configuration.html
ansible config: https://docs.ansible.com/ansible/latest/reference_appendices/config.html#ansible-configuration-settings
mkdir /etc/ansible
cd /etc/ansible
curl -O https://raw.githubusercontent.com/ansible/ansible/devel/examples/ansible.cfg
root@DESKTOP-BACQ0O3:/etc/ansible# ansible --version
ansible [core 2.11.2]
config file = /etc/ansible/ansible.cfg
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/lib/python3.8/dist-packages/ansible
ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/local/bin/ansible
python version = 3.8.10 (default, Jun 2 2021, 10:49:15) [GCC 9.4.0]
jinja version = 2.10.1
libyaml = True
配置 Managed nodes
vim vim /etc/ansible/hosts
[own_machine_group]
111.230.58.139:ssh_port
将 Control node 公钥上传至 Managed nodes
ssh-copy-id -i ~/.ssh/id_rsa.pub root@111.230.58.139 -p ssh_port
判断连通性
root@DESKTOP-BACQ0O3:/etc/ansible# ansible own_machine_group -m ping
111.230.58.139 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
Playbook Demo
root@DESKTOP-BACQ0O3:/etc/ansible# cat>mytask.yaml<<EOF
> ---
> - name: My task
> hosts: all
> tasks:
> - name: Leaving a mark
> command: "touch /tmp/ansible_was_here"
> EOF
root@DESKTOP-BACQ0O3:/etc/ansible# cat mytask.yaml
---
- name: My task
hosts: all
tasks:
- name: Leaving a mark
command: "touch /tmp/ansible_was_here"
root@DESKTOP-BACQ0O3:/etc/ansible# ansible-playbook mytask.yaml
PLAY [My task] *********************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************
ok: [111.230.58.139]
TASK [Leaving a mark] **************************************************************************************************
changed: [111.230.58.139]
PLAY RECAP *************************************************************************************************************
111.230.58.139 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0