基本概念-术语

https://docs.ansible.com/ansible/latest/user_guide/basic_concepts.html

pip 安装

ansible config: https://docs.ansible.com/ansible/latest/reference_appendices/config.html#ansible-configuration-settings

  1. mkdir /etc/ansible
  2. cd /etc/ansible
  3. curl -O https://raw.githubusercontent.com/ansible/ansible/devel/examples/ansible.cfg
  1. root@DESKTOP-BACQ0O3:/etc/ansible# ansible --version
  2. ansible [core 2.11.2]
  3. config file = /etc/ansible/ansible.cfg
  4. configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  5. ansible python module location = /usr/local/lib/python3.8/dist-packages/ansible
  6. ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
  7. executable location = /usr/local/bin/ansible
  8. python version = 3.8.10 (default, Jun 2 2021, 10:49:15) [GCC 9.4.0]
  9. jinja version = 2.10.1
  10. libyaml = True

配置 Managed nodes

  1. vim vim /etc/ansible/hosts
  1. [own_machine_group]
  2. 111.230.58.139:ssh_port

Control node 公钥上传至 Managed nodes

  1. ssh-copy-id -i ~/.ssh/id_rsa.pub root@111.230.58.139 -p ssh_port

判断连通性

  1. root@DESKTOP-BACQ0O3:/etc/ansible# ansible own_machine_group -m ping
  2. 111.230.58.139 | SUCCESS => {
  3. "ansible_facts": {
  4. "discovered_interpreter_python": "/usr/bin/python"
  5. },
  6. "changed": false,
  7. "ping": "pong"
  8. }

Playbook Demo

  1. root@DESKTOP-BACQ0O3:/etc/ansible# cat>mytask.yaml<<EOF
  2. > ---
  3. > - name: My task
  4. > hosts: all
  5. > tasks:
  6. > - name: Leaving a mark
  7. > command: "touch /tmp/ansible_was_here"
  8. > EOF
  9. root@DESKTOP-BACQ0O3:/etc/ansible# cat mytask.yaml
  10. ---
  11. - name: My task
  12. hosts: all
  13. tasks:
  14. - name: Leaving a mark
  15. command: "touch /tmp/ansible_was_here"
  16. root@DESKTOP-BACQ0O3:/etc/ansible# ansible-playbook mytask.yaml
  17. PLAY [My task] *********************************************************************************************************
  18. TASK [Gathering Facts] *************************************************************************************************
  19. ok: [111.230.58.139]
  20. TASK [Leaving a mark] **************************************************************************************************
  21. changed: [111.230.58.139]
  22. PLAY RECAP *************************************************************************************************************
  23. 111.230.58.139 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0