第一次使用Ansible
Ansible 是什么?
-
- オープンソースの設定管理ツール
-
- YAMLの読みやすい構文
-
- リモートホストへのインストールが不要
-
- プッシュベース
-
- スケールダウン
- 組み込みモジュール
安装
Python:
Python 是一种高级的、解释性的、通用型编程语言,它被广泛应用于计算机科学和软件开发领域。
- バージョン2.5移行
$ python -V
Python 2.7.10
自动化工具
$ brew install ansible
$ ansible --version
ansible 2.0.2.0
设置测试服务器
$ mkdir ansible_playbooks
$ cd ansible_playbooks
$ vagrant init ubuntu/trusty64
$ vagrant up
使用Ansible连接到服务器
明确指定参数并进行连接的方法
主持人制作
$ vi hosts
testserver ansible_ssh_host=127.0.0.1 \
ansible_ssh_port=2222 \
ansible_ssh_user=vagrant \
ansible_ssh_private_key_file=.vagrant/machines/default/virtualbox/private_key
使用Ping模块进行连接确认。
$ ansible testserver -i hosts -m ping
testserver | SUCCESS => {
"changed": false,
"ping": "pong"
}
使用ansible.cfg的连接方法
创建ansible.cfg
$ vi ansible.cfg
[defaults]
hostfile = hosts
remote_user = vagrant
private_key_file = .vagrant/machines/default/virtualbox/private_key
host_key_checking = False
Ansible会按照以下顺序查找ansible.cfg文件。
-
- 在环境变量ANSIBLE_CONFIG中指定的位置
-
- ./ansible.cfg
-
- ~/.ansible.cfg
- /etc/ansible/ansible.cfg
使用ping模块进行连接测试
$ ansible testserver -m ping
testserver | SUCCESS => {
"changed": false,
"ping": "pong"
}
命令模块 (Ming ling mo kuai)
$ ansible testserver -m command -a uptime
testserver | SUCCESS | rc=0 >>
01:16:21 up 27 min, 1 user, load average: 0.00, 0.01, 0.05
尾巴
$ ansible testserver -a "tail /var/log/dmesg"
testserver | SUCCESS | rc=0 >>
[ 8.605835] type=1400 audit(1462582124.605:15): apparmor="STATUS" operation="profile_replace" profile="unconfined" name="/usr/lib/connman/scripts/dhclient-script" pid=1005 comm="apparmor_parser"
[ 8.605953] type=1400 audit(1462582124.605:16): apparmor="STATUS" operation="profile_replace" profile="unconfined" name="/usr/lib/connman/scripts/dhclient-script" pid=1005 comm="apparmor_parser"
[ 8.786303] type=1400 audit(1462582124.785:17): apparmor="STATUS" operation="profile_load" profile="unconfined" name="/usr/sbin/tcpdump" pid=1007 comm="apparmor_parser"
[ 8.813174] vboxvideo: Unknown symbol drm_open (err 0)
[ 8.813179] vboxvideo: Unknown symbol drm_poll (err 0)
[ 8.813181] vboxvideo: Unknown symbol drm_pci_init (err 0)
[ 8.813185] vboxvideo: Unknown symbol drm_ioctl (err 0)
[ 8.813188] vboxvideo: Unknown symbol drm_mmap (err 0)
[ 8.813190] vboxvideo: Unknown symbol drm_pci_exit (err 0)
[ 8.813192] vboxvideo: Unknown symbol drm_release (err 0)
sudo
$ ansible testserver -s -a "tail /var/log/syslog"
testserver | SUCCESS | rc=0 >>
May 7 00:48:53 vagrant-ubuntu-trusty-64 pollinate[1695]: system was previously seeded at [2016-05-07 00:48:43.913686907 +0000]
May 7 00:48:53 vagrant-ubuntu-trusty-64 pollinate[1697]: To re-seed this system again, use the -r|--reseed option
May 7 00:48:57 vagrant-ubuntu-trusty-64 ntpdate[1400]: adjust time server 91.189.89.199 offset -0.002563 sec
May 7 00:49:05 vagrant-ubuntu-trusty-64 ntpdate[1953]: step time server 91.189.89.199 offset -0.001220 sec
May 7 01:02:16 vagrant-ubuntu-trusty-64 ansible-ping: Invoked with data=None
May 7 01:06:50 vagrant-ubuntu-trusty-64 ansible-ping: Invoked with data=None
May 7 01:16:21 vagrant-ubuntu-trusty-64 ansible-command: Invoked with warn=True executable=None _uses_shell=False _raw_params=uptime removes=None creates=None chdir=None
May 7 01:17:01 vagrant-ubuntu-trusty-64 CRON[2164]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly)
May 7 01:18:17 vagrant-ubuntu-trusty-64 ansible-command: Invoked with warn=True executable=None _uses_shell=False _raw_params=tail /var/log/dmesg removes=None creates=None chdir=None
May 7 01:19:37 vagrant-ubuntu-trusty-64 ansible-command: Invoked with warn=True executable=None _uses_shell=False _raw_params=tail /var/log/syslog removes=None creates=None chdir=None