Anableによる自動構成管理(22.04.18)
25899 ワード
複文
https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#playbooks-loops
タスクの下でloop
、with_
、until
、loop
繰り返し文を構成
リスト#リスト#
with_items
ではなくwith_list
、loop
- hosts: 192.168.100.11
gather_facts: no
tasks:
- debug:
msg: "{{ item }}"
with_items:
- apple
- banana
- carrot
- hosts: 192.168.100.11
gather_facts: no
vars:
fruits:
- apple
- banana
- carrot
tasks:
- debug:
msg: "{{ item }}"
with_items:
"{{ fruits }}"
- hosts: 192.168.100.11
gather_facts: no
vars:
fruits:
- apple
- banana
- carrot
tasks:
- debug:
msg: "{{ item }}"
loop:
"{{ fruits }}"
辞書
with_dict
ではなくwhen
- name: Add several users
user:
name: "{{ item.name }}"
state: present
groups: "{{ item.groups }}"
loop:
- name: 'testuser1'
groups: 'wheel'
- name: 'testuser2'
groups: 'root'
#[ {name: 'testuser1', groups: 'wheel'}, {name: 'testuser2', group: 'root'} ]
- hosts: 192.168.100.11
gather_facts: no
vars:
fruits:
- name: apple
count: 2
- name: banana
count: 3
tasks:
- debug:
msg: "{{ item.name }} / {{ item.count }}"
loop:
'{{ fruits }}'
条件文
https://docs.ansible.com/ansible/latest/user_guide/playbooks_conditionals.html#basic-conditionals-with-when
https://docs.ansible.com/ansible/latest/user_guide/playbooks_tests.html#playbooks-tests
https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#playbooks-filters
タスク使用test
キーワード、定義条件filter
、{{ }}
条件を定義するときにtasks
カッコXを使用- hosts: 192.168.100.11
vars:
switch: "on"
tasks:
- debug:
msg: "hello switch on"
when: switch == "on"
- debug:
msg: "hello switch off"
when: switch == "off"
条件文でよく使用されるパラメータ
https://docs.ansible.com/ansible/latest/user_guide/playbooks_conditionals.html#commonly-used-facts
- hosts: 192.168.100.11
gather_facts: no
tasks:
- debug:
msg: "{{ item }}"
with_items:
- apple
- banana
- carrot
- hosts: 192.168.100.11
gather_facts: no
vars:
fruits:
- apple
- banana
- carrot
tasks:
- debug:
msg: "{{ item }}"
with_items:
"{{ fruits }}"
- hosts: 192.168.100.11
gather_facts: no
vars:
fruits:
- apple
- banana
- carrot
tasks:
- debug:
msg: "{{ item }}"
loop:
"{{ fruits }}"
- name: Add several users
user:
name: "{{ item.name }}"
state: present
groups: "{{ item.groups }}"
loop:
- name: 'testuser1'
groups: 'wheel'
- name: 'testuser2'
groups: 'root'
#[ {name: 'testuser1', groups: 'wheel'}, {name: 'testuser2', group: 'root'} ]
- hosts: 192.168.100.11
gather_facts: no
vars:
fruits:
- name: apple
count: 2
- name: banana
count: 3
tasks:
- debug:
msg: "{{ item.name }} / {{ item.count }}"
loop:
'{{ fruits }}'
https://docs.ansible.com/ansible/latest/user_guide/playbooks_conditionals.html#basic-conditionals-with-when
https://docs.ansible.com/ansible/latest/user_guide/playbooks_tests.html#playbooks-tests
https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#playbooks-filters
タスク使用
test
キーワード、定義条件filter
、{{ }}
条件を定義するときにtasks
カッコXを使用- hosts: 192.168.100.11
vars:
switch: "on"
tasks:
- debug:
msg: "hello switch on"
when: switch == "on"
- debug:
msg: "hello switch off"
when: switch == "off"
条件文でよく使用されるパラメータhttps://docs.ansible.com/ansible/latest/user_guide/playbooks_conditionals.html#commonly-used-facts
- hosts: wp
tasks:
- debug:
msg: "hello CentOS"
when: ansible_facts["distribution"] == "CentOS"
- debug:
msg: "hello Ubuntu"
when: ansible_facts["distribution"] == "Ubuntu"
マニピュレータ
襟
できるだけべき乗等性を満たす
すべてのモジュール、モジュールのパラメータがべき乗等性を満たしていない
問題のあるコード- hosts: 192.168.100.11
become: yes
vars:
web_svc_port: "80"
tasks:
- yum:
name: httpd
state: installed
- lineinfile:
path: /etc/httpd/conf/httpd.conf
regexp: '^Listen'
line: 'Listen {{ web_svc_port }}'
- service:
name: httpd
state: restarted
enabled: yes
トラブルシューティングコード- hosts: 192.168.100.11
become: yes
vars:
web_svc_port: "80"
tasks:
- yum:
name: httpd
state: installed
- lineinfile:
path: /etc/httpd/conf/httpd.conf
regexp: '^Listen'
line: 'Listen {{ web_svc_port }}'
register: result
- service:
name: httpd
state: started
enabled: yes
- service:
name: httpd
state: restarted
enabled: yes
when: result is changed
ゲーム、ジョブ名
- name: Name Test Playbook
hosts: 192.168.100.11
tasks:
- name: task1
debug:
msg: hello world
- name: task2
debug:
msg: hello world
- name: task3
debug:
msg: hello world
- debug:
msg: hello world
name: task4
マニピュレータ
理由.特定の操作が変更された場合にのみ実行される操作を指定します.
ハンドラ内のアクションには名前が必要です
Handler運転の順番は?
- hosts: 192.168.100.11
become: yes
vars:
web_svc_port: "80"
tasks:
- yum:
name: httpd
state: installed
- lineinfile:
path: /etc/httpd/conf/httpd.conf
regexp: '^Listen'
line: 'Listen {{ web_svc_port }}'
- service:
name: httpd
state: restarted
enabled: yes
- hosts: 192.168.100.11
become: yes
vars:
web_svc_port: "80"
tasks:
- yum:
name: httpd
state: installed
- lineinfile:
path: /etc/httpd/conf/httpd.conf
regexp: '^Listen'
line: 'Listen {{ web_svc_port }}'
register: result
- service:
name: httpd
state: started
enabled: yes
- service:
name: httpd
state: restarted
enabled: yes
when: result is changed
- name: Name Test Playbook
hosts: 192.168.100.11
tasks:
- name: task1
debug:
msg: hello world
- name: task2
debug:
msg: hello world
- name: task3
debug:
msg: hello world
- debug:
msg: hello world
name: task4
block
)が完了すると、プロセッサは- name: handler example
hosts: 192.168.100.11
tasks:
- name: task1
file:
path: /tmp/test1
state: touch
notify:
- handle2
- handle1
#- name: error
# command: ls -P
- name: task2
file:
path: /tmp/test2
state: touch
notify:
- handle1
handlers:
- name: handle1
debug:
msg: "handle1"
- name: handle2
debug:
msg: "handle2"
- name: Handler Example
hosts: 192.168.100.11
become: yes
vars:
web_svc_port: "80"
tasks:
- name: Install httpd Package
yum:
name: httpd
state: installed
- name: Reconfigure httpd service port
lineinfile:
path: /etc/httpd/conf/httpd.conf
regexp: '^Listen'
line: 'Listen {{ web_svc_port }}'
notify:
- Restart httpd Service
- name: Start httpd Service
service:
name: httpd
state: started
enabled: yes
handlers:
- name: Restart httpd Service
service:
name: httpd
state: restarted
enabled: yes
ハンドラが実行されず、その後の操作に失敗しました
結論:Handlerが実行されていない
- name: Flush handlers
meta: flush_handlers
ansible-playbook test.yaml --force-handlers
強制実行ハンドラの設定積み木
ブロック=複数のタスクを含むグループ
ブロックの機能
1.複数のタスクに共通キーワードを付与可能(ex:条件文)
2.rescue
、always
、block
ブロックはエラー処理に使用できるrescue
常にブロックを実行block
ブロックにエラーが発生した場合のみ実行always
常時運転- hosts: 192.168.100.11
tasks:
- block:
- debug:
msg: hello world
- command: /usr/bin/false
- debug:
msg: hello world2
ignore_errors: yes
rescue:
- debug:
msg: It's rescue
always:
- debug:
msg: It's Always
タブ
タスクをマークでき、特定のタグのタスクしか実行できません.- hosts: 192.168.100.11
gather_facts: no
tasks:
- debug:
msg: "web server stage"
tags:
- stage
- debug:
msg: "web server product"
tags:
- prod
- debug:
msg: "web server all"
allラベル:すべてのタスクが
untaggedタグ:タグが設定されていないタスクに属するansible-playbook test.yaml --tags=stage,prod
チェックマークansible-playbook test.yaml --list-tasks
ansible-playbook test.yaml --list-tags
タスクせいぎょ
step
ansible-playbook test.yaml --step
特定のタスクから開始
ansible-playbook test.yaml --start-at-task="task3"
ansible-playbook test.yaml --start-at-task="task3" --limit 192.168.100.12
Reference
この問題について(Anableによる自動構成管理(22.04.18)), 我々は、より多くの情報をここで見つけました
https://velog.io/@sunny-10/22.04.18
テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol
- hosts: 192.168.100.11
tasks:
- block:
- debug:
msg: hello world
- command: /usr/bin/false
- debug:
msg: hello world2
ignore_errors: yes
rescue:
- debug:
msg: It's rescue
always:
- debug:
msg: It's Always
タスクをマークでき、特定のタグのタスクしか実行できません.
- hosts: 192.168.100.11
gather_facts: no
tasks:
- debug:
msg: "web server stage"
tags:
- stage
- debug:
msg: "web server product"
tags:
- prod
- debug:
msg: "web server all"
allラベル:すべてのタスクがuntaggedタグ:タグが設定されていないタスクに属する
ansible-playbook test.yaml --tags=stage,prod
チェックマークansible-playbook test.yaml --list-tasks
ansible-playbook test.yaml --list-tags
タスクせいぎょ
step
ansible-playbook test.yaml --step
特定のタスクから開始
ansible-playbook test.yaml --start-at-task="task3"
ansible-playbook test.yaml --start-at-task="task3" --limit 192.168.100.12
Reference
この問題について(Anableによる自動構成管理(22.04.18)), 我々は、より多くの情報をここで見つけました https://velog.io/@sunny-10/22.04.18テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol