0707-Ansible, docker

hyejin·2022년 7월 7일
0
  • shell은 멱등성이 없음
  • -k는 ask password란 의미

--- 앤서블 환경 설정 자동화

[root@ansible-server env]# vi keyscan.yml
- name: Setup for the Ansible's Environment
  hosts: localhost
  gather_facts: no
  
  tasks:
    - name: Generate sshkey
      shell: "{{ item }}"
      with_items:
        - "ssh-keyscan 192.168.0.250 >> ~/.ssh/known_hosts"
        - "ssh-keyscan 192.168.0.253 >> ~/.ssh/known_hosts"
        - "ssh-keyscan 192.168.1.0 >> ~/.ssh/known_hosts"
        - "ssh-keyscan 192.168.1.3 >> ~/.ssh/known_hosts"

[root@ansible-server env]# ansible-playbook keyscan.yml

PLAY [Setup for the Ansible's Environment] ********************************************************

TASK [Keyscan] ************************************************************************************
changed: [localhost] => (item=ssh-keyscan 192.168.0.242 >> ~/.ssh/known_hosts)
changed: [localhost] => (item=ssh-keyscan 192.168.0.248 >> ~/.ssh/known_hosts)
changed: [localhost] => (item=ssh-keyscan 192.168.0.217 >> ~/.ssh/known_hosts)
changed: [localhost] => (item=ssh-keyscan 192.168.0.252 >> ~/.ssh/known_hosts)

PLAY RECAP ****************************************************************************************
localhost                  : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
# vi ansible_env.yml
- name: Setup for the Ansible's Environment
  hosts: localhost
  gather_facts: no
  
  tasks:
    - name: Add "/etc/ansible/hosts"
      blockinfile: 
        path: /etc/ansible/hosts
        block: |
          [centos]
          192.168.0.242
          192.168.0.248

          [ubuntu]
          192.168.0.217 ansible_python_interpreter=/usr/bin/python3
          192.168.0.252 ansible_python_interpreter=/usr/bin/python3

    - name: Configure Bashrc
      lineinfile:   
        path: /root/.bashrc
        line: "{{ item }}"
      with_items:
        - "alias ans='ansible'"
        - "alias anp='ansible-playbook'"

[root@ansible-server env]# ansible-playbook ansible_env.yml
PLAY [Setup for the Ansible's Environment] ********************************************************

TASK [Add "/etc/ansible/hosts"] *******************************************************************
ok: [localhost]

TASK [Configure Bashrc] ***************************************************************************
ok: [localhost] => (item=alias ans='ansible')
ok: [localhost] => (item=alias anp='ansible-playbook')

PLAY RECAP ****************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
[root@ansible-server env]# ans all -m ping -k
SSH password:
192.168.0.248 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
192.168.0.242 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
192.168.0.252 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
192.168.0.217 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

0개의 댓글

관련 채용 정보