labunix's blog

labunixのラボUnix

ansibleを使ってみる。(intro_installation)

■ansibleを使ってみる。(intro_installation)

$ lsb_release -d
Description:	Debian GNU/Linux 10 (buster)

■今回は公式ドキュメントに従わずに、debianメンテバージョンの2.7系を使うので、公式の最新2.8ではない。
 公式ドキュメント上ではubuntuを使うことになっている。
 stretchならpython2系なので公式ドキュメントに従った方が良い。

 Installation Guide
 https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html

$ sudo apt-get install -y ansible

$ ansible --version
ansible 2.7.7
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/labunix/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.7.3 (default, Apr  3 2019, 05:39:12) [GCC 8.3.0]

■python3を使うので、20201月のサポート終了まで残り半年を切ったpython2は削除。
 未だに多くの依存関係があるけれど、これを機にpython3ベースに切り替える。

$ python -V
Python 2.7.16

$ python3 -V
Python 3.7.3

$ dpkg -l | awk '/^ii/&&/python2/{print $2}' | xargs sudo apt-get purge -y

$ sudo apt-get autoremove -y

$ sudo ln -s /usr/bin/python3 /usr/bin/python

$ python -V
Python 3.7.3

■SSH agentの登録前後
 まだ自動化しないので、SSHのパスワード認証で進める。

$ echo -e '[localhost]\n127.0.0.1' | sudo tee -a /etc/ansible/hosts
[localhost]
127.0.0.1

$ ansible localhost -m ping -u labunix -k
SSH password: 
127.0.0.1 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

$ ssh-agent bash

$ ssh-add ~/.ssh/id_rsa
Enter passphrase for /home/labunix/.ssh/id_rsa: 
Identity added: /home/labunix/.ssh/id_rsa (/home/labunix/.ssh/id_rsa)

$ ansible localhost -m ping -u labunix
127.0.0.1 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

■「-vvv」で見ると、「AnsiballZ_ping.py」をPUTして実行している。

$ ansible localhost -m ping -u labunix -k -vvv 2>&1 | tee trace.log 

$ awk '/.py/{for(a=1;a<=NF;a++){if($a ~ /tmp.*.py/){gsub(".py.*",".py",$a);print $a | "sort -uV"}}}' trace.log 
/home/labunix/.ansible/tmp/ansible-tmp-1566133079.387592-170762819780747/AnsiballZ_ping.py

■「/bin/echo」はcommandモジュール「AnsiballZ_command.py」を呼び出している。

$ ansible all -a "/bin/echo hello" -u labunix -k
SSH password: 
127.0.0.1 | CHANGED | rc=0 >>
hello

$ ansible all -a "/bin/echo hello" -u labunix -k -vvv 2>&1 | tee trace.log 

$ awk '/.py/{for(a=1;a<=NF;a++){if($a ~ /tmp.*.py/){gsub(".py.*",".py",$a);print $a | "sort -uV"}}}' trace.log 
/home/labunix/.ansible/tmp/ansible-tmp-1566133702.9949405-14558798693186/AnsiballZ_command.py

$ dpkg -L ansible | awk '/modules/&& !/doc|init/ && /\/ping.py/'
/usr/lib/python3/dist-packages/ansible/modules/system/ping.py

■「-m shell」でシェルを実行してみる。
 実際には「AnsiballZ_command.py」と「t.py」が使われる。

$ ansible -m shell -a 'env uname -a | sed -e "s%"$(hostname)"%XXX%"' all -i /etc/ansible/hosts -u labunix -k
SSH password: 
127.0.0.1 | CHANGED | rc=0 >>
Linux XXX 4.19.0-5-amd64 #1 SMP Debian 4.19.37-5+deb10u2 (2019-08-08) x86_64 GNU/Linux

$ ansible -m shell -a 'env uname -a | sed -e "s%"$(hostname)"%XXX%"' all -i /etc/ansible/hosts -u labunix -k -vvv 2>&1 | tee trace.log 
$ awk '/.py/{for(a=1;a<=NF;a++){if($a ~ /tmp.*.py/){gsub(".py.*",".py",$a);gsub(".*/home","/home",$a);print $a | "sort -uV"}}}' trace.log
/home/labunix/.ansible/tmp/ansible-local-1666_fhsiv0h/t.py
/home/labunix/.ansible/tmp/ansible-tmp-1566135686.9058464-199041705948960/AnsiballZ_command.py

■「-m raw」でシェルを実行してみる。

$ ansible -m raw -a 'env uname -a | sed -e "s%"$(hostname)"%XXX%"' all -i /etc/ansible/hosts -u labunix -k -vvv 2>&1 | tee trace.log 

$ awk '/<127.0.0.1>/{gsub(" b.|=10 ","&\n  ",$0);print $0}' trace.log 
<127.0.0.1> ESTABLISH SSH CONNECTION FOR USER: labunix
<127.0.0.1> SSH: EXEC sshpass -d10 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o User=labunix -o ConnectTimeout=10 
  -o ControlPath=/home/labunix/.ansible/cp/28a838bf52 -tt 127.0.0.1 'env uname -a | sed -e "s%"$(hostname)"%XXX%"'
<127.0.0.1> (0, b'
  Linux XXX 4.19.0-5-amd64 #1 SMP Debian 4.19.37-5+deb10u2 (2019-08-08) x86_64 GNU/Linux\r\n', b'
  Shared connection to 127.0.0.1 closed.\r\n')

■commands系のモジュールの使い方をまとめて出してみる。

$ dpkg -L ansible | \
    awk '/\/commands\//&&!/init/ \
         {gsub(".*/|.py","",$0); \
          print "echo \042["$0"]\042;ansible-doc "$0" | grep \042^ *"$0": [^ ]\042 | sort -uV"}' | sh
[command]
  command: cat /etc/motd
  command: cat {{ myfile|quote }}
  command: /usr/bin/make_database.sh arg1 arg2
[expect]
[psexec]
[raw]
  raw: cat < /tmp/*txt
  raw: dnf install -y python2 python2-dnf libselinux-python
  raw: "{{package_mgr|quote}} {{pkg_flags|quote}} install {{python|quote}}"
[script]
  script: /some/local/create_file.sh --some-argument 1234
  script: /some/local/remove_file.sh --some-argument 1234
  script: /some/local/script
  script: /some/local/script.py
  script: /some/local/script.sh --some-argument 1234
[shell]
  shell: cat < /tmp/*txt
  shell: cat {{ myfile|quote }}
  shell: curl --socks5 localhost:9000 http://www.ansible.com
  shell: somescript.sh >> somelog.txt
  shell: |
[telnet]