Ansibleを完全にマスターする
Keisuke.K
Ansible #とは
http://www.ansible.com/how-ansible-works
Ansible is a radically simple IT automation engine that automates cloud
provisioning, configuration management, application deployment, intra-
service orchestration, and many other IT needs.
Being designed for multi-tier deployments since day one, Ansible models
your IT infrastructure by describing how all of your systems inter-relate,
rather than just managing one system at a time.
It uses no agents and no additional custom security infrastructure, so it's
easy to deploy - and most importantly, it uses a very simple language (YAML,
in the form of Ansible Playbooks) that allow you to describe your
automation jobs in a way that approaches plain English.
On this page, we'll give you a really quick overview so you can see things in
context. For more detail, hop over to docs.ansible.com.
Infrastructure as a Code を実現する
要するに
Ansible の導入
まずは
• RHEL 6, 7 (EPEL)
– # yum install ansible
• Fedora ~21
– # yum install ansible
• Fedora 22~
– # dnf install ansible
_人人人人_
> 簡単 <
 ̄Y^Y^Y^Y ̄
Ansible の使い方
Playbook を書く
$ cat site.yml
- hosts: webservers
tasks:
- name: Install httpd
yum: name=httpd state=installed
- name: Start httpd
service: name=httpd state=started
適用先ホストを書く
$ cat hosts
[webservers]
www.example.com
Playbook を実行する
$ ansible-playbook –i hosts site.yml
_人人人人_
> 簡単 <
 ̄Y^Y^Y^Y ̄
もちろん
• 何度繰り返しても同様の結果になります
• 冪等性
• Playbook にはあるべき状態を記述すると
いう意識で
補足
• エージェントレス
• Python さえ入ってれば OK
• ノーパスフレーズ鍵または ssh-agent に鍵
を登録すると幸せになれる
ちょっと複雑なこともやってみる
要件
• httpd を入れて起動する
• httpd の自動起動を有効化する
• Listen ポートを変える
• 設定を変えて ansible-playbook したとき
は httpd を再起動する
site.yml
- hosts: webservers
tasks:
- name: Install httpd
yum: name=httpd state=installed
- name: Change listen port
lineinfile:
dest=/etc/httpd/conf/httpd.conf
regexp=”^Listen ” line=”Listen 10080”
notify: Restart httpd
- name: Start httpd
service: name=httpd enabled=yes state=started
handlers:
- name: Restart httpd
service: name=httpd state=restarted
_人人人人_
> 簡単 <
 ̄Y^Y^Y^Y ̄
Ansible を完全にマスターする
本題
完全にマスターする #とは
で、
https://twitter.com/kamekoopa/status/641840765904154624
Ansible 完マス
• Hello, World?
• /etc/motd に “Hello, world!” 書くくらい
では簡単にできてしまう。
• FizzBuzz?
• 冪等性 #とは
• やろう
FizzBuzz /w Ansible
- hosts: 127.0.0.1
connection: local
tasks:
- file: path=/tmp/fizzbuzz_counter state=touch
- shell: echo $((`cat /tmp/fizzbuzz_counter` + 1)) >
/tmp/fizzbuzz_counter
- set_fact: counter="{{lookup('file', '/tmp/fizzbuzz_counter')}}"
- debug: msg=FizzBuzz
when: "{{counter|int % 3}} == 0 and {{counter|int}} % 5 == 0"
register: rc
- debug: msg=Fizz
when: "{{counter|int % 3}} == 0 and rc.skipped is defined"
- debug: msg=Buzz
when: "{{counter|int % 5}} == 0 and rc.skipped is defined"
FizzBuzz /w Ansible
$ ansible-playbook fizzbuzz.yml
PLAY [127.0.0.1] **************************************************************
GATHERING FACTS ***************************************************************
ok: [127.0.0.1]
TASK: [file path=/tmp/fizzbuzz_counter state=touch] ***************************
changed: [127.0.0.1]
TASK: [shell echo $((`cat /tmp/fizzbuzz_counter` + 1)) > /tmp/fizzbuzz_counter] ***
changed: [127.0.0.1]
TASK: [set_fact counter="3"] **************************************************
ok: [127.0.0.1]
TASK: [debug msg=FizzBuzz] ****************************************************
skipping: [127.0.0.1]
TASK: [debug msg=Fizz] ********************************************************
ok: [127.0.0.1] => {
"msg": "Fizz"
}
TASK: [debug msg=Buzz] ********************************************************
skipping: [127.0.0.1]
PLAY RECAP ********************************************************************
127.0.0.1 : ok=5 changed=2 unreachable=0 failed=0
FizzBuzz /w Ansible
$ ansible-playbook fizzbuzz.yml
PLAY [127.0.0.1] **************************************************************
GATHERING FACTS ***************************************************************
ok: [127.0.0.1]
TASK: [file path=/tmp/fizzbuzz_counter state=touch] ***************************
changed: [127.0.0.1]
TASK: [shell echo $((`cat /tmp/fizzbuzz_counter` + 1)) > /tmp/fizzbuzz_counter] ***
changed: [127.0.0.1]
TASK: [set_fact counter="5"] **************************************************
ok: [127.0.0.1]
TASK: [debug msg=FizzBuzz] ****************************************************
skipping: [127.0.0.1]
TASK: [debug msg=Fizz] ********************************************************
skipping: [127.0.0.1]
TASK: [debug msg=Buzz] ********************************************************
ok: [127.0.0.1] => {
"msg": "Buzz"
}
PLAY RECAP ********************************************************************
127.0.0.1 : ok=5 changed=2 unreachable=0 failed=0
FizzBuzz /w Ansible
$ ansible-playbook fizzbuzz.yml
PLAY [127.0.0.1] **************************************************************
GATHERING FACTS ***************************************************************
ok: [127.0.0.1]
TASK: [file path=/tmp/fizzbuzz_counter state=touch] ***************************
changed: [127.0.0.1]
TASK: [shell echo $((`cat /tmp/fizzbuzz_counter` + 1)) > /tmp/fizzbuzz_counter] ***
changed: [127.0.0.1]
TASK: [set_fact counter="15"] *************************************************
ok: [127.0.0.1]
TASK: [debug msg=FizzBuzz] ****************************************************
ok: [127.0.0.1] => {
"msg": "FizzBuzz"
}
TASK: [debug msg=Fizz] ********************************************************
skipping: [127.0.0.1]
TASK: [debug msg=Buzz] ********************************************************
skipping: [127.0.0.1]
PLAY RECAP ********************************************************************
127.0.0.1 : ok=5 changed=2 unreachable=0 failed=0
Ansible 完全にマスターした
おしまい

Ansible を完全にマスターする

  • 1.
  • 2.
  • 3.
    http://www.ansible.com/how-ansible-works Ansible is aradically simple IT automation engine that automates cloud provisioning, configuration management, application deployment, intra- service orchestration, and many other IT needs. Being designed for multi-tier deployments since day one, Ansible models your IT infrastructure by describing how all of your systems inter-relate, rather than just managing one system at a time. It uses no agents and no additional custom security infrastructure, so it's easy to deploy - and most importantly, it uses a very simple language (YAML, in the form of Ansible Playbooks) that allow you to describe your automation jobs in a way that approaches plain English. On this page, we'll give you a really quick overview so you can see things in context. For more detail, hop over to docs.ansible.com.
  • 4.
    Infrastructure as aCode を実現する 要するに
  • 5.
  • 6.
    • RHEL 6,7 (EPEL) – # yum install ansible • Fedora ~21 – # yum install ansible • Fedora 22~ – # dnf install ansible
  • 7.
  • 8.
  • 9.
    Playbook を書く $ catsite.yml - hosts: webservers tasks: - name: Install httpd yum: name=httpd state=installed - name: Start httpd service: name=httpd state=started
  • 10.
  • 11.
  • 12.
  • 13.
    もちろん • 何度繰り返しても同様の結果になります • 冪等性 •Playbook にはあるべき状態を記述すると いう意識で
  • 14.
    補足 • エージェントレス • Pythonさえ入ってれば OK • ノーパスフレーズ鍵または ssh-agent に鍵 を登録すると幸せになれる
  • 15.
  • 16.
    要件 • httpd を入れて起動する •httpd の自動起動を有効化する • Listen ポートを変える • 設定を変えて ansible-playbook したとき は httpd を再起動する
  • 17.
    site.yml - hosts: webservers tasks: -name: Install httpd yum: name=httpd state=installed - name: Change listen port lineinfile: dest=/etc/httpd/conf/httpd.conf regexp=”^Listen ” line=”Listen 10080” notify: Restart httpd - name: Start httpd service: name=httpd enabled=yes state=started handlers: - name: Restart httpd service: name=httpd state=restarted
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
    Ansible 完マス • Hello,World? • /etc/motd に “Hello, world!” 書くくらい では簡単にできてしまう。 • FizzBuzz? • 冪等性 #とは • やろう
  • 23.
    FizzBuzz /w Ansible -hosts: 127.0.0.1 connection: local tasks: - file: path=/tmp/fizzbuzz_counter state=touch - shell: echo $((`cat /tmp/fizzbuzz_counter` + 1)) > /tmp/fizzbuzz_counter - set_fact: counter="{{lookup('file', '/tmp/fizzbuzz_counter')}}" - debug: msg=FizzBuzz when: "{{counter|int % 3}} == 0 and {{counter|int}} % 5 == 0" register: rc - debug: msg=Fizz when: "{{counter|int % 3}} == 0 and rc.skipped is defined" - debug: msg=Buzz when: "{{counter|int % 5}} == 0 and rc.skipped is defined"
  • 24.
    FizzBuzz /w Ansible $ansible-playbook fizzbuzz.yml PLAY [127.0.0.1] ************************************************************** GATHERING FACTS *************************************************************** ok: [127.0.0.1] TASK: [file path=/tmp/fizzbuzz_counter state=touch] *************************** changed: [127.0.0.1] TASK: [shell echo $((`cat /tmp/fizzbuzz_counter` + 1)) > /tmp/fizzbuzz_counter] *** changed: [127.0.0.1] TASK: [set_fact counter="3"] ************************************************** ok: [127.0.0.1] TASK: [debug msg=FizzBuzz] **************************************************** skipping: [127.0.0.1] TASK: [debug msg=Fizz] ******************************************************** ok: [127.0.0.1] => { "msg": "Fizz" } TASK: [debug msg=Buzz] ******************************************************** skipping: [127.0.0.1] PLAY RECAP ******************************************************************** 127.0.0.1 : ok=5 changed=2 unreachable=0 failed=0
  • 25.
    FizzBuzz /w Ansible $ansible-playbook fizzbuzz.yml PLAY [127.0.0.1] ************************************************************** GATHERING FACTS *************************************************************** ok: [127.0.0.1] TASK: [file path=/tmp/fizzbuzz_counter state=touch] *************************** changed: [127.0.0.1] TASK: [shell echo $((`cat /tmp/fizzbuzz_counter` + 1)) > /tmp/fizzbuzz_counter] *** changed: [127.0.0.1] TASK: [set_fact counter="5"] ************************************************** ok: [127.0.0.1] TASK: [debug msg=FizzBuzz] **************************************************** skipping: [127.0.0.1] TASK: [debug msg=Fizz] ******************************************************** skipping: [127.0.0.1] TASK: [debug msg=Buzz] ******************************************************** ok: [127.0.0.1] => { "msg": "Buzz" } PLAY RECAP ******************************************************************** 127.0.0.1 : ok=5 changed=2 unreachable=0 failed=0
  • 26.
    FizzBuzz /w Ansible $ansible-playbook fizzbuzz.yml PLAY [127.0.0.1] ************************************************************** GATHERING FACTS *************************************************************** ok: [127.0.0.1] TASK: [file path=/tmp/fizzbuzz_counter state=touch] *************************** changed: [127.0.0.1] TASK: [shell echo $((`cat /tmp/fizzbuzz_counter` + 1)) > /tmp/fizzbuzz_counter] *** changed: [127.0.0.1] TASK: [set_fact counter="15"] ************************************************* ok: [127.0.0.1] TASK: [debug msg=FizzBuzz] **************************************************** ok: [127.0.0.1] => { "msg": "FizzBuzz" } TASK: [debug msg=Fizz] ******************************************************** skipping: [127.0.0.1] TASK: [debug msg=Buzz] ******************************************************** skipping: [127.0.0.1] PLAY RECAP ******************************************************************** 127.0.0.1 : ok=5 changed=2 unreachable=0 failed=0
  • 27.
  • 28.