Mark-20170810-Centos 7 方便快捷安裝 Ansible 及 copy 文件/啓動 NTP 服務簡例

安裝

epel-release 真的好良心啊。

yum install -y epel-release
yum install -y ansible

順帶找一個 ubuntu 的安裝版本

sudo sed  -i  -re  's/\w+\.archive\.ubuntu\.com/archive.ubuntu.com/g'  /etc/apt/sources.list
sudo apt-get update
sudo apt-get install software-properties-common
sudo apt-add-repository ppa:ansible/ansible
sudo apt-get update

# 假裝以上是需要做的,目前未測試,後補吧。
sudo apt-get install ansible

ubuntu14.04安裝Ansible

copy 文件

一氣呵成,不含雜質,Mark 幾個知識點。

ansible.cfg

Ansible 按照如下位置和順序來查找ansible.cfg 文件:
1. ANSIBLE_CONFIG 環境變量所指定的文件。
2. ./ansible.cfg(當前目錄下的ansible.cfg)。
3. ~/.ansible.cfg(主目錄下的.ansible.cfg)。
4. /etc/ansible/ansible.cfg。

使用ansible.cfg 文件來簡化配置

所以第二個選擇相當不錯。

第一次登陸報錯

Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this. Please add this host's fingerprint to your known_hosts file to manage this host.

解決方法:在ansible.cfg文件中更改下面的參數:

[defaults]
host_key_checking = false

ansible運維自動化工具

apt & yum

ssh

ssh - openssh-server

fatal: [172.16.232.132]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: connect to host 172.16.232.132 port 22: Connection refused\r\n", "unreachable": true}
sudo apt-get install openssh-server
sudo service ssh start

root can’t login

fatal: [172.16.232.132]: UNREACHABLE! => {"changed": false, "msg": "Authentication failure.", "unreachable": true}

允許root用戶登錄ssh(Ubuntu)

daemon-reload

fatal: [172.16.232.132]: FAILED! => {"changed": true, "cmd": "systemctl daemon-reload", "delta": "0:00:00.002450", "end": "2017-08-11 11:29:27.429572", "failed": true, "rc": 127, "start": "2017-08-11 11:29:27.427122", "stderr": "/bin/sh: 1: systemctl: not found", "stderr_lines": ["/bin/sh: 1: systemctl: not found"], "stdout": "", "stdout_lines": []}
  • 原:
- name: enable ntp deamon
  shell: systemctl daemon-reload
  • 改:
- name: enable ntp deamon
  systemd: daemon_reload=yes

systemd - Manage services.

依舊報錯。

fatal: [172.16.232.132]: FAILED! => {"changed": false, "failed": true, "msg": "Failed to find required executable systemctl in paths: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"}

ubuntu 沒有裝 systemctl,不管了。

ansible_os_family 不生效

我果然還是太天真,Ubuntu 對應的是 Debian,CentOS 對應的是 RedHat。

[root@centos ansible-ntpservice]# ansible all -i inventory/ -m setup|more|grep ansible_os_family
        "ansible_os_family": "Debian",
        "ansible_os_family": "RedHat",

debug

- name: check ntp status
  shell: ntpq -p
  register: result

- name: print the ntp status
  debug: var=result.stdout_lines

Git

  • 1-11111/ansible-copyfile

http://git.oschina.net/zhoufen111111/ansible-copyfile

  • 1-11111/ansible-ntpservice

http://git.oschina.net/zhoufen111111/ansible-ntpservice/tree/master/

執行

ansible-playbook -i inventory/ copyfile.yaml
ansible-playbook -i inventory/ ntpservice.yaml

salt 版

saltstack使用cmd.run批量更改ntp統一時間

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章