啓動vagrant虛擬機nginx
vagrant up
而後在當前目錄添加ansible.cfg文件:shell
[defaults] hostfile = hosts remote_user = vagrant private_key_file = .vagrant/machines/default/virtualbox/private_key host_key_checking = False
在當前目錄添加hosts文件,匹配上面的ansible.cfg文件裏面的hosts文件名緩存
testserver ansible_ssh_host=127.0.0.1 ansible_ssh_port=2222
執行ansible命令:
ssh
$ ansible testserver -m #執行ping模塊 $ ansible testserver -m command -a uptime #執行uptime命令 $ ansible testserver -a uptime # command 模塊很經常使用,能夠默認 $ ansible testserver -a "tail /var/log/dmesg" # 執行tail命令,可是會失敗 $ ansible testserver -s -a "tail /var/log/dmesg" # 使用root權限執行命令 $ ansible testserver -s -m apt -a name=nginx # 使用root權限運行apt命令安裝nginx $ ansible testserver -s -m apt -a update_cache=yes # apt更新軟件包緩存
好像有個問題:spa
$ ansible testserver -s -m service -a name=nginx state=restarted
而後報了個錯誤:vagrant
ERROR! Missing target hosts
主機是debian jessie amd64, 不肯定是否是systemd的問題。rest
網上查了一下,應該是這麼寫:code
ansible testserver -s -m service -a "name=nginx state=restarted"