Upgrade to Pro — share decks privately, control downloads, hide ads and more …

社内LT_Ansible

okochang
October 08, 2014

 社内LT_Ansible

社内LTでAnsibleについて話した資料

okochang

October 08, 2014
Tweet

More Decks by okochang

Other Decks in Technology

Transcript

  1. 実行例 $ ansible all -m ping ansible01.okochang.com | success >>

    { "changed": false, "ping": "pong" } $ ansible all -a "uname -r" ansible01.okochang.com | success | rc=0 >> 3.10.35-43.137.amzn1.x86_64
  2. 実行時オプション⑦ # バッチ処理などをしたい $ ansible all -B 180 -a "sleep

    120" $ ansible all -m async_status \ -a "jid=51511436813.4715"
  3. AWS環境への対応 $ sudo pip install boto $ cd .ansible $

    curl -LO https://raw.github.com/ansible/ansible/devel/plugins/ inventory/ec2.py $ curl -LO https://raw.github.com/ansible/ansible/devel/plugins/ inventory/ec2.ini $ vi .bash_profile export AWS_ACCESS_KEY_ID='xxxxxxxxxxxxxxxxxxxx' export AWS_SECRET_ACCESS_KEY='yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy' export EC2_INI_PATH=~/.ansible/ec2.ini $ chmod +x .ansible/ec2.py $ .ansible/ec2.py --list
  4. モジュールを使う④ # yumモジュール $ ansible webservers -m yum -a "name=httpd

    state=installed" -u ec2- user --sudo $ ansible webservers -m yum -a "name=postfix state=removed" -u ec2- user --sudo
  5. モジュールを使う⑤ # serviceモジュール $ ansible webservers -m service -a "name=httpd

    state=started" -u ec2-user -- sudo $ ansible webservers -m service -a "name=httpd state=restarted" -u ec2-user -- sudo $ ansible webservers -m service -a "name=httpd state=stopped" -u ec2-user -- sudo
  6. Playbooksを使うその① # test_con.yml - hosts: all remote_user: ec2-user tasks: -

    name: test connection ping: ================================= $ ansible-playbook .ansible/playbooks/ test_con.yml
  7. Playbooksを使うその② # shell_result.yml - hosts: ec2 remote_user: ec2-user tasks: -

    name: run command and ignore the result shell: /bin/echo foo ================================= $ ansible-playbook -i .ansible/ec2.py -u ec2- user .ansible/playbooks/shell_result.yml
  8. Playbooksを使うその③ # foo_var.yml - name: echo foo command: /bin/echo foo

    - name: echo var command: /bin/echo var ================================= # hoge.yml - hosts: ec2 remote_user: ec2-user tasks: - include: foo_var.yml
  9. Playbooksを使う④ # webserver01.yml - hosts: tag_Name_target01 sudo: yes tasks: -

    name: ensure apache is installed yum: pkg=httpd state=installed - name: apacche running service: name=httpd state=startet enabled=yes
  10. Playbooksを使う⑤ ! # webserver02.yml - hosts: tag_Name_target01 vars: http_port: 80

    sudo: yes tasks: - name: create virtual host file for {{ vhost }} template: src=~/vhost.conf.j2 dest=/etc/httpd/conf.d/{{ vhost }}.conf notify: - restart apache handlers: - name: restart apache service: name=httpd state=restarted
  11. ディレクトリ構成 . ├── roles │ ├── common │ │ ├──

    files │ │ ├── handlers │ │ │ └── main.yml │ │ ├── meta │ │ ├── tasks │ │ │ └── main.yml │ │ ├── templates │ │ │ └── clock.j2 │ │ └── vars │ └── webservers └── site.yml
  12. site.yml - name: common configuration to all node hosts: ec2

    user: ec2-user roles: - common - name: webserver configuration to Role web hosts: tag_Role_web user: ec2-user roles: - webservers