Действия

IaaC (Ansible): различия между версиями

Материал из lulzette's wiki

Строка 64: Строка 64:
</source>
</source>


Ставим на все сервера <code>vim</code>:
Ставим на все сервера <code>vim</code> (host2 почему-то не нашел python-apt и выполнять сие действо отказался):


<source>
<source>
ansible all -m apt -a "name=vim state=latest" -u root
root@353536-otparch2:~/ansible# ansible all -m apt -a "name=vim state=latest"  
host1 | SUCCESS => {
    "cache_update_time": 1600183325,
    "cache_updated": false,
    "changed": false
}
host2 | FAILED! => {
    "changed": false,
    "cmd": "apt-get install python-apt -y -q",
    "msg": "E: Package 'python-apt' has no installation candidate",
    "rc": 100,
    "stderr": "E: Package 'python-apt' has no installation candidate\n",
    "stderr_lines": [
        "E: Package 'python-apt' has no installation candidate"
    ],
    "stdout": "Reading package lists...\nBuilding dependency tree...\nReading state information...\nPackage python-apt is not available, but is referred to by another package.\nThis may mean that the package is missing, has been obsoleted, or\nis only available from another source\nHowever the following packages replace it:\n  python-apt-doc python3-apt-dbg python3-apt python-apt-common\n\n",                                                                                                                                                                                                   
    "stdout_lines": [
        "Reading package lists...",
        "Building dependency tree...",
        "Reading state information...",
        "Package python-apt is not available, but is referred to by another package.",
        "This may mean that the package is missing, has been obsoleted, or",
        "is only available from another source",
        "However the following packages replace it:",
        "  python-apt-doc python3-apt-dbg python3-apt python-apt-common",
        ""
    ]
}
host3 | SUCCESS => {
    "cache_update_time": 1600184023,
    "cache_updated": false,
    "changed": false
}


</source>
</source>


= tips =
= tips =

Версия от 18:43, 15 сентября 2020

Ставим!

Предположим есть LXC контейнер slave и master хост

ставим на хост ansible...

на slaves необходимо поставить python

Config

  • Настроить ssh соединение от хоста к клиентам (ключи сгенерировать и скинуть на клиенты)
  • Добавим в файл /etc/ansible/hosts хост(ы):
[some_hosts]
host1 ansible_ssh_host=10.0.3.142

В [] указано название группы хостов, host1 - название хоста которое будем использовать при выполнении команд, ansible_ssh_host - указываем, что к хосту надо подключаться по SSH

RUN!

Можно выполнять на клиентах как команды, так и готовые модули (TODO: настроить nginx с помощью Ansible)

Выполняем команды с помощью параметра -a:

root@353536-otparch2:~# ansible -a 'ls -la /' host1
host1 | SUCCESS | rc=0 >>
total 68
drwxr-xr-x  21 root root 4096 Sep 15 14:54 .
drwxr-xr-x  21 root root 4096 Sep 15 14:54 ..
drwxr-xr-x   2 root root 4096 Sep 15 07:45 bin
drwxr-xr-x   2 root root 4096 Apr 24  2018 boot
drwxr-xr-x   7 root root  520 Sep 15 14:54 dev
drwxr-xr-x  65 root root 4096 Sep 15 15:01 etc
drwxr-xr-x   3 root root 4096 Sep 15 07:45 home
drwxr-xr-x  11 root root 4096 Sep 15 07:44 lib
drwxr-xr-x   2 root root 4096 Sep 15 07:44 lib64
drwxr-xr-x   2 root root 4096 Sep 15 07:43 media
drwxr-xr-x   2 root root 4096 Sep 15 07:43 mnt
drwxr-xr-x   2 root root 4096 Sep 15 07:43 opt
dr-xr-xr-x 136 root root    0 Sep 15 14:54 proc
drwx------   5 root root 4096 Sep 15 14:59 root
drwxr-xr-x  13 root root  440 Sep 15 15:06 run
drwxr-xr-x   2 root root 4096 Sep 15 07:45 sbin
drwxr-xr-x   2 root root 4096 Sep 15 07:43 srv
dr-xr-xr-x  13 root root    0 Sep 15 14:54 sys
drwxrwxrwt   9 root root 4096 Sep 15 15:06 tmp
drwxr-xr-x  10 root root 4096 Sep 15 07:43 usr
drwxr-xr-x  12 root root 4096 Sep 15 07:45 var

Пингуемся:

root@353536-otparch2:~# ansible -m ping host1
host1 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

Ставим на все сервера vim (host2 почему-то не нашел python-apt и выполнять сие действо отказался):

root@353536-otparch2:~/ansible# ansible all -m apt -a "name=vim state=latest" 
host1 | SUCCESS => {
    "cache_update_time": 1600183325, 
    "cache_updated": false, 
    "changed": false
}
host2 | FAILED! => {
    "changed": false, 
    "cmd": "apt-get install python-apt -y -q", 
    "msg": "E: Package 'python-apt' has no installation candidate", 
    "rc": 100, 
    "stderr": "E: Package 'python-apt' has no installation candidate\n", 
    "stderr_lines": [
        "E: Package 'python-apt' has no installation candidate"
    ], 
    "stdout": "Reading package lists...\nBuilding dependency tree...\nReading state information...\nPackage python-apt is not available, but is referred to by another package.\nThis may mean that the package is missing, has been obsoleted, or\nis only available from another source\nHowever the following packages replace it:\n  python-apt-doc python3-apt-dbg python3-apt python-apt-common\n\n",                                                                                                                                                                                                    
    "stdout_lines": [
        "Reading package lists...", 
        "Building dependency tree...", 
        "Reading state information...", 
        "Package python-apt is not available, but is referred to by another package.", 
        "This may mean that the package is missing, has been obsoleted, or", 
        "is only available from another source", 
        "However the following packages replace it:", 
        "  python-apt-doc python3-apt-dbg python3-apt python-apt-common", 
        ""
    ]
}
host3 | SUCCESS => {
    "cache_update_time": 1600184023, 
    "cache_updated": false, 
    "changed": false
}

tips

Устанавливался питона на сервера (ибо он нужен для ansible):

root@353536-otparch2:~/ansible# cat ans_install_python.yml 
# Add this snippet to the top of your playbook. 
# It will install python2 if missing (but checks first so no expensive repeated apt updates)
# gwillem@gmail.com

- hosts: all
  gather_facts: False
  
  tasks:
  - name: install python 2
    raw: test -e /usr/bin/python || (apt -y update && apt install -y python)

root@353536-otparch2:~/ansible# ansible-playbook ans_install_python.yml

PLAY [all] ***************************************************************************************

TASK [install python 2] **************************************************************************
changed: [host1]
changed: [host3]
changed: [host2]

PLAY RECAP ***************************************************************************************
host1                      : ok=1    changed=1    unreachable=0    failed=0   
host2                      : ok=1    changed=1    unreachable=0    failed=0   
host3                      : ok=1    changed=1    unreachable=0    failed=0