티스토리 뷰

Ansible

fact 변수 활용

looeon 2024. 2. 20. 12:34

 

▶ fact 변수를 [ ' ' ] 로 사용해도 되고 . 으로 사용해도 된다

▷ setup 으로 fact 변수 체크 ( dictionary )

ansible node2 -m setup > node2.txt

 

▷ hostvars[inventory_hostname] 은 매직변수

>> 현재 작업중인 host 표시

- hosts: nodes
  tasks:
    - debug:
        msg:
          - eth0's ip {{ansible_eth0.ipv4.address}}
          - eth1's ip {{ansible_eth1.ipv4.address}}
          - eth0's ip {{ansible_facts['eth1']['ipv4']['address']}}
          - eth1's ip {{ansible_facts.eth1.ipv4.address}}
    - debug:
        var: "{{item}}"
      with_items:
        - hostvars[inventory_hostname].ansible_eth0.ipv4.address
        - hostvars[inventory_hostname]['ansible_eth1']['ipv4']['address']
    - debug:
        var: hostvars[inventory_hostname]

 

■ ansible-doc 의 리스트

ansible-doc -l

## hosts: all >> all 이란 그룹에는 localhost 가 존재하지 않는다

 

▶ 뒤에 있는 모듈을 처리해주는 게 action

▷ action 모듈은 존재하지 않는다

 

▷ 불가능

## when 뒤의 변수는 중괄호 쓰지 않는다

## 모듈자리에는 변수가 오지 못한다 

>>> 따라서 action 을 사용한다

- hosts: all
  tasks:
    - {{pkg_tool}}:
        name: nginx
        state: present
      when: ansible_distribution == 'ubuntu'

 

>> action 은 세로로 쓰니까 syntax 에러가 뜬다

- hosts: all
  tasks:
    - action: "{{ansible_pkg_mgr}}"
        name: nginx
        state: present
        update_cache: yes
      when: ansible_distribution == 'ubuntu'
    - action: "{{ansible_pkg_mgr}}"
        name: nginx
        state: latest
      when: ansible_distribution == 'centos'

 

▷ 가능

>> action은 한줄로 + 가로로 쓰면 : 를 = 로 바꾼다

## 뭐지 이렇게 하면 더 짧게 되는데

( ubuntu centOS 둘다 service와 get_url 모듈이 동일한데 왜 책에서는 따로 쓰지..? )

- hosts: all
  become: yes
  tasks:
    - action: "{{ansible_pkg_mgr}} name=epel-release state=latest"
      when: ansible_distribution == 'CentOS'
    - action: "{{ansible_pkg_mgr}} name=nginx state=present update_cache=yes"
      when: ansible_distribution == 'Ubuntu'
    - action: "{{ansible_pkg_mgr}} name=nginx state=latest"
      when: ansible_distribution == 'CentOS'

    - get_url:
        url: https://www.nginx.com
        dest: /usr/share/nginx/html
        mode: 0644

    - service:
        name: nginx
        state: restarted

 

 

▶ task 만 포함된 부분을 include 해야 한다 host 는 하면 안된다

▷ 자주 쓰는 파일은 include 를 포함해서 꺼내 쓸 수 있다

- hosts: localhost
  tasks:
    - debug:
        msg: test messages
    - include_tasks: a.yml
    - debug:
        msg: test messages2
    - include_tasks: b.yml

 

>> a.yml 과 b.yml 은 완전한 playbook 인데

>> task 사이에 또 들어가므로 에러가 뜬다

 

● a.yml 을 수정

  tasks:
    - command: /usr/bin/nmcli
      register: ll
    - debug:
        msg: "{{ll}}""

 

● b.yml 수정

    - debug:
        msg: "{{ansible_host}}"
    - debug:
        msg: "{{stdout_lines}}"

 

>>> a.yml 은 오류 / b.yml 은 정상 작동

>>> include_tasks 에 해당되는 파일은 tasks 까지 포함되면 안된다 모듈만 남겨야 됨

 

 

# 모듈의 위치는  /usr/lib/python2.7/site-packages/ansible/modules 여기인듯..?

'Ansible' 카테고리의 다른 글

Template ( 템플릿 )  (0) 2024.02.20
Handlers 추가내용  (0) 2024.02.20
Ansible playbook 의 암호화  (0) 2024.02.20
매직변수  (0) 2024.02.19
Ansible playbook 활용하여 fingerprint/공개키  (0) 2024.02.19
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
글 보관함