●Ansibleインストール
# Rocky Linux の場合
$ sudo dnf config-manager –set-enabled crb
$ sudo dnf install epel-release
# Ubuntu の場合
$ sudo apt update
$ sudo apt install software-properties-common
$ sudo add-apt-repository –yes –update ppa:ansible/ansible
$ sudo apt install ansible
●インストール後チェック
$ ansible –version
$ cd
$ mkdir ansible-example
$ cd ansible-example
$ touch playbook.yml
$ date>example.txt
# playbook.ymlの内容(インデントも意味を持つ)
– hosts: localhost
tasks:
– name: ファイルをコピーする
ansible.builtin.copy:
src: example.txt
dest: /tmp/
$ ansible-playbook playbook.yml
●Ansible Playbook で手順書を作成してみる
ファイル:hosts
[webserver]
ub2204
ファイル:playbook.yml
– hosts: webserver
become: yes
tasks:
– name: Nginx をインストールする (Ubuntu)
ansible.builtin.apt:
name: nginx
update_cache: yes
state: present
when: ansible_distribution == “Ubuntu”
– name: Nginx を起動する
ansible.builtin.systemd:
name: nginx.service
enabled: yes
state: started
$ ansible-playbook –ask-pass -i hosts playbook.yml
$ ansible-playbook -i hosts playbook.yml -–ask-become-pass
mura@ub2204:~/ansible-example/step2$ ansible-playbook –ask-pass -i hosts playbook.yml
SSH password:
PLAY [webserver] *********************************************************************************************************
TASK [Gathering Facts] ***************************************************************************************************
ok: [192.168.56.110]
TASK [Nginx をインストールする (Ubuntu)] *********************************************************************************
fatal: [192.168.56.110]: FAILED! => {“changed”: false, “msg”: “Unsupported parameters for (ansible.builtin.apt) module: when. Supported parameters include: allow_change_held_packages, allow_downgrade, allow_unauthenticated, autoclean, autoremove, cache_valid_time, clean, deb, default_release, dpkg_options, fail_on_autoremove, force, force_apt_get, install_recommends, lock_timeout, only_upgrade, package, policy_rc_d, purge, state, update_cache, update_cache_retries, update_cache_retry_max_delay, upgrade (allow-downgrade, allow-downgrades, allow-unauthenticated, allow_downgrades, default-release, install-recommends, name, pkg, update-cache).”}
PLAY RECAP ***************************************************************************************************************
192.168.56.110 : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
ignore_errors: yes
changed_when: False