Playbook

Jeanoza·2024년 5월 15일
0

ansible

목록 보기
2/3

Why playbook?

Using Shell Script

#!/bin/sh

ansible -i ./inventory -m user -a "name=bob state=present" all
ansible -i ./inventory -m user -a "name=alice1 state=present" all
ansible -i ./inventory -m user -a "name=john state=present" all

then call

./ansible_tasks.sh

result

ansible-node0 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 1000,
    "home": "/home/bob",
    "name": "bob",
    "shell": "/bin/sh",
    "state": "present",
    "system": false,
    "uid": 1000
}
ansible-node1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 1000,
    "home": "/home/bob",
    "name": "bob",
    "shell": "/bin/sh",
    "state": "present",
    "system": false,
    "uid": 1000
}
ansible-node0 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 1001,
    "home": "/home/alice1",
    "name": "alice1",
    "shell": "/bin/sh",
    "state": "present",
    "system": false,
    "uid": 1001
}
ansible-node1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 1001,
    "home": "/home/alice1",
    "name": "alice1",
    "shell": "/bin/sh",
    "state": "present",
    "system": false,
    "uid": 1001
}
ansible-node0 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 1002,
    "home": "/home/john",
    "name": "john",
    "shell": "/bin/sh",
    "state": "present",
    "system": false,
    "uid": 1002
}
ansible-node1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 1002,
    "home": "/home/john",
    "name": "john",
    "shell": "/bin/sh",
    "state": "present",
    "system": false,
    "uid": 1002
}

Using Playbook

  • lisiblity ++
  • ++ easy to manage tasks
- name: Create users
  hosts: all
  tasks:
    - name: Create user 'bob'
      ansible.builtin.user:
        name: bob
        state: present

    - name: Create user 'alice1'
      ansible.builtin.user:
        name: alice1
        state: present

    - name: Create user 'john'
      ansible.builtin.user:
        name: john
        state: present
            

then call

ansible-playbook -i ./inventory ./playbook.yml

result

PLAY [Create users] *********************************************************************

TASK [Gathering Facts] ******************************************************************
ok: [ansible-node1]
ok: [ansible-node0]

TASK [Create user 'bob'] ****************************************************************
ok: [ansible-node1]
ok: [ansible-node0]

TASK [Create user 'alice1'] *************************************************************
ok: [ansible-node1]
ok: [ansible-node0]

TASK [Create user 'john'] ***************************************************************
ok: [ansible-node0]
ok: [ansible-node1]

PLAY RECAP ******************************************************************************
ansible-node0              : ok=4    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
ansible-node1              : ok=4    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
profile
Développeur Fullstack(JS/TS, Node, Vue3, React)

0개의 댓글