SlideShare una empresa de Scribd logo
1 de 24
第1回 Ansible ハンズオン
とりあえずPlaybookを動かしてみよう
Ansibleとは
構成管理ツール
自動化ツール
PythonとSSHで動く
Agentレス
Redhatに買収された
PlaybookはYAMLで書かれる
YAMLとは
YAML Ain't a Markup Language
YAMLはマークアップ言語では無いの意
マークアップ言語とはタグなどで属性を表現する記法
一般的にこのようなもの
<body>
<h1>HTTPとWebサーバ</h1>
<h2>学習のポイント</h2>
<p>この章では~</p>
<a href=“setsumei.html”>詳細説明</a>
</body>
で、このようなタグを使わない、というもの
覚えるべきは3つのみ
YAMLとは
①ハッシュ
キーとバリューで表現する
KEY: VALUE
となる。コロンのあとに半角スペースを忘れずに
一般的なプログラム的には
KEY=VALUE
と一緒
KEYが変数でVALUEが中に入れる値になる
YAMLとは
②リスト
配列みたいなことができる
KEY:
- A
- B
- C
でKEY=[A,B,C]みたいなことができる
具体的には KEY[1]=A,KEY[2]=B,KEY[3]=Cという感じ
YAMLとは
③ネスト
ネストをつけると階層構造にできる
KEY:
KEY1:
KEY2: A
でKEY[KEY1[KEY2]]=A
と一緒
ネストは半角スペース2つでつけること
Playbookの構文
- hosts: localhost
become: yes
vars:
fn: hoge
tasks:
- name: テスト用実行内容
shell: echo “hoge” > /var/tmp/{{ fn }}
実行
部分
・ ・rootユーザに
なってから実行する
・ ・対象ホスト
「test.yml」
変数
部分
実行の仕方
ansible-playbook test.yml
構文
# ansible-playbook [playbook名]
実行結果
$ ansible-playbook test.yml
[WARNING]: provided hosts list is empty, only localhost is available. Note that
the implicit localhost does not match 'all'
PLAY [localhost] ******************************************************************
TASK [Gathering Facts] ************************************************************
ok: [localhost]
TASK [テスト用実行内容] *******************************************************************
changed: [localhost]
PLAY RECAP ************************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0
モジュールとは
コマンドのようなもの
Playbook実行部の命令
Pythonで書かれている
日々増えている
自分で作成できる
冪等性がある
Moduleの種類
https://docs.ansible.com/ansible/latest/modules/modules_by_category.html
千数種類存在している
クラウド用・ネットワーク用・システム用など用途別に分かれている
日本語の一覧はこちら
http://pj-doaa.hatenablog.com/entry/2017/08/01/122859
モジュールの書き方
tasks:
- name: ユーザ作成
user:
name: james
shell: /bin/bash
state: present
・ ・モジュール名を指定
・ ・モジュール名の後はスペース
2つで絡むをずらして引数を渡す
条件分岐の書き方
tasks:
- name: コマンド実行
command: ls /var/tmp/hoge
register: result
ignore_errors: True
- name: 失敗時実行
command: echo false
when: result is failed
- name: 成功時実行
command: echo success
when: result is succeeded
・ ・実行結果を取得
・ ・エラーになっても処理を継続
・ ・結果がエラーの場合実行
・ ・結果が正常の場合実行
繰り返しの書き方
tasks:
- name: ディレクトリの作成
file: path=/root/{{ item }} state=directory
with_items:
- test1
- test2
- test3
・ ・itemに配列が入り、処理を繰り返す
・
・ ・繰り返す値を記載
先頭のハイフンは繰り返し
(配列)を意味する
アトリビュートとは
先ほど書いたwhenやwith_items
実はtasksやnameもアトリビュート
一覧は下記
https://qiita.com/yunano/items/8494e785390360011a88
課題1
自分の名前1のユーザを作成する
# cat /etc/passwd | grep [ユーザ名]
上記コマンドで動作確認を行う
課題2
自分の名前1のユーザ作る
ユーザのパスワードを設定する
# su - [ユーザ名]
上記コマンドで動作確認を行う
課題3
自分の名前1のユーザ作る
ユーザのパスワードを設定する
ユーザ作成が成功した場合には作業ディレクトリ
にuser_listというファイルを作成し、先ほど
作ったユーザ名を記載する
課題4
自分の名前1のユーザ作る
ユーザのパスワードを設定する
ユーザ作成が成功した場合には作業ディレクトリ
にuser_listというファイルを作成し、先ほど
作ったユーザ名を記載する
user_listにユーザ名が記載されている場合は
ユーザを削除する
課題5
自分の名前1のユーザ作る
ユーザのパスワードを設定する
ユーザ作成が成功した場合には作業ディレクトリ
にuser_listというファイルを作成し、先ほど
作ったユーザ名を記載する
user_listにユーザ名が記載されている場合は
ユーザを削除する
上の処理を自分の名前+項番(1~10)で10回繰り
返す
ex1
自分のworkフォルダ内にファイルを作成して、
その中に/home/ec2-user配下のファイルリスト
を記述する。
ex2
自分のworkフォルダ内にファイルを作成して、
その中に/home/ec2-user配下のファイルリスト
を記述する。
自分のworkフォルダ内にファイルを作成する
ex3
自分のworkフォルダ内にファイルを作成して、
その中に/home/ec2-user配下のファイルリスト
を記述する。
自分のworkフォルダ内にファイルを作成する
現時点の/home/ec2-user配下のファイルリスト
と最初に作成したファイルの内容を比較する。
ex4
自分のworkフォルダ内にファイルを作成して、
その中に/home/ec2-user配下のファイルリスト
を記述する。
自分のworkフォルダ内にファイルを作成する
現時点の/home/ec2-user配下のファイルリスト
と最初に作成したファイルの内容を比較する。
差分を検知した場合はメッセージとして検知した
内容を出力する。

Más contenido relacionado

Destacado

Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...DevGAMM Conference
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationErica Santiago
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellSaba Software
 

Destacado (20)

Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
 

Ansible 20181031