ansible使用日记

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   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值