2016-08-19 1 views
2

내가 다음 파일 I이In Anabilities에서 한 작업의 환경 변수를 다른 작업에서 사용할 수 있도록 내보내려면 어떻게합니까?

foo.txt 
Hello world=1 
Hello world=2 
... 
Hello world=N 
Hello world=N+1 

IOW처럼, 또 다른 안녕하세요 세계 줄을 삽입하는 Ansible를 사용하려면

foo.txt 
Hello world=1 
Hello world=2 
... 
Hello world=N 

, 나는 집에 많은 안녕하세요 세계 선이 모르는이 말 나는, 스크립트를 통해 알아, 그리고 Ansible에서 N.

의 값을 얻을 필요가 나는 다음과 같은 쉘 작업이 있다고

- name: Find the last Hello world in foo.txt, and get the value of N 
    shell: > 
    # Get last Hello world line in foo.txt. I want to export this as an 
    # environment variable 
    LAST_HW_LINE=`awk '/Hello world/ {aline=$0} END{print aline}' "foo.txt"` 
    # Get left side of equation 
    IFS='=' read -ra LS <<< "$LAST_HW_LINE" 
    # Get the value of N 
    NUM=${LS##*\.} 
    # Increment by 1. I want to export this as an environment variable 
    NUM=$((NUM+1)) 
,

나는 계속해서 환경 변수를 사용하는 것보다이 작업을 수행하는이

- name: Add the next Hello world line 
    lineinfile: 
    dest: foo.txt 
    insertafter: "{{ lookup('env', 'LAST_HW_LINE') }}" 
    line: "Hello world={{ lookup('env', 'NUM') }}" 

어쩌면 거기에 더 나은 방법을 할 수 있도록하려면?

답변

2

Register Variables

- shell: /usr/bin/foo 
    register: foo_result 

Registering stdout as ansible variables

- debug: msg="{{ hello.stdout }}"  
- debug: msg="{{ hello.stderr }}" 

Including task with variables :

tasks: 
    - include: wordpress.yml wp_user=timmy 

Including role with variables

귀하의 경우를 들어 4,353,210

:

- name: so question 39041208 
    hosts: '{{ target | default("all") }}' 
    tasks: 
    - name: Find the last Hello world in foo.txt, and get the value of N 
# shell: awk '/Hello world/ {aline=$0} END{print aline}' "/home/ak/ansible/stackoverflow/q39041208.txt" 
    shell: awk '/Hello world/ {aline=$0} END{print NR}' "/home/ak/ansible/stackoverflow/q39041208.txt" 
    register: last_line 
    ignore_errors: true 
    - name: debug 
    debug: msg="last line {{ last_line.stdout }}" 
    - name: debug next number 
    debug: msg="next num {{ last_line.stdout | int + 1 }}" 
    - name: Add the next Hello world line 
    lineinfile: 
     dest: /home/ak/ansible/stackoverflow/q39041208.txt 
     insertafter: "{{ last_line.stdout }}" 
     line: "Hello world={{ last_line.stdout | int + 1 }}" 
+0

어떻게 내 특정 예에서 이런 짓을 했을까? LAST_HW_LINE과 NUM의 두 변수를 등록해야합니까? 아니면 그 일을 나눌까요? –

+0

@ChrisF 업데이트보기 –

관련 문제