How can I pass variable to ansible playbook in the command line?

后端 未结 10 1898
不知归路
不知归路 2021-01-29 21:57

I\'m new to ansible and wonder how to do so as the following didn\'t work

ansible-playbook -i \'10.0.0.1,\' yada-yada.yml --tags \'loaddata\' django_fixtures=\"t         


        
相关标签:
10条回答
  • 2021-01-29 22:07
     s3_sync:
          bucket: ansible-harshika
          file_root: "{{ pathoftsfiles  }}"
          validate_certs: false 
          mode: push
          key_prefix: "{{ folder }}"
    

    here the variables are being used named as 'pathoftsfiles' and 'folder'. Now the value to this variable can be given by the below command

    sudo ansible-playbook multiadd.yml --extra-vars "pathoftsfiles=/opt/lampp/htdocs/video/uploads/tsfiles/$2 folder=nitesh"
    

    Note: Don't use the inverted commas while passing the values to the variable in the shell command

    0 讨论(0)
  • 2021-01-29 22:10

    You can use the --extra-vars option. See the docs

    0 讨论(0)
  • 2021-01-29 22:10

    ansible-playbok -i <inventory> <playbook-name> -e "proc_name=sshd"

    You can use the above command in below playbooks.

    ---
    - name: Service Status
    gather_facts: False
    tasks:
    - name: Check Service Status (Linux)
    shell: pgrep "{{ proc_name }}"
    register: service_status
    ignore_errors: yes
    debug: var=service_status.rc`
    
    0 讨论(0)
  • 2021-01-29 22:13

    Other answers state how to pass in the command line variables but not how to access them, so if you do:

    --extra-vars "version=1.23.45 other_variable=foo"
    

    In your yml file you assign these to scoped ansible variables by doing something like:

    vars:
        my_version: "{{ version }}"
        my_other_variable: {{ other_variable }}
    

    An alternative to using command line args is to utilise environmental variables that are already defined within your session, you can reference these within your ansible yml files like this:

    vars:
        my_version: "{{ lookup('env', 'version') }}"
        my_other_variable: {{ lookup('env', 'other_variable') }}
    
    0 讨论(0)
  • 2021-01-29 22:14

    Reading the docs I find the section Passing Variables On The Command Line, that give this sample:

    ansible-playbook release.yml --extra-vars "version=1.23.45 other_variable=foo"
    

    Others examples demonstrate how to load from JSON string (≥1.2) or file (≥1.3)

    0 讨论(0)
  • 2021-01-29 22:14
    ansible-playbook release.yml --extra-vars "username=hello password=bye"
    
    #you can now use the above command anywhere in the playbook as an example below:
    tasks:
    - name: Create a new user in Linux
    shell: useradd -m -p {{username}} {{password}}"
    
    0 讨论(0)
提交回复
热议问题