Ansible的初始设置

前提 – Qian2 ti2

前提是一个条件或者基础,必须满足或者存在才能达到某种结果或者目标。

    • クライアント: AnsibleはMacにインストール済(ansible 2.10.2)

 

    サーバー: VagrantのUbuntu2004の基本的な構築をansibleを使って行うことを想定してます

最初的设置

准备日志的输出目标。

$ mkdir logs

创建2个ansible.cfg文件

remote_user: ansibleを実行するユーザの指定

remote_port: defaultは22番ポートだが、今回はvagrant起動時に指定された2222番ポートを使用するため設定

private_key_file: ansibleユーザのkeyを指定

gathering: ターゲットノードのデータ取得方法の指定

host_key_checking: SSH接続時に公開鍵のフィンガープリントをチェックを行うか否か

log_path: ansible実行時のログの出力先を指定

diff --git a/ansible.cfg b/ansible.cfg
index 0440d48..3b83cfc 100644
--- a/ansible.cfg
+++ b/ansible.cfg
@@ -1 +1,8 @@
 [defaults]
+
+remote_user = ansible
+remote_port = 2222
+private_key_file = /Users/tshu1/.ssh/id_ed25519_ansible
+gathering = smart
+host_key_checking = False
+log_path = /Users/tshu1/ghq/github.com/tshu1/ubuntu-2004/logs/ansible.log

创建3个库存文件

$ mkdir inventory

$ vim inventory/vagrant

[ubuntu]
localhost 

用 4 个 ansible-galaxy 命令创建角色。

$ ansible-galaxy role init base

$ tree
.
├── README.md
├── Vagrantfile
├── ansible.cfg
├── base
│   ├── README.md
│   ├── defaults
│   │   └── main.yml
│   ├── files
│   ├── handlers
│   │   └── main.yml
│   ├── meta
│   │   └── main.yml
│   ├── tasks
│   │   └── main.yml
│   ├── templates
│   ├── tests
│   │   ├── inventory
│   │   └── test.yml
│   └── vars
│       └── main.yml
├── inventory
│   └── vagrant
└── logs
    └── ansible.log

5 链接确认

$ ansible -i inventory ubuntu -m ping
localhost | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}