1. ansible版本
[root@kwemhisprc01468 sunny]# ansible --version
ansible [core 2.12.3]
config file = None
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.8/site-packages/ansible
ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/bin/ansible
python version = 3.8.5 (default, Nov 12 2021, 19:29:02) [GCC 9.3.0]
jinja version = 3.0.3
libyaml = True
2. ansible命令
(1) 指定inventory执行命令
inventory文件内容:
[group1]
10.244.79.247 ansible_ssh_user=test ansible_ssh_pass=Huawei@123
执行的命令:ansible -i /home/code/sunny/ansible-examples-master/sunny/hosts all -a "ls"
命令中的"all"是pattern, -i后跟的是inventory
[root@kwemhisprc01468 sunny]# ansible -i /home/code/sunny/ansible-examples-master/sunny/hosts all -a "ls"
[WARNING]: Platform linux on host 10.244.79.247 is using the discovered Python interpreter at /usr/local/bin/python3.9, but future installation
of another Python interpreter could change the meaning of that path. See https://docs.ansible.com/ansible-
core/2.12/reference_appendices/interpreter_discovery.html for more information.
10.244.79.247 | CHANGED | rc=0 >>
traefik
traefik-config.yaml
traefik-deploy.yaml
[root@kwemhisprc01468 sunny]# ansible -i /home/code/sunny/ansible-examples-master/sunny/hosts all -a "ls" --ask-pass
SSH password:
[WARNING]: Platform linux on host 10.244.79.247 is using the discovered Python interpreter at /usr/local/bin/python3.9, but future installation
of another Python interpreter could change the meaning of that path. See https://docs.ansible.com/ansible-
core/2.12/reference_appendices/interpreter_discovery.html for more information.
10.244.79.247 | CHANGED | rc=0 >>
traefik
traefik-config.yaml
(2) 简单的playbook
playbook内容:
---
- hosts: group1
vars:
http_port: 80
max_clients: 200
remote_user: test
tasks:
- name: enter etc directory
command: cd /home/test
- name: list all etc files
command: ls
执行结果:
[root@kwemhisprc01468 sunny]# ansible-playbook -i ./hosts firstPlay.yaml
PLAY [group1] ****************************************************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************************************************************************************************************************************************************
[WARNING]: Platform linux on host 10.244.79.247 is using the discovered Python interpreter at /usr/local/bin/python3.9, but future installation of another Python interpreter could change the meaning of that path. See https://docs.ansible.com/ansible-
core/2.12/reference_appendices/interpreter_discovery.html for more information.
ok: [10.244.79.247]
TASK [enter etc directory] ***************************************************************************************************************************************************************************************************************************************************
changed: [10.244.79.247]
TASK [list all etc files] ****************************************************************************************************************************************************************************************************************************************************
changed: [10.244.79.247]
PLAY RECAP *******************************************************************************************************************************************************************************************************************************************************************
10.244.79.247 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0